Answered by:
problem converting VB to C#

Question
-
User5363102 posted
HI Guys,
I am writing server side validation in c# which was in VB
the code in VB is
If Not IsNumeric(Args.Values("BookQty")) Then
Args.Cancel = True
EditMSG.Text = "• Quantity is not numeric"If Args.Values("BookQty") < 0 Then
Args.Cancel = True
EditMSG.Text = "• Quantity is out of range"
End Ifthe code I converted to c# using converter
These are two errors
if (!IsNumeric(Args.NewValues["BookQty"]))
{
Args.Cancel = true;
EditMSG.Text = "•Quantity is not numeric";The name 'IsNumeric' does not exist in the current context
if ((Args.Values["BookQty"] < 0))
{
Args.Cancel = true;
EditMSG.Text = "• Quantity is out of range";
}Operator '<' cannot be applied to operands of type 'object' and 'int'
can anybody please help me.
Friday, August 29, 2008 12:11 PM
Answers
-
User242249085 posted
if (!IsNumeric(Args.Values["BookPrice"])
Error : The name 'IsNumeric' does not exist in the current context
In C# you have to call a static method using the name of the class, this is why I wrote "Microsoft.VisualBasic.Information.IsNumeric" (this can be shortened with a using directive).
if ((int.Parse(Args.Values["BookID"].ToString()) == ""))
Error: Operator '==' cannot be applied to operands of type 'int' and 'string'.
What are you trying to check for here?
If you want to check whether the conversion worked, then you are better off with int.TryParse (as noted by another reply).
If you want to check if converting makes sense, then you need to check before the call:
string str = Args.Values["BookID"].ToString(); if (!String.IsEmptyOrNull(str)) { int res; if (int.TryParse(str, out res)) { // Conversion succeeded. } }
Finally, I note you are calling ToString on the value from Args collection. Why not cast directly to its actual type rather than converting to a string and then parsing to an int?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 1, 2008 4:55 AM
All replies
-
User56914146 posted
The problem is that you need to create a method called IsNumeric to check if the value is numeric.. for example...
private bool IsNumeric(Object obj ){
int result; bool isNumeric = int.TryParse(obj.ToString(), out result); return isNumeric;}
Your other problem is that you are trying to compare an object type to an int type... You need to cast or parse the value to an int.. for example.....
if ((int.Parse(Args.Values["BookQty"]) < 0)){
Args.Cancel =
true; EditMSG.Text = "• Quantity is out of range";}
Hope this helps
Friday, August 29, 2008 12:40 PM -
User5363102 posted
I have already tried int.parse if ((int.Parse(Args.Values["BookQty"]) < 0))
but the error still is the same
thank you though
The problem is that you need to create a method called IsNumeric to check if the value is numeric.. for example...
private bool IsNumeric(Object obj ){
int result;bool isNumeric = int.TryParse(obj.ToString(), out result);
return isNumeric;}
Your other problem is that you are trying to compare an object type to an int type... You need to cast or parse the value to an int.. for example.....
if ((int.Parse(Args.Values["BookQty"]) < 0)){
Args.Cancel = true;
EditMSG.Text = "• Quantity is out of range";}
Hope this helps
Friday, August 29, 2008 12:55 PM -
User56914146 posted
try adding ToString()...
if(int.Parse(Args.Values["BookQty"].ToString())< 0)
Friday, August 29, 2008 3:54 PM -
User242249085 posted
The problem is that you need to create a method called IsNumeric to check if the value is numeric..Actually you don't.
You can use the VB method:
- Add Microsoft.VisualBasic.dll as a project reference.
- Call as:
if (Microsoft.VisualBasic.Information.IsNumeric(val) {
...Saturday, August 30, 2008 10:13 AM -
User5363102 posted
I am still getting the same error
if ((int.Parse(Args.Values["BookID"].ToString()) == ""))
Error: Operator '==' cannot be applied to operands of type 'int' and 'string'.
if (!IsNumeric(Args.Values["BookPrice"])
Error : The name 'IsNumeric' does not exist in the current context
This is after adding the reference Microsoft.Visual basic.
I am just looking to write a required field validator using the details view Item Inserting event here is the signature Validate_Insert_Data(object Src, DetailsViewInsertEventArgs Args)
can anybody help me with an example.
thanks in advance .........
Sunday, August 31, 2008 8:45 PM -
User-1910946339 posted
Hi, don't know why you can't get IsNumeric to work but you don't need it. Try changing
if (!IsNumeric(Args.NewValues["BookQty"]))
{
Args.Cancel = true;
EditMSG.Text = "•Quantity is not numeric";if ((Args.Values["BookQty"] < 0))
{
Args.Cancel = true;
EditMSG.Text = "• Quantity is out of range";
}to
int bookQty; bool quantityIsAnInt = int.TryParse(Args.Values["BookQty"], out bookQty); if (!quantityIsAnInt) { Args.Cancel = true; EditMSG.Text = .... ... exit from the routine } if (bookQty < 0) { Args.Cancel = true; EditMSG.Text = ... ... exit from the routine } // At this point bookQty is a valid int
Sunday, August 31, 2008 9:38 PM -
User242249085 posted
if (!IsNumeric(Args.Values["BookPrice"])
Error : The name 'IsNumeric' does not exist in the current context
In C# you have to call a static method using the name of the class, this is why I wrote "Microsoft.VisualBasic.Information.IsNumeric" (this can be shortened with a using directive).
if ((int.Parse(Args.Values["BookID"].ToString()) == ""))
Error: Operator '==' cannot be applied to operands of type 'int' and 'string'.
What are you trying to check for here?
If you want to check whether the conversion worked, then you are better off with int.TryParse (as noted by another reply).
If you want to check if converting makes sense, then you need to check before the call:
string str = Args.Values["BookID"].ToString(); if (!String.IsEmptyOrNull(str)) { int res; if (int.TryParse(str, out res)) { // Conversion succeeded. } }
Finally, I note you are calling ToString on the value from Args collection. Why not cast directly to its actual type rather than converting to a string and then parsing to an int?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 1, 2008 4:55 AM -
User-16010594 posted
Hi all,
I hv added refernce of Microsoft.VisualBasic.dll (V4.0) for vs 2010 still i m getting error on line
"double CheckNumericNull =Microsoft.VisualBasic.Conversion.Val(Data);"
Error 170 The type or namespace name 'Conversion' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)
Plz help me resolve the error.
I m struggling since yesterday.
Thanx all in advance
Tuesday, May 21, 2013 2:58 AM -
User397347636 posted
You should be able to call it like that, but if you want to avoid having the VisualBasic namespace in your project you can use a helper class we use in one of our converters to replace calls to 'Val':
//---------------------------------------------------------------------------------------- // Copyright © 2003 - 2013 Tangible Software Solutions Inc. // This class can be used by anyone provided that the copyright notice remains intact. // // This class simulates the behavior of the classic VB 'Val' function. //---------------------------------------------------------------------------------------- public static class SimulateVal { public static double Val(string expression) { if (expression == null) return 0; //try the entire string, then progressively smaller //substrings to simulate the behavior of VB's 'Val', //which ignores trailing characters after a recognizable value: for (int size = expression.Length; size > 0; size--) { double testDouble; if (double.TryParse(expression.Substring(0, size), out testDouble)) return testDouble; } //no value is recognized, so return 0: return 0; } public static double Val(object expression) { if (expression == null) return 0; double testDouble; if (double.TryParse(expression.ToString(), out testDouble)) return testDouble; //VB's 'Val' function returns -1 for 'true': bool testBool; if (bool.TryParse(expression.ToString(), out testBool)) return testBool ? -1 : 0; //VB's 'Val' function returns the day of the month for dates: System.DateTime testDate; if (System.DateTime.TryParse(expression.ToString(), out testDate)) return testDate.Day; //no value is recognized, so return 0: return 0; } public static int Val(char expression) { int testInt; if (int.TryParse(expression.ToString(), out testInt)) return testInt; else return 0; } }
Tuesday, May 21, 2013 10:06 AM