473,386 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Switch/Case statement

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
Aug 20 '07 #1
2 2683
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Cook" <br********@bnsf.comwrote in message
news:F2**********************************@microsof t.com...
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


Aug 20 '07 #2
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,
"Nicholas Paldino [.NET/C# MVP]" wrote:
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Brian Cook" <br********@bnsf.comwrote in message
news:F2**********************************@microsof t.com...
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


Aug 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
10
by: clueless_google | last post by:
hello. i've been beating my head against a wall over this for too long. setting the variables 'z' or 'y' to differing numbers, the following 'if/else' code snippet works fine; however, the ...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
5
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions...
3
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. ...
14
by: Chris | last post by:
Hi, can you specify a range in a switch - statement ? switch (i) { case 100 - 999 : // do something break; case 1000 - 9999:
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
22
by: John | last post by:
Hi Folks, I'm experimenting a little with creating a custom CEdit control so that I can decide on what the user is allowed to type into the control. I started off only allowing floating point...
13
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.