2013-11-28 29 views
0

我必須從系統中加載一個文本文件,我收到以下錯誤:不能轉換類型就是System.IO.StreamReader翻番

Cannot Convert type System.IO.Streamreader to double

class Program 
{ 
    static void Main(string[] args) 
    { 
     Double num1; 
     int num2; 
     Double mul; 
     Double div; 
     Double result; 
     Double conv; 

     // this we are adding m3 size using int num1 
     Console.WriteLine("What is M3 Size"); 
     num1 = Convert.ToDouble(Console.ReadLine()); 

     // now we are adding buying price using number 2 
     Console.WriteLine("What is buying price"); 
     num2 = Convert.ToInt32(Console.ReadLine()); 

     System.IO.StreamReader file = new System.IO.StreamReader("c:\\conversion.txt"); 
     conv = new System.IO.StreamReader("c:\\conversion.txt"); 

     div = num2/conv; 
     mul = num1 * 128; 
     result = div + mul; 

     Console.WriteLine(" \n\nC&F $ {0}", result); 

     Console.ReadLine(); 
     } 
    } 
} 
+0

獲得對於未來的問題,請記住,你的問題並不需要跟公司有關的或你自己的背景故事。它應該包含3點:描述問題(包括錯誤信息),到目前爲止的嘗試以及預期的+實際輸出。大多數其他事情都是混亂的。 –

+0

好吧,我不知道這一點。謝謝 – user3047162

回答

2

的StreamReader的只是讀者,你真正需要.Read()在文件

using (TextReader reader = new StreamReader("C:\blabla.txt")) 
{ 
    //Create a reader - the using ensures that the system cleans up when we're done. 

    //Read the whole file as a single string 
    string fileContents = reader.ReadToEnd(); 

    //Parse the string to a double 
    double conv= Double.Parse(fileContents); 

} 
+3

真的,它應該''Dispose'd,理想情況下在'使用'塊。開始學習良好實踐永遠不會太早。 –

+0

有效的點。編輯。 – Haedrian

+0

非常感謝你們反應太快,非常感謝它。現在,如果我使用這個即時通訊得到這個錯誤「命名空間TextReader找不到你是否缺少程序集引用,並StreamReader以及」 – user3047162

0

您聲明變量convdouble,但隨後嘗試將其分配給StreamReader

請閱讀如何在提供的MSDN文章中使用StreamReader

+0

是的,因爲如果我無法描述它加倍,它不能得到/通過雙重,因爲我也需要十進制值,所以沒有選擇留給我,而不是描述爲雙重 – user3047162

相關問題