2014-01-23 56 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

相关问题