Results 1 to 9 of 9

Thread: Function to ensure variables have same value or null

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308

    Question 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#?

  2. #2
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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.

  4. #4
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    if (var1 & var2 & var3 & ...... & var22 == true) {
    TODO if TRUTH
    } else
    {
    TODO if ELSE
    }
    \m/\m/

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2003
    Posts
    308
    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?

  7. #7
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  8. #8
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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.

  9. #9
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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
  •  



Click Here to Expand Forum to Full Width