Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
C# syntax question: switch and IndexOf
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 1.1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01010821
Message ID:
01010830
Views:
20
This message has been marked as a message which has helped to the initial question of the thread.
>Thorny C# commands and syntax problem I encountered today while working on a reall app at the same time I teach myself C# and .Net:
>
>Situation:
>
>I am looking on a folder for delimited files that will be received daily. These I will parse and process.
>There are 11 types of files with 11 slightly different file structures. These go to a SQL Server database for later reporting (11 different reports) based on this data. So far it's an easy proposition and a good simple app to teach myself some .NET
>
>The only way to tell the 11 different files from each other is the file name. File names are very similar (I don't have any choice or control as they come from a mainframe).
>
>Some sample filenames:
>"Caribbean Generic MCC C&S Merch2005-03-06 22.35.15.250.txt"
>"Central Am Missing & Invalid C&S Merch2005-03-05 04.12.09.578.txt"
>"Mexico Illogical Auth Rule B&C2005-03-07 22.29.21.953.txt"
>"Mexico Illogical C&S Rule A&B2005-04-07 22.11.54.765.txt"
>
>The country or region is ignored as it is included in a data field inside the file. Date and time is also ignored as it does not tell me the type of file.
>
>I divided all 11 possible names into:
>"Missing & Invalid C&S Merch"
>"Illogical Auth Rule B&C"
>"Illogical C&S Rule A&B"
>etc.
>
>and established a number for each in code (01-11)
>
>I search with IndexOf and see if the first type is found. So far so good. Now I'd have to test against all the other ones until found. Not cool.
>
>
>Question:
>
>I'd like a switch construct to determine which of the 01 to 11 types it is, so I can direct to the method that processes the proper data file structure. But 'switch 'in C# can't do it. In VFP 'DO CASE' I can test each case to see if filename matches one of the 11 patterns. In C# 'switch' the test is done only once.
>
>Anybody has a good idea of how to approach the problem? Maybe creating an Enum for the 11 types? Some complicated RegEx? I'd like to avoid using RegEx (too cryptic) if possible.
>
>
>In short:
>
>All I need to know, in a fast and graceful way, is test each actual filename against the 11 possible cases and find which one it matches.
>
>TIA,

Alex,

While I'm sure someone can come up with a way to do this with a switch construct, you may just want to use if/else which can function much more like VFP's DO CASE:
if (check for match 1)
{
  routine 1 or call routine 1 here;
}
else if (check for match 2)
{
  routine 2 or call routine 2 here;
}
...
else if (check for match 11)
{
  routine 11 or call routine 11 here;
}
else
{
  invalid file type encountered;
}
And you could also have a method that takes an int or string that coincides with the file types and use the switch construct there and just call that method from the above code.

HTH,
Chad
_________________________________
There are 2 types of people in the world:
    Those who need closure
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform