Click here to Skip to main content
15,889,840 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a method that checks is the object properties fulfill some requirements, it returns true if yes, false otherwise. But the function is returning always false even if the parameters are correct. Here is the original code and a workaround i made and that did the job, but i still don't know the origin of the problem, so i posted it here to see if someone can understand the problem.

The Class contains an ArrayList of Class SacVariable, which also has a method isValid(). If all SacVariable objects in the arrayList are valid then the big guy is valid too. During testing the SacVariable.isValid() works perfectly returning true or false according to the case. But the if inside the for loop always read false...


Java
// Original code (does not work because always returns false)
public boolean isValid()
    {
        if (!this.getList().isEmpty())
        {
            for (SacVariable var : this.getList())
            {
                if (!var.isValid())
                {
                    return false;
                }
            }
            return true;        
        }
        return false;
    }

I think the logic is clear but i will explain it for clarity's sake.
If the arrayList is empty (size==0) i do nothing and return false straight away.
if there is something in the arrayList, i check each one to see if they are valid, if at least 1 is false then i immediately return false. if the for loops ends then i return true because it means all objects SacVariable are valid.



Java
// Workaround that does the job...
public boolean isValid()
    {
        int validConditions = 0;
        if (this.getList().isEmpty())
        {
            return false;
        }

        for (SacVariable var : this.getList())
        {
            if (var.isValid())
            {
                validConditions++;
            }
        }

        if (validConditions == this.getList().size())
        {
            return true;
        }

        return false;
    }


The work around is the same, i initially separated the conditions to improve legibility in hope of finding the problem, that failed so i decided to add a counter, if validConditions==this.getList().size() then i know all SacVariables are valid. It worked... why it didn't the other way? it may be something incredible small or trivial but for the life of me i can't see it.

SacVariable.isValid() does not return always false. That i checked. The mystery here is that despite the fact that the method is indeed returning true the loop is broken.

I was not asking for people to give me alternative code, I only wanted to show this method to everyone here to see if someone spotted a problem with the code.
Posted
Updated 7-Sep-11 13:12pm
v2
Comments
Firo Atrum Ventus 5-Sep-11 21:19pm    
Interesting, is that method placed inside SacVariable class?
Renzo Ciafardone 5-Sep-11 21:46pm    
No, there are 2 classes at play here, both have a isValid() method. The big class conteins an arrayList of SacVariable. And its isValid() is the one i posted. The isValid() of SacVariables just makes sure that its own properties are initialized with valid values.
Firo Atrum Ventus 7-Sep-11 22:05pm    
I'm sorry if this is a silly question, but are you sure that SacVariable.isValid returns boolean?
TorstenH. 9-Sep-11 3:55am    
This is no silly question - but if you make it an answer you'll probably also get a downvote as I did (solution 2) rather than an answer to your implicated question.
Renzo Ciafardone 9-Sep-11 22:08pm    
Yes. IsValid() is indeed a boolean.

I would think that this should work:
Java
public boolean isValid() {
    if (this.getList().isEmpty()) {
        // There are no items in the list
        return false;
    }

    for (SacVariable var : this.getList()) {
        if (!var.isValid()) {
            // this item is invalid ergo the whole list is invalid
            return false;
        }
    }

    // every item in the list is valid
    return true;
}


If it doesn't then try this:
Java
public boolean isValid() {
    if (this.getList().isEmpty()) {
        // There are no items in the list
        return false;
    }

    // assume every item in the list is valid
    boolean valid = true;
    for (SacVariable var : this.getList()) {
        if (!var.isValid()) {
            // this item is invalid ergo the whole list is invalid
            valid = false;
            break;
        }
    }
    
    return valid;
}


You shouldn't need to continue in the loop once you have found an invalid item.
 
Share this answer
 
To me this sounds like SacVariable.isValid() always returns false.

Check that one.
 
Share this answer
 
Comments
TorstenH. 8-Sep-11 1:39am    
WTF? At least write a comment if you downvote!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900