Searching part of a file in C#

RSanders

New member
Joined
Dec 30, 2003
Messages
4
Location
Ohio, USA
Ok im a complete newbie at C#, bascially I put this together with as I thought careful planning.

I am wanting to search a text file line by line for "xxx" To me this looks to be very straight forward but the compiler does not like it =/

Its giving me 2 Errors:
D:\MyDocs\Visual Studio Projects\ConsoleApplication2\Class1.cs(25): Control cannot fall through from one case label (case 1:) to another

D:\MyDocs\Visual Studio Projects\ConsoleApplication2\Class1.cs(25): Control cannot fall through from one case label (default:) to another

I am guessing, that it does not like the dynamic generation of the "to search" string, or its not public or something, like i said im a total newbie =/

If anyone could give me some pointers as to my solution that would be great, not looking for someone to write my code.

Below is my code
C#:
using System;

namespace ConsoleApplication2
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			int counter = 0;
			string line;

			// Read the file and display it line by line.
			System.IO.StreamReader file = 
				new System.IO.StreamReader("c:\\test.txt");
			while((line = file.ReadLine()) != null)
			{
				string searchThis = "Bill";
				switch(line.IndexOf(searchThis))
				{
					case 1: 
						Console.WriteLine (line);
					default:
						Console.WriteLine ("Nothing found");
				}
				counter++;
			}

			file.Close();

			// Suspend the screen.
			Console.ReadLine();	
		}
	}
}
Thanks
Rich
 
Why are you using a switch?
C#:
if(line.IndexOf(searchThis) > -1)
      Console.WriteLine(line);
else
      Console.WriteLine("Nothing Found");
You may also want to put "string searchThis = Bill" before the loop, you dont need to continually set the value to "Bill" as far as I can see.
As for whats wrong with the switch, youre missing the "break" keywords:
C#:
switch(line.IndexOf(searchThis))
                {
                    case 1: 
                        Console.WriteLine (line);
                        break;
                    default:
                        Console.WriteLine ("Nothing found");
                        break;
                }
 
Last edited by a moderator:
Yea I tried that approach as well and its telling me I cant convert a int to bol, i understand that point.

But thanks for the reply, all keep hacking away at it.

BTW: Thanks for pointing out that pt about the varible
 
Back
Top