假设我有以下两种方法:
在method1中,我们为ModelClass分配一个值。
method1(ModelClass mc)
{
mc.name = "ABC";
mc.contact = 123;
mc.salary = 123.00;
//how to store this data for use in method2 here
}
method2是参数化方法,参数与模型类不同。
method2(string Address, long officeContact)
{
//and how to retrieve stored data from method1 to use here
Hashtable ht = new Hashtable();
ht.Add("@name", method1.name);
ht.Add("@contact", method1.contact);
ht.Add("@salary ", method1.salary);
ht.Add("@Address", Address);
ht.Add("@officeContact", officeContact);
}
如何使用从method1到method2的多个值。示例代码将有助于理解我的问题。
在您的示例中,method1
正在设置要传递的对象的属性。
为了进一步修改ModelClass
您的实例,您需要向method2
类型添加另一个参数ModelClass
:
method2(string Address, long officeContact, ModelClass mc)...
要么
用于的ModelClass实例method1
必须位于method2
可以看到它的范围内。像这样:
void SomeMainMethod()
{
var mainClass = new MainClass();
method1(mainClass);
method2("some address", 1);
}
已修改 method2
method2(string Address, long officeContact)
{
//and how to retrieve stored data from method1 to use here
Hashtable ht = new Hashtable();
ht.Add("@name", mainClass.name);
ht.Add("@contact", mainClass.contact);
ht.Add("@salary ", mainClass.salary);
ht.Add("@Address", Address);
ht.Add("@officeContact", officeContact);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句