Convert Generic Value to Int32

Hi,
I have got Variable named BZWBK which is GenericValue. I need to convert that variable to Int32. To do that I’m using Assign activity and this VB expression: Convert.ToInt32(BZWBK As Integer) As Integer, but I get message error: Comma or valid expression continuation expected.

What I’m doing wrong? Or maybe there is quicker way to convert generic value to int32?

Thanks for any advice

2 Likes

Does Convert.ToInt32(BZWBK.ToString) works?

1 Like

No, it ends with this error:
Assign : Input string was not in a correct format.

It means you don’t have something that can be converted to an int in your variable.
Please debug or use a Write Line to output the content of BZWBK.

1 Like

Write Line returned right value, which is 4.2074

That value is a Decimal, not an Integer, hence the error is correct.
Question now becomes - do you want just the full numbers and if yes, floored or rounded?

You could do CInt(BZWBK.ToString) which would change to Int with normal math rounding, but since this is a Banking operation, I really think that what you’re looking for is Decimal.Parse(BZWBK.ToString), or even just BZWBK.ToDecimal (note that GenericValue type objects have helper methods for converting to primitive types like Int32, Decimal, Date etc.).

Somehow it feels wrong to round things when banks are involved :slight_smile:

10 Likes

Mmmm…

I think already is valid. What are you trying?, sum two variables?

When I do, for example, var1 + var2 (where var1 is GenericValue and var2 is Int32), works fine.

Take a look:

It works because GenericValue has implicit conversions defined for primitive types.
Sometimes you need specific format or type and implicit conversion is not enough of a control.

In your example, var1 is a GenericValue and var2 is an Int32. What actually happens in the addition operation, is that the Int is implicitly converted to GenericValue and the result is not an Int32, which you might expect, but a GenericValue instead.

You can check it by f.e. doing a WriteLine/MessageBox for this:
(var1 + var2).GetType.ToString
It will print out UiPath.Core.GenericValue

Now if you change var2 to be Decimal (in the example by Rafal it looks like a currency exchange rate, so the precision is required) it will throw an error, because the compiler can’t decide by itself if it should change var1 to Decimal or var2 to GenericValue.

It gets trickier the further you go…
With GenericValue you can check what’s the type of the underlying value by using GetTypeCode.
Consider these 2 scenarios:
GenericValue var1 = 123; Int32 var2 = 456; (var1 + var2).GetType.ToString // UiPath.Core.GenericValue (var1 + var2).GetTypeCode.ToString // Int32

This is as expected. But if the readout is not a whole number?

GenericValue var1 = 123.5D; // equivalent of reading 123.5 with GetText activity Int32 var2 = 456; (var1 + var2).GetType.ToString // UiPath.Core.GenericValue (var1 + var2).GetTypeCode.ToString // Decimal

This can bite back in some cases where you’re expecting a Decimal or Int, but you get the other one, and when using GenericValue you also don’t have access to type-specific methods.
Consider this easy to do mistake:
GenericValue var1 = 456D; Console.WriteLine(var1.ToString("F")); // prints F Decimal var2 = 456D; Console.WriteLine(var2.ToString("F")); // prints 456.00
This can very easily result in a very unexpected runtime error.

This got lengthy, but in short:

  • GenericValue can be used with implicit conversions as most primitive types
  • Implicit conversions can save you the hassle of casting/converting making code easier to read
  • Implicit conversions can sometimes make you end up with unexpected results
  • Using explicit conversions is sometimes needed to make sure you have the exact type you need to call correct methods and/or preserve formatting

Regards,
Andrzej

3 Likes

Thank you Andrzej for your help. Of course in banking process I need decimal, this conversion works in UIPath: Decimal.Parse(BZWBK.ToString)

1 Like

Hi,

You can also try using FormatValue activity to specify how the implicit conversion should be used. It might not help for some scenarios that Andrzej specified.

Regards,
Lavinia

3 Likes

maybe we should add implicit conversion from/to Decimal ?

Adrian.

It has implicit conversion for decimal but for operations it depends on the type of the left operand.

Therefore
Decimal d = 0.0D + someGenericValue will convert someGenericValue to Decimal

yes, but the GenericValue d = 0.0D + someIntVariable will convert to GenericValue

Convert.ToInt32(BZWBK.ToString) will work …plz try it once…

2 Likes

in my code i want to get the text from SAP application which i am getting from get full text activity.

Output variable is generic. (testStr)

what i want is to get the number from that variable.

I used this code :slight_smile:

Int testOne = Int32.parse(testStr.tostring)

and its giving me the error : input string is not in correct format.

Could you please help.

@Suchi3190 I am not sure try once

testOne = testStr.GetType.ToString

Generic variable testStr containsvalue : “11 Clarification code is present”

I want 11 from that varibale.

@indra its giving implicit conversion compile time error.

Hey @Suchi3190

Try this :slight_smile:

  Generic txtvalue = “11 Clarification code is present”;

String int_data =Regex.Replace(txtvalue.ToString, "[^0-9]+", string.Empty) // will return 11

Convert.ToInt32(int_data)

Note- it will give you integer part from the string(by replacing other things based on pattern). if there will be more integer occurrence then also let me know :slight_smile:

Regards…!!
Aksh

1 Like