2013-11-28 28 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

相关问题