473,568 Members | 2,923 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Multiple String Switch Statement

233 New Member
I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined" , that I would like to set to Null. My first thought was to create an array and then use a foreach statement to run each string through the switch statement. The obvious problem here is that you cannot assign to the iteration variables. How can I get around this?
Apr 11 '08 #1
13 1981
Mr Gray
47 New Member
You could make a collection of those string values and iterate through the collection and assign to each of them.
Apr 11 '08 #2
mcfly1204
233 New Member
You could make a collection of those string values and iterate through the collection and assign to each of them.
I suppose I do not understand how that solves the problem of assigning to the strings.
Apr 11 '08 #3
Curtis Rutland
3,256 Recognized Expert Specialist
I have numerous strings that I would like to run though a switch statement. These strings are terms such as "None" or "Undefined" , that I would like to set to Null. My first thought was to create an array and then use a foreach statement to run each string through the switch statement. The obvious problem here is that you cannot assign to the iteration variables. How can I get around this?
Create a list/array but use a for loop instead of a foreach. Then you will be able to assign values to your strings.

Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<stringsArray.Length; i++)
  2. {
  3.   switch (stringsArray[i])
  4.   {
  5.     case "None":
  6.       stringsArray[i] = null;
  7.       break;
  8.   }
  9. }
Apr 11 '08 #4
mcfly1204
233 New Member
Thank you for that, but I now have a new issue. Now that I have eliminated unwanted strings and set them to null, I now need an if statement to not add the parameter if the value is null. Such as:
Expand|Select|Wrap|Line Numbers
  1. If (strValue1 != null)
  2. {
  3.      OleDBCommand.Parameters.AddWithValue("@value1", strValue1);
  4. }
  5. else
  6. {
  7.      *code to move on to next parameter to add*
  8. }
If the value is null, how would I proceed to the next parameter to add?
Apr 11 '08 #5
Curtis Rutland
3,256 Recognized Expert Specialist
Thank you for that, but I now have a new issue. Now that I have eliminated unwanted strings and set them to null, I now need an if statement to not add the parameter if the value is null. Such as:

If (strValue1 != null)
{
OleDBCommand.Pa rameters.AddWit hValue("@value1 ", strValue1);
}
else
{
*code to move on to next parameter to add*
}

If the value is null, how would I proceed to the next parameter to add?
Just use the same string array and iterate through it again. Also, set up an array with your parameter names.
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < stringsArray.Length; i++)
  2. {
  3.   if (stringsArray[i] != null)
  4.     cmd.Parameters.AddWithValue(paramArray[i], stringsArray[i]);
  5. }
  6.  
Where cmd is your OleDbCommand.
Apr 11 '08 #6
mcfly1204
233 New Member
Just use the same string array and iterate through it again. Also, set up an array with your parameter names.
Expand|Select|Wrap|Line Numbers
  1. for (int i = 0; i < stringsArray.Length; i++)
  2. {
  3.   if (stringsArray[i] != null)
  4.     cmd.Parameters.AddWithValue(paramArray[i], stringsArray[i]);
  5. }
  6.  
Where cmd is your OleDbCommand.
So instead of adding the parameters, I would take all parameters in question, add them to an array, and then call something such as what you have above and dynamically add those parameters based of the string value? I just would not have thought to add parameters for a query in one method from another parameter, but I suppose if they're in the same class, it shouldn't matter.
Apr 11 '08 #7
Curtis Rutland
3,256 Recognized Expert Specialist
So instead of adding the parameters, I would take all parameters in question, add them to an array, and then call something such as what you have above and dynamically add those parameters based of the string value? I just would not have thought to add parameters for a query in one method from another parameter, but I suppose if they're in the same class, it shouldn't matter.
Well, that might not work, now that I think about it. I think that you have to add something for each parameter. I don't usually use parameters. I usually dynamically build my sql statement. So what you could do is write your sql statement up to the "where" clause. Then check to see if you have any valid parameters. If you do, add "where " and then iterate through your array of parameter values. If it isn't null, then use
Expand|Select|Wrap|Line Numbers
  1. sql += String.Format("value1 = '{0}' and" ,stringsArray[i]);
to the end of your sql statement (assuming you have your sql statement stored in a string called sql. Once you are done, remove the last 4 chars from the string (the last "and ") and you have a full sql statement.

Also, instead of "value1" you could have another string[] paramArray. This could hold all the names of your parameters. So then the statement would be:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<stringsArray.Length; i++)
  2. {
  3.   if(stringsArray[i] != null)
  4.     sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
  5. }
  6.  
Just remember to remove the last "AND ".
If you don't know what the String.Format() does, look it up. It is very useful.
Apr 11 '08 #8
mcfly1204
233 New Member
This would be a lot simpler if I could just insert Null parameters.
Apr 11 '08 #9
mcfly1204
233 New Member
Also, instead of "value1" you could have another string[] paramArray. This could hold all the names of your parameters. So then the statement would be:
Expand|Select|Wrap|Line Numbers
  1. for(int i=0; i<stringsArray.Length; i++)
  2. {
  3.   if(stringsArray[i] != null)
  4.     sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
  5. }
  6.  
Just remember to remove the last "AND ".
If you don't know what the String.Format() does, look it up. It is very useful.
So here you are formatting the first item in the array to equal the next item and then adding the string AND to the end I am inexperience both with building sql statements dynamically as well as not using parameters.
Apr 14 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
20360
by: Guadala Harry | last post by:
After RTFM, I hope I have missed something: I would like to have a switch() statement process multiple input values the same way - but without having multiple case: blocks for multiple input values that need to be processed the same way. For example, please consider the following sample switch block and notice that "other value 1, 2, and 3"...
1
1310
by: gordon | last post by:
Hi I am trying to set some elements to values depending on the selection in a test box. The first possible value that can be selected should set fmttest1= "", fmttest2=" to " and fmttest3="". The second value that can be selected from the test box should set the fmttest1 ="From ", fmttest2=" to ", fmttest3=" less than". here is what i...
4
4973
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if else statements but it looked pretty ugly and the string values to be tested is still increasing. The switch statement seems to be a better choice then...
4
71867
by: Kevin Blount | last post by:
I've been unsuccessful finding a site that tells me, so I'm asking here: can I have multiple 'clauses' per 'case' in a 'switch' statement? e.g. switch "myVar" { case "1", "a": break;
11
10844
by: Peter Kirk | last post by:
Hi i have a string variable which can take one of a set of many values. Depending on the value I need to take different (but related) actions. So I have a big if/else-if construct - but what is a better way of doing this? if ("control" == commandString) { } else if ("execute" == commandString)
13
8330
by: jsta43catrocks | last post by:
In C++, my "switch" statement is okay when I ask it do evaluate ONE expression. (My number that I'm evaluating is one of ten single digits; I have ten cases for the ten digits.) BUT, I have five separate digits to evaluate. I can't believe that the only solution is to do: switch (num1) { case 0:... case 9:
0
1741
by: steve.lorimer | last post by:
Thank you for taking the time to look at this: I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code below, I can take 5 characters and create a const uint64_t value at compile time. I can then create a switch statement that has these const values...
10
487
by: steve.lorimer | last post by:
I'm looking for a pre-processor command that will allow me to resolve const strings into const char literals at compile time. Looking at the code below, I can take 5 characters and create a const uint64_t value at compile time. I can then create a switch statement that has these const values as the various cases. Since each case resolves...
16
12926
by: Silent1Mezzo | last post by:
Is it possible to have multiple control variables in a switch statement? For example: switch(a , b) { case(a>b): case(b<a): case(a==b): }
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.