2014-02-22 69 views
0

爲了達到的可管理性,我需要有一個VariablesMethods的類。 問題是我不知道如何訪問方法類中分配的變量的值。如何從一個變量中獲取值,該值是在另一個類中賦值的變量?

這裏是可變的類,它只是持有變量:

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

可變'myVarClass'在方法'Main'和場''的類MyMethods'是myVarClass'不同的對象。 –

回答

2

問題:您在兩個不同的對象的工作。

  1. 您正在創建一個對象,其實例變量myVarClassMyMethods類中。
  2. 您正在使用Main()方法中的實例變量myVarClass創建另一個新對象。

注:你應該總是remeber,在面向對象的編程對象是獨立的,保持自己的副本,以便修改一個對象參數/屬性不列入影響其他對象參數或屬性。

溶液:而不是創建兩個不同的對象創建具有例如在Main()方法可變myVarClass只有一個對象,並將其傳遞給myClass方法。

所以你應該改變你的myClass方法DoSomeStuff()如下接受Main()方法`MyVariableClass``

public void DoSomeStuff(MyVariableClass myVarClass, string myString1, string myString2) 
{ 
    //do something 
} 

實例變量調用如下上述方法:

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(); 
     } 
    } 
} 
+0

'Main()方法中的實例變量myVarClass'它是局部變量。 –

+0

@HamletHakobyan:我沒有得到你,請你詳細說明一下嗎? –

+0

謝謝我的朋友們:這些都是非常好的和有趣的解決方案。 – Asynchronous

1

MyMethodsmyVarClass成員是默認私有的,所以如果你希望能夠從類本身之外調用它,那麼你需要使它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(); 
} 

另外,要小心的是,在main定義的myVarClass是一個完全不同的對象。

乾杯

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(); 
     } 
    } 
} 
+0

你可以告訴我在上面的方法中實例化類時是否需要條件語句?試圖理解你的方法。 (MyVarClass == NULL) { MyVarClass = new MyVariableClass(); }' – Asynchronous

+0

延遲初始化。您可以避免這種構造,並在構造函數或聲明中初始化它。 –

相關問題