Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I would like to ask that I want to set cases within a switch by reading statements from a file, for example this is the code I have currently:
C#
if (cause == "VPN CONDITION IS CRITICAL")
                        {
                            insert(name, ip, "0", severity, nativ, time, date);
                            insert_history(name, ip, "0", severity, nativ, time, date);
                        }
                        if (cause == "Device is down/unreachable")
                        {
                            insert(name, ip, "0", severity, nativ, time, date);
                            insert_history(name, ip, "0", severity, nativ, time, date);
                        }
                        if (cause == "DEVICE HAS STOPPED RESPONDING TO POLLS")
                        {
                            insert(name, ip, "0", severity, nativ, time, date);
                            insert_history(name, ip, "0", severity, nativ, time, date);
                        }
                        if (cause == "BAD LINK DETECTED")
                        {
                            insert(name, ip, "0", severity, nativ, time, date);
                            insert_history(name, ip, "0", severity, nativ, time, date);
                        }
                        if (cause == "WIDE-AREA CONNNECTION LOST")
                        {
                            insert(name, ip, "0", severity, nativ, time, date);
                            insert_history(name, ip, "0", severity, nativ, time, date);
                        }

I just want to know how set the checks with values read from a file. Any help would be highly apreciated.
Posted

It seems it doesn't matter what the value of cause is. Just always call the insert and insert_history.

Good luck!
 
Share this answer
 
Comments
StianSandberg 23-Apr-13 6:28am    
good point!
If you mean the equivalent of:
C#
string[] causes = File.ReadAllLInes(@"D:\Temp\Causes.txt");
switch (cause)
   {
   case causes[0]:...
   case causes[1]:...
   ...
   }
You can't - you can only use constant values in case statments, never dynamic ones (because the values have to be fully known and resolved as compile time).

To do anything other than the if..else if...else you are effectively using in your example, you would probably be better off populating a Dictionary<string, delegate> and using that instead.


You can - but...a question if you don't mind.
Are you trying to do the same thing if the "cause" string is any one of the strings in the file?
Because if so, there is an easy way.
Reply
09hadi - 11 mins ago
exactly



Then do this:
C#
string[] causes = File.ReadAllLInes(@"D:\Temp\Causes.txt");

if (causes.Contains(cause))
    {
    insert(name, ip, "0", severity, nativ, time, date);
    insert_history(name, ip, "0", severity, nativ, time, date);
    }
if any line in the file contains the string in "cause" it will match it. (This is a Linq method, which works with any IEnumerable class)
You may want to move the File.ReadAllLines to load a static array, or to do it in a constructor rather than doing it each time you check.
 
Share this answer
 
v2
Comments
09hadi 22-Apr-13 7:29am    
so cant we populate it in if statements?
OriginalGriff 22-Apr-13 8:22am    
You can - but...a question if you don't mind.
Are you trying to do the same thing if the "cause" string is any one of the strings in the file?
Because if so, there is an easy way.
09hadi 23-Apr-13 6:09am    
exactly
OriginalGriff 23-Apr-13 6:26am    
Answer updated

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900