2012-02-07 139 views
0
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int id = 0, stock = 0, published = 0, newstock; 
      double price = 0.00; 
      string type = " ", title = " ", author = " "; 

      Program inventroy = new Program(); 
      inventroy.read_one_record(ref id, ref stock, ref published, ref price, ref type, ref title, ref author); 

      Console.WriteLine("Update Number In Stock"); 
      Console.WriteLine("======================="); 
      Console.Write("Item ID: "); 
      Console.WriteLine(id); 
      Console.Write("Item Type: "); 
      Console.WriteLine(type); 
      Console.Write("Price: "); 
      Console.WriteLine(price); 
      Console.Write("Number In Stock: "); 
      Console.WriteLine(stock); 
      Console.Write("Title: "); 
      Console.WriteLine(title); 
      Console.Write("Author/Artist: "); 
      Console.WriteLine(author); 
      Console.Write("Published: "); 
      Console.WriteLine(published); 
      Console.WriteLine("======================="); 

      Console.Write("Please Enter New Stock Number: "); 
      string line = Console.ReadLine(); 
      newstock = int.Parse(line); 

      Program writeinv = new Program(); 

      writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author); 
     } 

     void read_one_record(ref int id, ref int stock, ref int published, ref double price, ref string type, ref string title, ref string author) 
     { 
      StreamReader myFile = File.OpenText("Inventory.dat"); 

      id = int.Parse(myFile.ReadLine()); 
      stock = int.Parse(myFile.ReadLine()); 
      published = int.Parse(myFile.ReadLine()); 
      price = double.Parse(myFile.ReadLine()); 
      type = myFile.ReadLine(); 
      title = myFile.ReadLine(); 
      author = myFile.ReadLine(); 

      myFile.Close(); 

     } 
     void write_one_record(int id, int newstock, int published, double price, string type, string title, string author) 
     { 
      StreamWriter myFile = new StreamWriter(File.OpenWrite("Inventory.dat")); 


      myFile.WriteLine(id); 
      myFile.WriteLine(newstock); 
      myFile.WriteLine(published); 
      myFile.WriteLine(price); 
      myFile.WriteLine(type); 
      myFile.WriteLine(title); 
      myFile.WriteLine(author); 

      myFile.Close(); 
     } 
    } 
} 

有人可以編譯這個(或者只是粘貼代碼到編譯器中)並告訴我爲什麼writeinv.write_one_record(ref id,ref newstock,ref published,ref price,ref type,ref title,ref author);說它有無效的論點?重載方法?爲什麼這個功能不起作用?

「爲 'as2.programs.write_one_record(INT,INT,INT,雙,字符串,字符串,字符串)' 最佳重載的方法匹配具有一些無效的參數。

+0

錯誤說的是什麼? – SLaks 2012-02-07 01:25:50

+0

請減少您的代碼示例。 (也就是刪除**所有**,除了使錯誤出現所需的東西外)。 – millimoose 2012-02-07 01:26:25

+0

你需要void write_one_record(ref int id,ref int newstock,... – shenhengbin 2012-02-07 01:26:47

回答

3

您定義write_one_record爲

void write_one_record(int id, int newstock, int published, double price, string type, string title, string author) 

- >無ref參數

,但你把它作爲

writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author); 

- >所有ref參數

+0

謝謝。這很好解釋,我不知道我是如何錯過它的。謝謝。 – Nogg 2012-02-07 01:32:32

+0

毆打我9秒... – 2012-02-07 01:41:11

+0

@ M.Babcock只是+1了你... – 2012-02-07 01:42:50

0

你錯過了ref關鍵字在每個參數

如果你想打電話write_one_record(...)時使用的refkeyword你需要在方法聲明爲好。就像你在void red_one_record(...)

void write_one_record(ref int id, ref int newstock, ref int published, ref double price, ref string type, ref string title, ref string author) 

如果您不打算使用ref,請將其從方法調用中刪除。

+0

你在說什麼?我讀完和寫完全一樣。 – Nogg 2012-02-07 01:31:04

+1

不,你沒有在方法聲明中包含'ref'。 – Gabe 2012-02-07 01:32:39

+0

Cehck我的更新回答 – Gabe 2012-02-07 01:33:00

0

因爲write_one_record參數不聲明作爲「參考」,但你通過他們作爲「參考」。

2

write_one_record不會在您的通話包含任何ref參數尚未就他們都做...

鑑於您的方法使用參數的方式,它應該被稱爲:

writeinv.write_one_record(id, newstock, published, price, type, title, author); 

相反比:

writeinv.write_one_record(ref id, ref newstock, ref published, ref price, ref type, ref title, ref author); 
1

請首先閱讀ref (C# Reference)

你需要還可以添加ref關鍵字,以你的方法參數

write_one_record(ref int id, ref int newstock , .... 
0

要麼不使用ref或直接,你當你打電話給你的方法使用的每個參數前加上「裁判」。