use*_*798 1 c# class parameter-passing
我不知道这是否可行,但感觉应该是从使用 C# 到现在为止。
我想要一堆静态类,其中包含库用户的“设置值”,以将其作为参数发送到另一个类中。
这就是我要去的地方,但我无法弄清楚。下面只是我的想法的一个例子,所以不要试图找出“为什么”:-)
首先 - 将被调用的类
public class myClass
{
public bool isError { private set; get; }
public DataTable output { private set; get; }
public String filename { set; private get; }
public settingModule settings { set; private get; }
public static void execute()
{
//Call Private 'getTheData'
//set isError accordingly
//Load output
}
private static DataTable getTheData()
{
//Open and read file for 'fileName'
//Use settings.startingRow
//Use settings.fileType
//User settings.skipEmpty
//Do some stuff
return Datatable from workings
}
}
Run Code Online (Sandbox Code Playgroud)
第二 - 我希望用户通过的课程
public static class settingMobule
{
public static class fileTypeA
{
public static int startingRow = 1;
public static String fileType = "txt";
public static bool skipEmpty = true;
}
public static class fileTypeB
{
public static int startingRow = 10;
public static String fileType = "csv";
public static bool skipEmpty = false;
}
public static class fileTypeC
{
public static int startingRow = 3;
public static String fileType = "hex";
public static bool skipEmpty = true;
}
}
Run Code Online (Sandbox Code Playgroud)
最后我希望能够调用它的方式
myClass test = new myClass();
test.filename = "c:\\temp\\test.txt;
test.settings = settingModule.fileTypeA;
test.execute();
if(test.isError == false
{
DataTable myTable = test.output;
test.dispose()
}
Run Code Online (Sandbox Code Playgroud)
预先感谢...是的,“你的坚果有更好的方法”是一个完全有效的答案:-)
我也很想知道如何将 .dispose() 添加到我的代码中,这不是我必须要做的事情,但当我在这里时......:-D
不,基本上;但你可以这样做:
public sealed class SettingMobule
{
public int StartingRow {get; private set;}
public string FileType {get; private set;}
public bool SkipEmpty {get; private set;}
private SettingMobule(int startingRow, string fileType, bool skipEmpty)
{
StartingRow = startingRow;
FileType = fileType;
SkipEmpty = skipEmpty;
}
public static SettingMobule FileTypeA {get;}
= new SettingMobule(1, "txt", true);
public static SettingMobule FileTypeB {get;}
= new SettingMobule(10, "csv", false);
public static SettingMobule FileTypeC {get;}
= new SettingMobule(3, "hex", true);
}
Run Code Online (Sandbox Code Playgroud)
并SettingMobule.FileTypeA作为实例传递等。
| 归档时间: |
|
| 查看次数: |
1085 次 |
| 最近记录: |