473,396 Members | 2,011 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,396 software developers and data experts.

Multiple String Switch Statement

233 100+
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 1976
Mr Gray
47
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 100+
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 Expert 2GB
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 100+
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 Expert 2GB
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.Parameters.AddWithValue("@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 100+
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 Expert 2GB
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 100+
This would be a lot simpler if I could just insert Null parameters.
Apr 11 '08 #9
mcfly1204
233 100+
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
Curtis Rutland
3,256 Expert 2GB
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.
Not quite. The way string.Format works is that it evaluates "{#}" as one of the parameters following. So:
Expand|Select|Wrap|Line Numbers
  1. string temp = "Bob";
  2. string out = String.Format("{0} is his name",temp);
  3. //out == "Bob is his name"
  4.  
What I did in that loop:
lets say I have a sql statement with 4 possible params: nameFirst, nameLast, nameMiddle, and userName. What I might do is:
Expand|Select|Wrap|Line Numbers
  1. string[] paramArray = new string[4];
  2. paramArray[0] = "nameFirst";
  3. paramArray[1] = "nameLast";
  4. paramArray[2] = "nameMiddle";
  5. paramArray[3] = "userName";
  6.  
Then, I would get my value strings like you did, and assign them to stringsArray. Then run them through the switch statement and so forth.

Then you could build your sql statement.
Expand|Select|Wrap|Line Numbers
  1. string sql = "select * from tableName ";
  2. bool hasParams = false;
  3. for(int i=0; i<paramArray.Length; i++)
  4. {
  5.   if(stringsArray[i] != null)
  6.   {
  7.     hasParams = true;
  8.     break;
  9.   }
  10. }
  11. if(hasParams)
  12. {
  13.   sql += " where ";
  14.   for(int i=0; i<stringsArray.Length; i++)
  15.   {
  16.     if(stringsArray[i] != null)
  17.       sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
  18.   }
  19.   sql = sql.Substring(0,sql.Length - 4);
  20. }
  21.  
  22.  
What I did there is check to see if any of the strings aren't null (as per the way you were setting them null in the switch.) Then, if there are any "where" parameters, we finish building the sql statement. What that String.Format would evaluate to is "paramName = 'paramValue' AND "
So, in practice, if nameFirst should have a value of Bob, it will evaluate to:
"nameFirst = 'Bob' AND "
And at the end, we remove the extra "AND " (4 chars off the end.) You have a complete sql statement.

Maybe not the most simple way of doing it, but it works.
Apr 14 '08 #11
mcfly1204
233 100+
So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:


INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**

cmd.Parameters.AddWithValue("@parameter1", value1);
cmd.Parameters.AddWithValue("@parameter2", value2);
***

Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.
Apr 14 '08 #12
Curtis Rutland
3,256 Expert 2GB
So, I see what you are doing in your example by adding a where clause, if a known value exists, but I still do not see how this will work with an insert statement. For example:


INSERT INTO [table] (column1, column2*
VALUES (@parameter1, @parameter2**

cmd.Parameters.AddWithValue("@parameter1", value1);
cmd.Parameters.AddWithValue("@parameter2", value2);
***

Value1 and value2 are known not to be null, but any value after that will need to be checked for nulls and if not null, added to the sql statement. Given I will potentially be adding to the insert statement at *, **, ***, I do not understand how this will work.
Ok, I was working on the incorrect assumption on a select statement. What you want to do is perfectly possible with System.Data.SqlTypes.

Expand|Select|Wrap|Line Numbers
  1. SqlCommand cmd = new SqlCommand(sqlInsertText,connectionObject);
  2. SqlParameter parameter = new SqlParameter("@param1", System.Data.SqlTypes.SqlString.Null);
  3. cmd.Parameters.Add(parameter);
  4.  
That will insert a null value for that param. Sorry for all the confusion :(

Now if this isn't a varchar or nvarchar field you might have to use System.Data.SqlTypes.whateverDbTypeYouNeed, but this works for an nvarchar.
Apr 14 '08 #13
mcfly1204
233 100+
I suppose I should have been more specific... but I think this may have something to do with why I was having a difficult time grasping this. I appreciate the effort nonetheless.
Apr 15 '08 #14

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

Similar topics

2
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...
1
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="". ...
4
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...
4
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
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...
13
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...
0
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...
10
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...
16
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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,...
0
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.