为了可管理性,我需要为Variables
和Methods
设置一个课程。
问题是我不知道如何访问方法类中指定的变量的值。
这是变量类,它只包含变量:
namespace Classes
{
class MyVariableClass
{
public string MyVariable { get; set; }
}
}
以下是方法: 我在方法类中调用变量类的实例,以便我可以将变量赋值给方法的值。
namespace Classes
{
class MyMethods
{
MyVariableClass myVarClass = new MyVariableClass();
public void DoSomeStuff(string myString1, string myString2)
{
myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
最后,下面是主要方法: 当我运行此代码时,MyVariable返回null,我假设我在为变量赋值之前访问该变量?
如何使用Method类中指定的值获取变量?
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myVarClass.MyVariable);
Console.ReadKey();
}
}
}
答案 0 :(得分:2)
问题:您正在处理两个不同的对象。
myVarClass
类中创建一个具有实例变量MyMethods
的对象。myVarClass
创建另一个新对象。注意:您应该始终记住,面向对象的编程对象是独立的并且维护自己的副本,因此修改一个对象参数/属性不会影响其他对象参数或属性。
解决方案:而不是创建两个不同的对象,只在myVarClass
方法中创建一个具有实例变量Main()
的对象,并将其传递给myClass
方法。 / p>
因此您应该更改myClass
方法DoSomeStuff()
,如下所示接受`MyVariableClass``的实例变量
public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
{
//do something
}
从Main()
方法调用上面的方法如下:
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(myVarClass, something, nothingHere);
完整代码:
namespace Classes
{
class MyMethods
{
public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2)
{
myVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
和主程序应该是:
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(myVarClass, something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myVarClass.MyVariable);
Console.ReadKey();
}
}
}
答案 1 :(得分:1)
myVarClass
的{{1}}成员默认为私有,因此如果您希望能够从类外部调用它,那么您需要将其设为MyMethods
。< / p>
一旦公开,您就可以:
public
另外,请注意static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
MyVariableClass myVarClass = new MyVariableClass();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myMethod.myVarClass.MyVariable);
Console.ReadKey();
}
中定义的myVarClass
是完全不同的对象。
干杯
答案 2 :(得分:1)
将您的课程定义为:
namespace Classes
{
class MyMethods
{
public MyVariableClass MyVarClass {get; private set;}
public void DoSomeStuff(string myString1, string myString2)
{
if(MyVarClass == null)
{
MyVarClass = new MyVariableClass();
}
MyVarClass.MyVariable = (!string.IsNullOrEmpty(myString2)
? myString2 : "String 2 has nothing!");
}
}
}
然后用作:
namespace Classes
{
class Program
{
static void Main(string[] args)
{
MyMethods myMethod = new MyMethods();
string something = "something";
string nothingHere = null;
myMethod.DoSomeStuff(something, nothingHere);
//I need to call MyVariable here
//Or be able to access it's values assigned in the MyMethod Class.
Console.Write(myMethod.MyVarClass.MyVariable);
Console.ReadKey();
}
}
}