2011-09-20 113 views
1
static void Main(string[] args) 
{ 
    int test = 1; 

    resetTest(); 

    Console.Write(test); // Should be 0 
} 

static void resetTest() 
{ 
    test = 0; 
} 

爲什麼它不起作用?我怎麼才能得到它? (我不想使用int函數並返回變量)現在我有一個錯誤消息,它說明函數resetTest上的變量測試未定義。通過void函數重置變量

回答

5

因爲測試是在您的Main方法的範圍內定義的。

您需要將其移到該方法之外才能使用。

喜歡的東西:

static int test = 1; 

static void Main(string[] args) 
{ 

    resetTest(); 

    Console.Write(test); // Should be 0 
} 

static void resetTest() 
{ 
    test = 0; 
} 
+1

我明白,非常感謝。我會在8分鐘內給你答覆。 – Luis

+2

儘管全局變量很有用,但必須小心使用它們。它可能會成爲你最糟糕的噩夢。 – Sergio

2

變量測試是Main程序本地的。你將不得不在該範圍之外聲明它。

static int test = 0; 

static void Main(string[] args) 
{ 
    test = 1; 
    Console.Write(test); // Should be 1 
    resetTest(); 
    Console.Write(test); // Should be 0 
} 

static void resetTest() 
{ 
    test = 0; 
} 
2

這不是空回報問題。問題是測試的範圍是Main方法。你必須把它變成一個靜態的領域去做你想做的事。

2

這將工作

static int test = 1; 
static void Main(string[] args) 
{ 


    reserTest(); 

    Console.Write(test); // Should be 0 
} 

static void resetTest() 
{ 
    test = 0; 
} 
2

測試是一個局部變量的主要功能。所以你不能從一個單獨的函數訪問它。

如果你想這樣做,你需要將它提升爲你的類的一個(靜態的,在你的情況下)成員變量。

2

testMain()的局部變量。因此resetTest()甚至無法訪問它,忘記重置它。

2

test在您的功能resetTest()中無法訪問,因爲本地變量test的範圍是您的Main函數。

你必須通過裁判通過它,如果你想在功能resetTest()

2

有幾個選項來改變數值。

1)可以傳遞變量,通過參考,給該函數:

static void resetTest(ref int test) 
{ 
    test = 0; 
} 

這樣就可以改變這種(或任何其他)變量。

2)你可以聲明其主外,作爲靜態INT本地類型本身:

static int test; 

static void Main(string[] args) 
{ 
    resetTest(); 

    Console.Write(test); // Should be 0 
} 

static void resetTest() 
{ 
    test = 0; 
} 

這使得它在作用域整個類型。

2

它不起作用,因爲您將test定義爲Main靜態方法的本地。嘗試並將其定義爲

private static int test = 1; 

在您的類定義中,但在任何方法之外。

2

這是一個範圍和靜態變量問題。現在測試的範圍僅限於主要功能,因此resetTest甚至無法訪問它。

您目前已將resetTest設置爲static method。要更改靜態方法中的任何變量,該變量要麼需要作爲參考傳遞(在另一個答案中回答),要麼變量需要是該類的靜態變量。

因此,你在目前有一些不匹配的代碼。

public class SomeClass { 
    public int test = 1; 

    static void Main(string[] args) { 
     SomeClass myClass = new SomeClass(); // Will instantiate test as 1 
     myClass.resetTest(); 

     Console.Write(myClass.test); // Should be 0 
    } 

    public void resetTest() { 
     test = 0; 
    } 
} 
2

測試變量不休息,如果你想實現這一目標,不僅測試,但任何詮釋到你的應用程序。這樣,你就應該Reference

static void Main(string[] args) 
{ 
    int test = 1; 
    reserTest(ref test); 

    Console.Write(test); // Should be 0 now 
} 

static void resetTest(ref int i) 
{ 
    i = 0; 
} 
2

傳遞變量所定義的主要方法裏面的「測試」變量,所以它只存在裏面。

你可以(不這樣做,只是因爲你並不意味着你應該)1:

  • 放置方法

    int test = 1; 
    
    static void Main(string[] args) 
    { 
        resetTest(); 
    
        Console.Write(test); // Should be 0 
    } 
    
    static void resetTest() 
    { 
        test = 0; 
    } 
    
  • 外的聲明發送變量通過參考:

    static void Main(string[] args) 
    { 
        int test = 1; 
        resetTest(ref test); 
    
        Console.Write(test); // Should be 0 
    } 
    
    static void resetTest(ref int test) 
    { 
        test = 0; 
    } 
    

無論哪種方式,你應該讀一下關於示波器:http://www.blackwasp.co.uk/CSharpVariableScopes.aspx