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()
{
//int PrvFile = FreeFile();
try
{
// Create a new file to work with
FileStream fsOut = File.Create("C:\\data2.txt");
StreamWriter sw = new StreamWriter (fsOut);
sw.WriteLine("My name is");
sw.WriteLine("Neil Rastogi");
sw.WriteLine("I am aged");
sw.WriteLine("25");
sw.Flush();
sw.Close();
fsOut.Close();
}
catch (Exception)
{
Console.WriteLine("Error raised writing to the file");
}
}

The method above works fine. The second method below is what is causing the error message below:

public string PblReadFile()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
while (sr.Peek() > -1)
{
lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
return sr.ReadLine(); 
}
sr.Close();
fsIn.Close();
}

I am not sure really what is wrong can anyone suggest any ideas? Thanks.

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

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()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
//while (sr.Peek() > -1)
//{
//lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
//}
return sr.ReadLine();
sr.Close();
fsIn.Close();
}

This compiles fine but does not do anything. Can you provide a bit more clarification please.

Member Avatar for iamthwee

http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx

using System;
using System.IO;


    class TextFileReader
    {
        static void Main(string[] args)
        {
            // create reader & open file
            TextReader tr = new StreamReader("C:\\data2.txt");

            // read a line of text
            Console.WriteLine(tr.ReadLine());

            // close the stream
            tr.Close();
        }
    }

swap the Console.WriteLine for lbLines.Items.Add I guess

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()
{
FileStream fsIn = File.OpenRead("C:\\data2.txt");
StreamReader sr = new StreamReader (fsIn);
//And read the data
//while (sr.Peek() > -1)
//{
//lbLines.Items.Add(sr.ReadLine());
//PblReadFile() = sr.ReadLine();
//}
return sr.ReadLine();
sr.Close();
fsIn.Close();
}

This compiles fine but does not do anything. Can you provide a bit more clarification please.

No, the while loop is an essential part to make you possible to read all the lines of the text files using StreamReader.ReadLine().

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

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
    {
        static void Main(string[] args)
        {
           using( System.IO.FileStream fsOut = System.IO.File.Create("c:\\TestText.txt"))
            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fsOut))
            {
                string[] sStuff =  {"Some Text" , "Test", "Bravo"};
                for (int i = sStuff.GetLowerBound(0); i <= sStuff.GetUpperBound(0); i++)
                {
                    sw.WriteLine(sStuff[i]);
                 }
                 sw.Flush();

            }

             using (System.IO.StreamReader sr = new System.IO.StreamReader(new System.IO.FileStream("c:\\TestText.txt", System.IO.FileMode.Open)))
                     
            //using( System.IO.StreamReader sr = new System.IO.StreamReader(fsRead))
            {
               while (sr.Peek() > -1)
                {
                  Console.WriteLine( sr.ReadLine());
                }
            }

            Console.ReadLine();
                        

        }
    }

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 return sr.ReadLine(); and it populated the listbox just fine.

Jerry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.