Switch System.Type

@richarddenton

Has anybody had any success doing a Switch on System.Type?

https://uipath.zendesk.com/hc/en-us/requests/63536?page=1

Thanks

1 Like

hey @radu.ghitescu

You only have to use switch?

what about ?

if(property.PropertyType == typeof(bool)) {
    //dostuff;
}
else if (property.PropertyType == typeof(string)) {
    //do other stuff;
} 

or nested if-else?

because You cannot use a switch block to test values of type Type. Compiling your code should give you an error saying something like: A switch expression or case label must be a bool, char, string, integral, enum, or corresponding nullable type.

1 Like

Thanks for the reply.

I know that C# only supports switching on primitives, but I thought that VB 2010 does support switching on objects?

1 Like

Sorry Not much aware with that fact but you can use if-else as a workaround i know it will be like that but you can use dictionary as well.

for if-else you can use above mentioned as well this one also “Type.GetTypeCode(data_int.GetType)” will return the typecode like if data_int variable is int then it will return int32 and if it is string then it will return string.

Hey @aksh1yadav - cheers for the input!

This seems like a workaround, the only problem is that it doesn’t allow us to route the flowchart as a result. We would have to assess the system type and then assign a variable with which to continue down the correct path. This is what w are trying to avoid - we’d like to be completely reliant on the Exception type rather than creating variables.

Hi Radu,

Happy to see you here.

In addition of what the other said, here is an example so you could look at to handle the exception types in VB.Net.

Unfortutaly it is not possible to reproduce it within Switch activity in UiPath since the Cases do not accept expressions when it comes objects.

If you are not interested in the Exception Inheritence Mechanism you could also use TypeName(ex) which would return the most specific type of the exception as String, then you can switch on it without issue.

That all depends what exactly what you would like to achieve.

Cheers

2 Likes

Yes for now you can do like this

Because UiPath switch activity only supports now only such as integer, string or array, and more complex ones such as data tables.

:thumbsup:

Regards…!!
Aksh

If DataTable is supported, then all objects should be supported. I can understand the restriction for primitives, but it doesn’t seem to be the case :slight_smile:

DataTables are supported? In which version?

Btw - Switch activity is MS, not UiPath (unless they rolled their own in a release newer than I’m using).

:laughing::sweat_smile: missed in