Alternative for Microsoft.VisualBasic.Information.IsNumeric in C# - c#

attrval[5] = WrmService.WindowsAgent.AgentVersion;
From above if attrval[5] is null or not getting any value or any strings other than numeric values I want to assign attrval[5] to value '0.0.0.0' otherwise I will
display the numeric value which is coming.What coding i have to implement here
as per the information while doing googling I did this
attrval[5] = (WrmService.WindowsAgent.AgentVersion == null || Microsoft.VisualBasic.Information.IsNumeric(WrmService.WindowsAgent.AgentVersion)) ?
"0.0.0.0" : WrmService.WindowsAgent.AgentVersion;
but Microsoft.VisualBasic.Information.IsNumeric making problems. Is there any similar one in C#?
only two outputs I required one will be numeric and one will be any other, it can be string or null whatever it i have to set in to 0.0.0.0

Try
if(!int.TryParse(WrmService.WindowsAgent.AgentVersion, out attrval[5])) attrval[5] = 0;
In this case, if AgentVersion is numeric, it will place the parsed value into attrval[5], otherwise it will set it to 0.
edit
Ah I guess you are looking for:
attrval[5] = string.IsNullOrEmpty(WrmService.WindowsAgent.AgentVersion) ? "0.0.0.0" : WrmService.WindowsAgent.AgentVersion;

I would recomend using something like
Int32.TryParse Method (String, Int32)
See also C# Equivalent of VB's IsNumeric()

You could use Int32.TryParse() to check if it is an integer value.

Related

AND operation in C#

