Hi,
How to resolve this
Error 4 'string' does not contain a definition for 'DataType' and no extension method 'DataType' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?) C:\App16\Default.aspx.cs 64
24 App16
due to this line?
if (!(str1.DataType==typeof(int)))
...
How you declared str1?
Thanks
It is a string.
Hi,
You need to use GetType() method to get the datatype of variable.
Please try with the below code
//Checking variable datatype is int or not
if (!(str1.GetType() == typeof(int)))
{
//Your code below
}
Another suggestion
//Checking variable datatype is int or not
if (!(str1.GetType().Equals(typeof(int))))
{
//Your code below
}
wmec
It is a string.
Since it's a string, you can't compare it to anything else. A string will always be of type System.String so your if statement will always be false. If you are trying to determine what the contents are, that's an entirely different story. You'll have to
test them though, but .Net makes some easy ways to do that. For ints, and most other basic types, there is a TryParse() method that accepts a string value as the first parameter, and outputs the end result through the second parameter and returns a boolean
true/false if the item passes the parse. Example:
int test = 0;
if(int.TryParse(str1, out test))
{
// then we know it's an int, and test now holds the integer value of whatever was in the string str1
}
Sorry A2H. I don't know why 2nd if below is return true
string str1 = tb_ite.Text;
if (tb_ite.Text.Trim() != "")
{
if (!(str1.GetType() == typeof(int)) && !(str1.GetType() == typeof(double)) && !(str1.GetType() == typeof(long)) && !(str1.GetType() == typeof(short)) && !(str1.GetType() == typeof(float)))
{
...
even if str1 is having 5 inside.
Hi,
You have to separate each check condition using "()' properly.
Can you please try with the below code
if (tb_ite.Text.Trim() != "")
{
if ((str1.GetType() == typeof(int)) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float))))
{
}
}
Sorry, it is the same by this
string str1 = tb_ite.Text;
if (tb_ite.Text.Trim() != "")
{
if ((!(str1.GetType() == typeof(int))) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float))))
{
...
Hi,
Apologize for providing you wrong code. I missed to add one not condition in if statement
Try with the below code
string str1 = tb_ite.Text;
if (tb_ite.Text.Trim() != "")
{
if (!((!(str1.GetType() == typeof(int))) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float)))))
{
}
}
Sorry, the real question is, it should not fall into 2nd if-line below, as str1 is having 5 inside.
string str1 = tb_ite.Text;
if (tb_ite.Text.Trim() != "")
{
if ((!(str1.GetType() == typeof(int))) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float))))
{
...
but why does it fall into it?
Hi,
If you are checking for null values in variable 'str1' with the below statement
if (tb_ite.Text.Trim() != "")
{
}
then the inner if condition will always get executed. The reason is str1 holds a value "5" and it is not null("")
If you don't want to go inside the inner if condition if str1 holds some value then you may need to change your checking condition like given below.
Basically here you are checking whether the value is equal to null or not.
Solution 1:
//Here we are checking whether the value of str1 equals "" and if it is not then inner code wont execute
if (tb_ite.Text.Trim() == "")
{
if (!((!(str1.GetType() == typeof(int))) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float)))))
{
}
}
Solution 2:
if (String.IsNullOrEmpty(tb_ite.Text.Trim()))
{
if (!((!(str1.GetType() == typeof(int))) && (!(str1.GetType() == typeof(double))) && (!(str1.GetType() == typeof(long))) && (!(str1.GetType() == typeof(short))) && (!(str1.GetType() == typeof(float)))))
{
}
}
Hope this helps.
沒有留言:
張貼留言