Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I would like to know what to put in the if statement brackets to tell the program, if x or y equals a double, it can break out and continue carrying out the rest of my code.

Any suggestions?

while (true)
{                    
    Console.Write("I need to pour this much from this one: ");

    string thisOne = Console.ReadLine();
    Double.TryParse(thisOne, out x);

    if ( /* Here I want to put "x is a number/double*/ )
    {
        break;
    }

}

while (true)
{
    Console.Write("I need to pour this much from that one: ");

    string thatOne = Console.ReadLine();
    Double.TryParse(thatOne, out y);

    if (/* Here I want to put "y is a number/double*/)
    {
        break;
    }
}
share|improve this question
2  
My suggestion is to read the documentation for Double.TryParse. Hint: It returns a boolean. – D Stanley Jul 23 '14 at 12:27
up vote 4 down vote accepted

TryParse returns a boolean to say whether the parse was successful

if (Double.TryParse(thatOne, out y))
{
    break;
}

From documentation

A return value indicates whether the conversion succeeded or failed.

share|improve this answer

Double.TryParse returns a boolean, perfect fit for your if statement

if (Double.TryParse(thatOne, out y)) {
    break;
}
share|improve this answer

You have a misconception about TryParse(). You want to check if x is a double. somewhere above in your code, you did not post it here there is probably a line like double x = 0;. You defined x and y already as double. You want to check if your input which is string can be parsed to double:

The shorthand version is this:

if (Double.TryParse(thatOne, out x))
{
    break;
}

This can also be written as:

bool isThisOneDouble = Double.TryParse(thisOne, out x);

if (isThisOneDouble)
{
    break;
}

If you really want to check if a variable is of certain type without trying to parse it, try it like this:

double x = 3;
bool isXdouble = x.GetType() == typeof(double); 

or

double x = 3;
if(x.GetType() == typeof(double)) 
{
   // do something
}
share|improve this answer

According to the documentation, TryParse returns true if parsing succeeded so just put your tryparse into your if statement.

share|improve this answer

Control your loop with a bool, set the bool false when your condition is met...

bool running = true;
while (running)
{                    
    Console.Write("I need to pour this much from this one: ");

    string thisOne = Console.ReadLine();

    if (Double.TryParse(thisOne, out y))
    {
         running = false
    }
}
share|improve this answer
2  
Seems like an unnecessary step? This also requires the loop to perform a full iteration, the OP might want to break out early. – James Jul 23 '14 at 12:28
2  
It also doesn't answer the question of what goes into the if statement. – Sayse Jul 23 '14 at 12:29
    
Just edited to include, missed that from the original question. The last step is a break so not adding extra execution. – kidshaw Jul 23 '14 at 12:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.