Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

Switch/Case statement

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Cook - 20 Aug 2007 20:10 GMT
New programmer lost!!!

I need some help with a switch/Case statement;

I need to use OpenFileDialog to open a file,

If the file extension is .dat, then do x.
If the file extension is .rtf, then do y.
if the file extension is .txt, then do z.

Thanks,

Brian
Nicholas Paldino [.NET/C# MVP] - 20 Aug 2007 20:21 GMT
Brian,

   If you have the full path to the file, then you can call the static
GetExtension method on the Path class in the System.IO namespace to get the
extension.  Then, you can call ToLower on it and switch on that:

// The filename.
string filename = ...;

// The extension.
string extension = Path.GetExtension(filename).ToLower();

// Switch on the extension.
switch (extension
{
   case ".dat":
       // Do Something
       break;

   case ".rtf":
       // Do something else.
       break;

}

   You might want to put a "default" statement as well in there to handle
cases where extension is not any of the ones you specify.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> New programmer lost!!!
>
[quoted text clipped - 9 lines]
>
> Brian
Brian Cook - 20 Aug 2007 20:38 GMT
Nicholas, the default file would have an extension of .DAT, and would be the
routine I want to run first.

The program is an RichText editor that opens and formats the DAT file
normally, and can also open;

RTF, TXT, LOG files as needed.

The name of the DAT always begins with OCG, the rest would be dependent on
what ever date and hour is current.

So the bottom line is that the name is not static.(rambled didn't I).

Thanks,

> Brian,
>
[quoted text clipped - 37 lines]
> >
> > Brian
Nicholas Paldino [.NET/C# MVP] - 20 Aug 2007 20:47 GMT
It doesn't matter if the filename is not static, you stated your logic
is to be dependent on the extension, which is what the code I have supplied
does.  It only works on the extension.  There is no first, second, or third
order of processing.  It chooses one course of action based on the
extension.

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Nicholas, the default file would have an extension of .DAT, and would be
> the
[quoted text clipped - 55 lines]
>> >
>> > Brian
Brian Cook - 20 Aug 2007 21:16 GMT
Nicholas, this is what I have and it does work. How would I make one of them
"default"?

Thanks,
      private void OpenFile()
       {
           try
           {
               if (OpenFileDialog.ShowDialog() == DialogResult.OK)
               {
                   if (OpenFileDialog.FileName == "")
                   {
                       return;
                   }
                   string strExt;
                   strExt =
System.IO.Path.GetExtension(OpenFileDialog.FileName);
                   strExt = strExt.ToLower();
                   //if (strExt == ".rtf")
                       switch (strExt)
                       {
                           case ".rtf":
                               System.IO.StreamReader txtReader;
                               txtReader = new
System.IO.StreamReader(OpenFileDialog.FileName);
                               rtbDoc.Text = txtReader.ReadToEnd();
                               txtReader.Close();
                               txtReader = null;
                               rtbDoc.SelectionStart = 0;
                               rtbDoc.SelectionLength = 0;
                               break;
                           case ".dat":
                               rtbDoc.Text =
Streamer.LayoutInput(OpenFileDialog.FileName);
                               break;
                           case ".txt":
                               System.IO.StreamReader txtReader;
                               txtReader = new
System.IO.StreamReader(OpenFileDialog.FileName);
                               rtbDoc.Text = txtReader.ReadToEnd();
                               txtReader.Close();
                               txtReader = null;
                               rtbDoc.SelectionStart = 0;
                               rtbDoc.SelectionLength = 0;
                               break;
                       }
                   currentFile = OpenFileDialog.FileName;
                   rtbDoc.Modified = false;
                   this.Text = "Train Control Editor: " +
currentFile.ToString();
               }
               else
               {
                   MessageBox.Show("Open File request cancelled by user.",
"Cancelled");
               }
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message.ToString(), "Error");
           }
       }

>     It doesn't matter if the filename is not static, you stated your logic
> is to be dependent on the extension, which is what the code I have supplied
[quoted text clipped - 61 lines]
> >> >
> >> > Brian
Nicholas Paldino [.NET/C# MVP] - 20 Aug 2007 21:35 GMT
Brian,

   You can use the default case, like so:

switch (<expression>)
{
   case <case expression>:
       // Do something.
       break;

   default:
       // Do things here in the event none of the case expressions match.
       break;
}

Signature

         - Nicholas Paldino [.NET/C# MVP]
         - mvp@spam.guard.caspershouse.com

> Nicholas, this is what I have and it does work. How would I make one of
> them
[quoted text clipped - 132 lines]
>> >> >
>> >> > Brian
Brian Cook - 20 Aug 2007 21:38 GMT
Boy was that simple!

I guess I am over-complicating things when I don't know them.

Thanks Nicholas.

> Brian,
>
[quoted text clipped - 147 lines]
> >> >> >
> >> >> > Brian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.