new to sql server

I have written the following nested if to prevent someone from entering a value that won't work. I am trying to get the user to enter a value between 1 & 13. When I run this I get the following errors for both of the "if"s that use the comparison operators.

This is the error that VS 2005 express gave me. "Operator '<=' cannot be applied to types of string and int.".

I don't understand why I cant do this with an int. It seems like this datatype would be used frequently for cpmparisons.

It also seems like there would be a need to compare strings this way too.

I don't want to change the datatype. This is what the back end is using.

if (newBreedID == null)

{

Console.WriteLine("TRY AGAIN");

}

if (newBreedID <= 0)

{

Console.WriteLine("TRY AGAIN");

}

if (newBreedID > 13)

{

Console.WriteLine("TRY AGAIN");

}

else

{

Console.WriteLine("good job");

}

...............................................thanks for any suggestions



Re: Visual C# Language using or operator with strings and ints

FlorianReischl

It seems that your "newBreedID" is a string not an int. So convert it to int and go on:

Code Block

string newBreedID = "1";

if (newBreedID == null)

{

Console.WriteLine("TRY AGAIN");

}

else

{

int newBreedIDAsInt = int.Parse(newBreedID);

if (newBreedIDAsInt <= 0)

{

Console.WriteLine("TRY AGAIN");

}

else if (newBreedIDAsInt > 13)

{

Console.WriteLine("TRY AGAIN");

}

else

{

Console.WriteLine("good job");

}

}

Best Regards

Flo





Re: Visual C# Language using or operator with strings and ints

Asim Patnaik

Hi,

I think you are trying to read the newBreedID value from the console using Console.ReadLine(). Console.ReadLine() reads the input as a string. So as you are expecting an integer, you have to convert the input string into an integer using int.Parse() or Convert.ToInt32(). After the conversion you can do the necessary comparison.

Hope this helps.

Regards,

Asim.





Re: Visual C# Language using or operator with strings and ints

new to sql server

Thanks. I just tried the code and it fixed my problem.

thanks to both of you. I didn't know that about the way that a console app reads the input with Console.Readline.

Do either of you know the difference between a signed and unsigned integer





Re: Visual C# Language using or operator with strings and ints

new to sql server

Is there a way to do a validation for datatypes and not just values I am wondering if there is a way to check to see if a user entered the right datatype. For example, if you are looking for an int and the user enters text.

....................................................thanks again





Re: Visual C# Language using or operator with strings and ints

Asim Patnaik

Hi,

There is no way in which we can directly check the data type of an input. To check the correctness of the data type, you can try to convert the input into the necessary data type within a try...catch block. If there is no exception, then the data type is the one expected else the data type was different.

Hope this helps.

Regards,

Asim.





Re: Visual C# Language using or operator with strings and ints

Kent Boogaart

Using try/catch will work, but in this case you have good reason to believe you will often receive bad input (because it's coming from the user). Therefore, you should expect the worst and try to avoid the exceptions that will be thrown by Parse(...). Instead, you could use TryParse:

Code Block

int i;

if (int.TryParse(userInput, out i))
{
//input was OK
}
else
{
//bad input
}



HTH,
Kent