|
-
Sep 8th, 2003, 09:35 AM
#1
Thread Starter
Hyperactive Member
Function to ensure variables have same value or null
Given 3 variables [var1, var2, var3], which represent the version [i.e.: 1, 2, 3, etc.] of each station.
I need a method/function to ensure that the version is the same in all stations that are reporting. So if the value [varX] is blank/null then do not count it in the comparisons, otherwise ensure all filled variables are the same.
I am looking for a way to check this without going threw case/select statements [first thing that comes to mind] for each possible case [when a variable is null].
Of course in reality there are 22 variables, which explains why I cannot do the trivial Case/Select for all possibilities of null variables.
Anyone have a better idea on how to code the pseudo-code above in VC#?
-
Sep 8th, 2003, 10:00 AM
#2
Frenzied Member
If there are 22 variables why not just use an array? And pass that array to a method and check if null or "" exists in the array subscripts.
Being educated does not make you intelligent.
Need a weekend getaway??? Come Visit
-
Sep 8th, 2003, 10:39 AM
#3
Thread Starter
Hyperactive Member
How would that help?
The goal is to confirm (boolean) if all reporting values are the same, I already have the 22 variables [i.e.: they cannot be replaced by an array].
So you suggest I populate an Array with the results from my 22 variable. Then how do I perform my checking function?
I need to ensure that all 22 Reporting variables are the same, if they are not reporting then they would be null.
-
Sep 9th, 2003, 01:22 AM
#4
yay gay
if (var1 & var2 & var3 & ...... & var22 == true) {
TODO if TRUTH
} else
{
TODO if ELSE
}
\m/  \m/
-
Sep 9th, 2003, 07:50 AM
#5
PowerPoster
Try this:
Code:
static void Main(string[] args)
{
string var1 = "1.1", var2 = null, var3 = "1.1", var4 = "";
if (EnsureStations(new string[] {var1, var2, var3, var4}))
Console.WriteLine("All stations are in sync");
else
Console.WriteLine("All stations are not in sync");
}
static bool EnsureStations(string[] versions) {
string currentVersion = null;
foreach (string v in versions) {
if (v != null && v != String.Empty) {
if (currentVersion != null) {
if (v != currentVersion)
return false;
}
else {
currentVersion = v;
}
}
}
return true;
}
Last edited by Lethal; Sep 9th, 2003 at 08:13 AM.
-
Sep 9th, 2003, 08:40 AM
#6
Thread Starter
Hyperactive Member
PT Exorcist:
That would not work, first of all the variables are int or strings, not boolean. And what about if one was null?
Lethal:
Great function, problem is I already have the 22 variables [as variables, not an array].
So is my best bet to populate an array from the string variables that already exists or is there a quicker way without the steps and memory required to create the temporary array?
-
Sep 9th, 2003, 08:43 AM
#7
PowerPoster
In my example, I am not storing my values in an array, just your typical variables. However, when I invoke the method, i create an array on the fly, which should be exactly what you need. This way, your code is much more maintainable, since you dont have a method receiving 20+ paramaters, not to mention, if you need to add another station in the future, you would not have to break your interface.
Last edited by Lethal; Sep 9th, 2003 at 09:14 AM.
-
Sep 9th, 2003, 02:18 PM
#8
Lethal's method is good -- you could go one step further and have the function create the array for you by creating a function like:
Code:
bool IsGoodData(params string[] myStrings)
{
for(int i = 0; i < myStrings.Length; i++)
if (myStrings[i] == null) /* or whatever */ return false;
return true;
}
if (IsGoodData(string1, string2, somestring, strX)) ...
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
-
Sep 9th, 2003, 02:27 PM
#9
PowerPoster
Originally posted by sunburnt
Lethal's method is good -- you could go one step further and have the function create the array for you by creating a function like:
Code:
bool IsGoodData(params string[] myStrings)
{
for(int i = 0; i < myStrings.Length; i++)
if (myStrings[i] == null) /* or whatever */ return false;
return true;
}
if (IsGoodData(string1, string2, somestring, strX)) ...
Yeah, I thought about doing it that way, but figured either way will work fine.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|