Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在c中存储和访问多个值#_C#_Winforms_Variables - Fatal编程技术网

C# 在c中存储和访问多个值#

C# 在c中存储和访问多个值#,c#,winforms,variables,C#,Winforms,Variables,我正在寻找一种简单而优雅的方法来保存多个字符串,并在winforms应用程序中以同样简单的方式访问它 我有10个字符串变量,我用一种形式,每次我做一些改变,按下一个按钮或任何其他行动,我需要更新这些值 下面是我如何用数据填充这些变量的代码: string cDir = clientsbox2.SelectedItem.ToString(); string ClientPath = PMFunc.XMLDir(settings) + cDir; string ProjectPath = PMFun

我正在寻找一种简单而优雅的方法来保存多个
字符串
,并在winforms应用程序中以同样简单的方式访问它

我有10个字符串变量,我用一种形式,每次我做一些改变,按下一个按钮或任何其他行动,我需要更新这些值

下面是我如何用数据填充这些变量的代码:

string cDir = clientsbox2.SelectedItem.ToString();
string ClientPath = PMFunc.XMLDir(settings) + cDir;
string ProjectPath = PMFunc.XMLDir(settings) + cDir + @"\"
                     + outlookGrid1.CurrentRow.Cells[0].Value.ToString();
string pathString = Path.Combine(ProjectPath);
string path3DString = Path.Combine(ProjectPath + @"\02-3D");
string pathDatosString = Path.Combine(ProjectPath + @"\01-Datos");
string pathImagenesString = Path.Combine(ProjectPath + @"\03-Imagenes");
string pathProcesso3D = Path.Combine(ProjectPath + @"\02-3D\Processo");
string pathTrafico3D = Path.Combine(ProjectPath + @"\02-3D\Trafico");
string maxfilename = path3DString + @"\" + clientsbox2.SelectedItem.ToString() 
                    + @"-" + PMFunc.ReturnWipNumber(cDir,
                    outlookGrid1.CurrentRow.Cells[0].Value.ToString().ToString(),
                    outlookGrid1.CurrentRow.Cells[2].ToString()) + @"-"
                    + outlookGrid1.CurrentRow.Cells[0].Value.ToString() + @"-"
                    + PMFunc.ReturnCopyNumber(cDir,
                    outlookGrid1.CurrentRow.Cells[0].Value.ToString().ToString(),
                    outlookGrid1.CurrentRow.Cells[2].ToString()) + @".max";
正如您所看到的,其中一些是由表单中的一些对象生成的,一些是由
PMFunc
类中的方法填充的,我在单独的文件中提供了这些方法

所以每次我需要填充这些变量时,我都在使用这段代码,但我相信有一种方法可以更容易地完成


您能给我一些建议吗?

另一种选择是使用读取组件值的getter将这些变量转换为表单属性

public string cdir
{
    get
    {
       return clientsbox2.SelectedItem.ToString();
    }
}

... etc

当需求需要时,重复执行相同的代码没有错。唯一错误的是重复复制/粘贴此代码。如果它不是一个函数,就把它变成一个函数,并在需要的时候调用它——这样你就可以多次调用它,而只编写一次。你的意思是把它封装在一个单独的方法中?如果是-如何访问数据?它应该看起来像是声明了这些变量的单独类,但我无法从单独的类访问表单元素。@Oleg.Budeanu是的,您应该停止使用
@“\”
,因为您已经知道
路径。组合
,您不需要
@
这里
“-”
。感谢您的提示,但我仍然无法将此块用作方法,因为我需要所有这些值,不仅需要填写,而且以后可以访问。@Oleg.Budeanu只需在表单中添加一个方法即可。您已经可以访问所有数据,否则您将无法通过复制/粘贴使用这些数据。对不起,我不明白。。。您的意思是,通过稍后访问cdir(如果我将添加您的代码块),它将计算get{}块?是的,正是这样。这称为延迟执行,当您访问此属性时,它会执行getter。我想Tim会回答是的。基本上,您将为列出的每个变量定义一个属性或编写一个函数()。如果你有访问表单控件,那么每个表单上都需要有道具/函数。非常感谢,这正是我想要的!这些都是你在没有老师的情况下自学语言的缺点。。你可能会错过这些基本的东西。