2013-01-01 17 views
0

我的計劃是有一些方法,其中有些是調用一些其他的方式,我的問題是,我想用一些數據的方法在以前的方法產生,我不某些特定數據知道我應該怎麼做。我如何可以存儲來自我的方法

namespace MyProg 
{ 
    public partial class MyProg: Form 
    { 
     public static void method1(string text) 
     { 
      //procedures 
      method2("some text"); 
      // Here i want to use the value from string letter from method2. 
     } 

     public static void method2(string text) 
     { 
      //procedures 
      string letter = "A"; //Its not A its based on another method. 
     }  
    } 
} 

回答

3

只需使用返回值:

public partial class MyProg: Form 
{ 
    public static void method1(string text) 
    { 
     string letter = method2("some text"); 
     // Here i want to use the value from string letter from method2. 
    } 

    public static string method2(string text) 
    { 
     string letter = "A"; //Its not A its based on another method. 
     return letter; 
    }  
} 

Methods

方法可以返回一個值給調用者。如果返回類型,方法名前列出的類型 ,是不是空白,則該方法可以返回 使用返回關鍵字的值。以關鍵字 返回其次是相匹配的返回類型將返回 該值的方法調用者的值的聲明...


既然你提到你不能使用返回值,另一個選項是使用out parameter

public static void method1(string text) 
{ 
    string letter; 
    method2("some text", out letter); 
    // now letter is "A" 
} 

public static void method2(string text, out string letter) 
{ 
    // ... 
    letter = "A"; 
} 
+0

是的,但我的方法不能改變,以靜態的字符串它必須是靜態無效的,無論哪種方式,我不能在這一點上使用的返回值。 – Incognito

+0

@Incognito:另一種方法是使用'out'參數,編輯我的答案。 –

+0

非常感謝,這將做的工作就好了,雖然我的價值是3steps深,但我也跟着你的例子,你用恰當的方式了。 – Incognito

0

您可以在值存儲在類(在這種情況下,必須是靜態的,因爲提到它的方法是靜態)的成員變量,也可以從方法2返回的值,並調用方法2從內部方法1,你要使用它。

我會留給你弄清楚如何編碼它。

相關問題