2014-01-23 59 views
-5

對於你們中的一些人來說,這是一個業餘愛好者的問題,但我可以弄明白。我知道如何去我的程序中的其他方法,因爲他們不需要任何參數。當我試圖回到主要方法時,我在括號裏面寫了什麼?如何從另一種方法獲得主要方法c#

static void writeToFile(string filename, Customer obj, int pos, int size) 
{ 

    FileStream fout; 

    BinaryWriter bw; 

    //create a file stream object 

    fout = new FileStream(filename, FileMode.Open, FileAccess.Write); 

    //create a binary writer object 
    bw = new BinaryWriter(fout); 

    //set file position where to write data 
    fout.Position = pos * size; 
    //write data 
    bw.Write(obj.CustomerNo); 
    bw.Write(obj.Surname); 
    bw.Write(obj.Forename); 
    bw.Write(obj.Street); 
    bw.Write(obj.Town); 
    bw.Write(obj.DOB); 
    //close objects 
    bw.Close(); 
    fout.Close(); 

    Main(); // what goes inside these parenthesis 
} 
+0

'ClassName.Main();'??? – 2014-01-23 00:55:02

+0

你沒有。主要方法在程序運行開始時執行,並在程序運行結束時結束。如果你想去別的地方,然後回到主體,請撥打一個方法。 –

+2

我很驚訝。你問過幾個C#的問題,你不知道這個? – puretppc

回答

3
public static void main() 
{ 
    // do some stuff 
    // ... 
    WriteToFile(filename, obj, pos, size); 
    // ... 
    // Program execution automatically returns here after WriteToFile is done. 
    // do some more stuff 
    // ... 
    // Program ends. Thank you for playing. 
} 

static void WriteToFile(string filename, Customer obj, int pos, int size) 
{ 
    // yada yada 
    // No need for a Main() call 
    // We're done, and about to leave the WriteToFile method. See you later. 
} 
+0

在我的主要,我去一個方法調用然後我去另一個叫做addCust的方法,然後在這個方法的底部是writeToFile(@「.. \ .. \ .. \ Files \ Customers.txt」,_cust,pos,Record_Size); //結束方法然後這將我帶到我的WriteToFile方法...所以當它回來時,它已經沒有什麼可以這樣做退出..希望你現在可以理解爲什麼我不能回到主要,爲什麼有需要叫它。 – BigGizzy

+0

然後,在我的例子中,只需將'cust'替換爲'WriteToFile',它仍然有效。方法調用可以嵌套; 'writeToFile'將返回到'addCust',它將返回到'cust',它將返回到'Main()'。 –

+0

對不起,我不能理解這一點。如果你想說你想說的話,有沒有辦法調用主體? – BigGizzy

相關問題