Указатель NULL
- это тот, который указывает на никуда. Когда вы разыскиваете указатель p
, вы говорите «дайте мне данные в месте, хранящемся в« p ». Когда p
является нулевым указателем, местоположение, хранящееся в p
, является nowhere
, вы говорите «Дайте мне данные в месте« нигде ». Очевидно, он не может этого сделать, поэтому он выбрасывает NULL pointer exception
.
В общем, это потому, что что-то не было правильно инициализировано.
Можно узнать, является ли переменная числовым использованием Type.GetTypeCode()
метод:
TypeCode typeCode = Type.GetTypeCode(desiredType);
if (typeCode == TypeCode.Double || typeCode == TypeCode.Integer || ...)
return true;
необходимо будет завершить все доступные числовые типы в "..." часть;)
[еще 115] детали здесь: Перечисление TypeCode
Большая статья здесь Исследование IsNumeric для C#.
Опция 1:
Ссылка Microsoft.VisualBasic.dll, затем сделайте следующее:
if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
//Do Something
}
Опция 2:
public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;
IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
iConvertible = (IConvertible) Expression;
}
if (iConvertible == null)
{
if ( ((Expression is char[])))
{
Expression = new String ((char[]) Expression);
goto IL_002d; // hopefully inserted by optimizer
}
return 0;
}
IL_002d:
TypeCode typeCode = iConvertible.GetTypeCode ();
if ((typeCode == 18) || (typeCode == 4))
{
string str = iConvertible.ToString (null);
try
{
if ( (StringType.IsHexOrOctValue (str, l)))
{
f = true;
return f;
}
}
catch (Exception )
{
f = false;
return f;
};
return DoubleType.TryParse (str, a);
}
return Utils.IsNumericTypeCode (typeCode);
}
internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3:
case 6:
case 7:
case 9:
case 11:
case 13:
case 14:
case 15:
return 1;
};
return 0;
}
Используйте тип. IsValueType () и TryParse ():
public bool IsInteger(Type t)
{
int i;
return t.IsValueType && int.TryParse(t.ToString(), out i);
}
Можно теперь использовать метод Платформы.NET
typeof(decimal?).IsNumericType()
''// Return true if a type is a numeric type.
Private Function IsNumericType(ByVal this As Type) As Boolean
''// All the numeric types have bits 11xx set whereas non numeric do not.
''// That is if you include char type which is 4(decimal) = 100(binary).
If this.IsArray Then Return False
If (Type.GetTypeCode(this) And &HC) > 0 Then Return True
Return False
End Function