I want to perform an AND operation. My inputs are 2 objects. It could be a string like "true" or it could be an expression like "1==1" as well. When using && operator, am getting an exception that String was not recognized as a valid boolean.
Please help me.
return Convert.ToBoolean(obj[0]) && Convert.ToBoolean(obj[1]);
Sorry for the earlier post which was not clear enough.
Converting "1==1" to a boolean is not possible for the Convert.ToBoolean method. It just converts the strings true and false.
You will have to either write a expression evaluator yourself, or use some kind of library to parse your string to a boolean (like Flee for example)
First make sure obj[0], obj[1] will only contain 1 or 0(char or integer).
Because Convert.ToBoolean does not understand anything other than 1 or 0.
The below one will work
Convert.ToBoolean(true) && Convert.ToBoolean(1==1)
Why use a string?
The conversion will not evaluate code, it will check if the supplied data is possible to convert to a bool and do so if possible.
Your function will always return true if it was working, cause 1 is always equal to 1, and true is always true.
This is nearly impossible as C# is strongly type language.
What you trying to do is for weakly types languages like JS. "1==1" will work for JS, not for C#.
Remove quotes in order to make it work(You might as well remove first operand, as it doesn't make any sense):
return ( 1 == 1 );

string.Compare("KHA","KTB",true) return incorrect result in C#

I am using C#, .NET 3.5. I have following code
string.Compare("KHA","KTB",true)
It returned value 1. This means KHA > KTB in alphabet order.
I expect it returns value -1.
Can anyone help me fix this?
Yes, all of you are right. It's because of the Culture. I add CultureInfo.InvariantCulture and it is solved.
Thanks all!
strig.Compare returns the relative position in the sort order. Since 'H' comes before 'T' that is why you are getting 1
Its should return -1, See the image
There must be something wrong going on with your compiler, it should return -1 and your understanding for the string.Compare is right.
You may try using CultureInfo.InvariantCulture:
int value = string.Compare("KHA", "KTB", true,CultureInfo.InvariantCulture);
The call string.Compare("KHA","KTB",true) should return -1 as expected. It does when I test it.
If you get any other result, you either are using other strings, or you have a default culture where 'T' is actually considered to come before 'H'.
For the latter case, you can specify a culture info in the call:
string.Compare("KHA", "KHB", true, CultureInfo.InvariantCulture)
If you are really getting 1 against string.Compare("KHA","KTB",true) then your system's current culture must be making an effect. Check the documentation of String.Compare. Also check the best practices of comparing a string here.

Convert.ToInt32 in C#

My problem is, when I want to convert (result[i].JobOrder) to int.I have a string 120100 in (result[i].JobOrder). In return I get not integer but something like "0x0001d524". And I could not understand why.
for (int i = 0; i < result.Count; i++)
{
if (Convert.ToInt32(result[i].JobOrder) > maxJobOrder)
{
maxJobOrder = Convert.ToInt32(result[i].JobOrder);
}
}
Your code works, but you have set your debugger to display integers in hexadecimal. The value 0x0001d524 is the hexadecimal representation of the integer 120100.
This is not an error in the program, but a configuration option for your IDE. If you use Visual Studio, you can change this setting by pressing the "Hex" button in the "Debug" toolbar.
As an aside, if you are using C# 3 or newer you can simplify your code by using the Max method to find the maximum instead of looping:
maxJobOrder = result.Max(x => int.Parse(x.JobOrder));
Try to use
Int32.TryParse
see
http://msdn.microsoft.com/en-us/library/f02979c7.aspx
try to replace Convert.ToInt32 with int.parse()
The suggestion to use int.parse is slightly flawed.
int.parse will throw one of the following exception whenever it fails. And it only accepts strings as input to convert.
ArgumentNullException
FormatException
OverflowException
Convert.ToInt32 will throw one of the following exceptions whenever it fails. In addition,
it allows passing of null values, however this means that it returns a 0 as the output value, and it also handles multiple datatypes to be converted into an integer.
FormatException
OverflowException
int.TryParse will not throw any exceptions, however, it will return a 0 as the output value as the method returns false, and it only accepts strings as input to convert.
You should choose the right tool for the job to prevent any issues in the integrity of your solution.

Format.exception C#/Asp.Net/SQL

I am getting the error "System.FormatException : input string was not correct".
TextBox2.Text = objnm.rupees(Convert.ToInt64(Convert.ToDecimal(txtWOrds.Text.Trim())));
First, you don't need to convert it to decimal (Convert.ToDecimal) and then to Int64 (Convert.ToIn64).
Second, if txtWOrds.Text is not a number or is empty, than you will get this exception. Make sure that it is a number.
Third, if your value is a number, than your problem likes somewhere in objnm.rupees()
You should check the input in case its empty, like string.IsNullOrEmpty(txtWOrds.Text) then proceed with the parsing of the contents of the textbox.
Also you should be using TryParse which evaluates if the text can be parsed and if true you can use the value of the out parameter of this method.
In your case it could fail if the TextBox is empty.
Also if its anything related to money/currency not sure if you need the conversion to Long (seems like a mismatch there, please clarify. If you want a specific set of decimal points then it would be better to use decimal.Round )
Decimal value = default(decimal);
bool isValid = decimal.TryParse(txtWOrds.Text.Trim(), out value);
if (isValid)
{
//your code using output 'value'
}
Remove Convert.Int64 and just use Convert.ToDecimal (ideally you should use decimal.TryParse). Also, ensure that the input textbox contains the correct type (a decimal)

I need the sum of two fields that are in different DataTables

The problem is that in c# I can't subtract the objects, so I need to figure out how to get the integers out of them and then do the arithmetic? Here's the code.. what am I missing?
dsfDataSet.itemTotals.Compute( "SUM(priceSum)", String.Empty ) - dsfDataSet.discountItems.Compute("SUM(totDiscount)", String.Empty)
If you know the data is integer, you should use Convert.ToInt16 or similar functions to extract the integers. Be sure to add additional exception handling, in case the data turns out to be non integer.
you can use int.TryParse as the output of DataTable.Compute is object
int priceSum,totDiscount;
if(int.TryParse(dsfDataSet.itemTotals.Compute( "SUM(priceSum)", String.Empty ).ToString(),out priceSum))
{
if(int.TryParse(dsfDataSet.discountItems.Compute("SUM(totDiscount)", String.Empty).ToString(),out totDiscount))
{
priceSum - totDiscount;
}
}
What is the return type of your Compute function? Object? Or a defined type?
If defined, you CAN overload the '-' operator, you know?
Otherwise, what is stopping you from creating a method that take those two structures and return the integer result you need? Why must it be with '-' ?

Categories

Resources