![]() |
| ||
| Trying to obtain text from a file. Hi Guys I am new to C# programming and I wandered whether anyone could help me regarding this error message below: C:\MyFirstCOMTemplate\COM\PushTemplateC\PushTemplateC.cs(86): 'PushTemplateC.PushTemplateC.PblReadFile()': not all code paths return a value I am simply trying to get the text below: My name is Neil Rastogi I am aged 25 to be output in a listbox. I have created a simple data2.txt file using streamwriter class below within a c# component: public void PblAddFile()The method above works fine. The second method below is what is causing the error message below: public string PblReadFile()I am not sure really what is wrong can anyone suggest any ideas? Thanks. |
| ||
| Re: Trying to obtain text from a file. Hi, your problem is not related to the text file that you want to read. The real problem is because you create a method that has a return value of type string, but inside the method's logic, there's a program flow that will end up with not giving any return value. I don't know what is exactly do you want with the second method. Since inside the loop, there's a return statement, meaning if you invoke this method, it will quit directly after reading the first line of your text file, and give the second line of your text file as a return value. Is this really what you want? Ok, back to the problem, so let's say there's nothing inside your text file. So the flow of the program never goes inside the loop. The flow jump into the next line after the while(...) { .... }, closing the sr and fsIn, then... then what? here's the things who makes the problem, since after closing the sr and fsIn, the flow is end, without any return value given. Hope that's help. regards, Lok |
| ||
| Re: Trying to obtain text from a file. Hi Thanks for your help so far but I am not sure exactly what the solution is? Are you sugesting the while loop code should be omitted? I did this below: public string PblReadFile() This compiles fine but does not do anything. Can you provide a bit more clarification please. |
| ||
| Re: Trying to obtain text from a file. http://www.csharp-station.com/HowTo/...eTextFile.aspx using System; swap the Console.WriteLine for lbLines.Items.Add I guess |
| ||
| Re: Trying to obtain text from a file. Quote:
Before I continue, can you explain first, what should method PblReadFile return? In your program, it return one/single string, but actually the data on your text file is multi-line, so what do you want? do you want to return the whole text file inside one string? or actually you want a multi string (one string for each lines)? My best guess is you better return an array of string, so it'is also more easier for you to loop the array, and put each string item into a listbox. if not, use StreamReader.ReadToEnd(), return it as single string, then split it using String.Split("\n\r"), and put it into listbox one by one. Regards, Lok |
| ||
| Re: Trying to obtain text from a file. I think your problem is the way you were trying ot use the StreamReader object Please see below, for some pointers Hope this helps, class Program |
| ||
| Re: Trying to obtain text from a file. You mentioned in your original post that you wanted to just post this text data to a listbox. I took your public string PblReadFile() and changed it to public void PblReadFile() then commented out your and it populated the listbox just fine.Jerry |
| All times are GMT -4. The time now is 6:54 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC