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?
13 1981
You could make a collection of those string values and iterate through the collection and assign to each of them.
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.
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. -
for(int i=0; i<stringsArray.Length; i++)
-
{
-
switch (stringsArray[i])
-
{
-
case "None":
-
stringsArray[i] = null;
-
break;
-
}
-
}
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?
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. -
for (int i = 0; i < stringsArray.Length; i++)
-
{
-
if (stringsArray[i] != null)
-
cmd.Parameters.AddWithValue(paramArray[i], stringsArray[i]);
-
}
-
Where cmd is your OleDbCommand.
Just use the same string array and iterate through it again. Also, set up an array with your parameter names. -
for (int i = 0; i < stringsArray.Length; i++)
-
{
-
if (stringsArray[i] != null)
-
cmd.Parameters.AddWithValue(paramArray[i], stringsArray[i]);
-
}
-
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.
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 - 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: -
for(int i=0; i<stringsArray.Length; i++)
-
{
-
if(stringsArray[i] != null)
-
sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
-
}
-
Just remember to remove the last "AND ".
If you don't know what the String.Format() does, look it up. It is very useful.
This would be a lot simpler if I could just insert Null parameters.
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: -
for(int i=0; i<stringsArray.Length; i++)
-
{
-
if(stringsArray[i] != null)
-
sql += String.Format("{0} = '{1}' AND ",paramArray[i], stringsArray[i]);
-
}
-
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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"...
|
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...
|
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...
|
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;
|
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)
| |
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:
|
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...
|
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...
|
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):
}
|
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. ...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |