views:

115

answers:

4
+2  Q: 

leave if statement

I have an if statement inside an if statement.

If the condition in the second if statement returns false, I want to go to the first else because there it sets my validation controls automatically.

I hope you understand

if (page.isvalid() )
{
    if (datetime.tryparse (date) == true)
    {
        // ok    
    } 
    else
    {
       //go to the other else
    }
}
else
{
    // want to go here
}

Edit:

Important is that I have to first validate the page because after the validation, I know I can parse the datetimes from the 2 input controls and check if the second one is greater than the first one. Otherwise it throws an exception, maybe, if the date is not valid.

+9  A: 

instead of DateTime.Parse(date) use

DateTime dt;
bool isParsed = DateTime.TryParse(date, out dt);

//if ( page.isvalid() && (datetime.parse (date) == true) )   
if ( page.isvalid() && isParsed )   
{        
     // ok        
}     
else    
{       
    // want to go here
}
Saar
this is a good boolean check, but it would fall to the //want to go here section when page is valid and datetime doesn't parse, which doesn't seem right.
Jim Schubert
It might not seem "right", but it is exactly what the question asker is asking for.
Anon.
@Jim - this is what the OP asked for!
Dexter
I use tryparse. just wrote to fast here in sackoverflow. and yes thats the problem what jim schubert told.
snarebold
I edited my question. see below.
snarebold
This calls DateTime.TryParse (aka datetime.tryparse()?) even when page.isvalid() fails. Why not just call the parser when the page is known to be valid?
Jonathan Leffler
@Jonathan: http://stackoverflow.com/questions/1039465/should-i-always-call-page-isvalid-in-asp-net-webforms-c
Saar
thank you all for your interest to help
snarebold
+1  A: 

Take out the elses, and it should be what you're looking for. Also, add a return statement after everything is good to go.

if ( page.isvalid() )
{
    if (datetime.parse (date) == true)
    {
        // ok    
    } 
    return;
}

// code here happens when it's not valid.
Jim Schubert
this works with little adapts. hmm seems not to be so proper but ... no other complete solutions
snarebold
what do you mean by "proper"? this is basic flow control. Remember, in .NET languages, void methods implicitly return when the last `}` is met. Exiting a method at any point is completely valid. Depending on your context, you may need to flip the logic around to say `if(!Page.IsValid){ /* invalid code block */ } else { /* valid code */ }`. Another option would be to refactor the 'valid' code and 'invalid' code into methods and say `if(Page.IsValid()) { ValidMethod(): } else { InValidMethod();}`
Jim Schubert
+1  A: 

This does exactly what you want, I believe.

if (page.isvalid() && datetime.tryparse(date) == true)
{        
    // ok
}
else
{
    // want to go here
}

It is not clear whether the '== true' is necessary; you might be able to drop that condition.

Jonathan Leffler
A: 

Exactly what you want to do is impossible. There are two options. One is to determine both answers at the top of your if clause. This is what the other posters are telling you to do. Another option would be something like this:

bool isvalid = true;
if ( page.isvalid() )
{    
    if (datetime.tryparse (date) == true)    
    {        
        // ok        
    }
    else    
    {       
        isvalid = false;    
    }
}
else
{
    isvalid = false;
}
if (isvalid == false)
{
    //do whatever error handling you want
}

This extracts the error handling code from your else clause and puts it somewhere both code paths can reach.

Steve Rowe
Very verbose - accurate, but very verbose.
Jonathan Leffler