Studio allows me to set a default "Nothing" value for non-nullable types, yet later it complains when I try to make use of it

I’m not used to program in Visual Basic (yet I have some background).

I’m declaring a couple of Int32 variables in the most outer scope of the project. I expected to use a change in these to control the flow of a state diagram project. As it allowed me to assign a “Nothing” default value to them, I wanted to use the value of these as transitioning conditions, including the value of greater than zero and having no value.

Well, when I try to compare the value with “Nothing”, then it it complains about “‘Is’ operator does not accept operands of type ‘Integer’. Operands must be reference or nullable types”.

Then I wonder why Studio allowed me to assign this “Nothing” value from the beginning, allowing further assumptions and programming that later has to be undone.

If the type doesn’t allow Nothing, then constraint it from the beginning. That’s the purpose of strongly-typed languages.

I asked also ChatGTP about this and here’s the full conversation, with the replies I got from it:

User

in Visual Basic, why if I can assign a “Nothing” value to an Int32 variable as default, later the compilator complains about not being possible to compare that same variable against being “Nothing”?

ChatGPT

In Visual Basic, an Int32 variable (or any other value type variable) cannot be assigned a value of Nothing directly because Nothing represents the default value for reference types, not value types. Value types, such as Int32, hold their values directly rather than references to an object in memory, so they cannot be set to Nothing.

However, Visual Basic does allow the use of nullable value types using the Nullable(Of T) structure or the shorthand notation T?, where T represents a value type. This allows you to assign a null-like value to a value type. For example, you can use Integer? instead of Int32, and then you can assign Nothing to it.

Here’s a simple example:

vbCopy code

Dim nullableInt As Integer? = Nothing

Now, when comparing a nullable value type to Nothing, Visual Basic provides special syntax to handle it:

vbCopy code

If nullableInt Is Nothing Then
    ' Do something
End If

Or you can use the HasValue property:

vbCopy code

If Not nullableInt.HasValue Then
    ' Do something
End If

But for non-nullable value types like Int32, you cannot compare them directly to Nothing because they cannot hold a null-like value. If you try to compare a non-nullable value type variable to Nothing, you will encounter a compilation error because they are inherently not nullable.

User

But I’m using UiPath Studio (which uses Visual Basic in the background) and it allowed me to assign a “Nothing” default value for Int32 variables

ChatGPT

ChatGPT

In UiPath Studio, the default value for an Int32 variable is indeed Nothing. This behavior might seem contradictory, but it’s due to how UiPath Studio handles variable initialization and the specifics of its implementation.

When you declare an Int32 variable in UiPath Studio and don’t assign it a value explicitly, it’s initialized with the default value, which is Nothing. This is a convenience feature provided by UiPath Studio to simplify workflow design, but it’s not standard behavior in Visual Basic.

However, even though UiPath Studio allows you to initialize Int32 variables with Nothing, it doesn’t change the fact that Int32 is a value type in Visual Basic and cannot normally hold a Nothing value. UiPath Studio’s implementation abstracts this detail from the user, allowing for more straightforward automation workflows.

So, while UiPath Studio permits the use of Nothing as the default value for Int32 variables, when comparing such variables, you should use the .HasValue property or Is Nothing syntax for nullable types. If you encounter issues with comparisons, double-check your variable declarations and consider using nullable types where appropriate.

User

I don’t understand what kind of advantage provides this. It explicitly allows me to assign a Nothing as a default value, yet later it doesn’t allow me to check if that variable is still “Nothing”. No sense to me. Studio better should be checking from the beggining that I don’t attempt that “Nothing” assignation and forbid it to me.

ChatGPT

ChatGPT

You raise a valid point. The behavior you described can indeed lead to confusion and unexpected results, especially for those familiar with the standard behavior of value types in Visual Basic.

Allowing Nothing as the default value for Int32 variables in UiPath Studio might have been a design decision made to simplify workflow creation for users who are not necessarily familiar with the intricacies of programming languages. It could be seen as a way to provide flexibility and ease of use within the UiPath Studio environment.

However, you’re correct that this behavior can lead to inconsistencies and errors when users attempt to compare such variables against Nothing. In an ideal scenario, UiPath Studio would provide clearer guidance or restrictions to prevent such inconsistencies, either by disallowing the assignment of Nothing to non-nullable value types or by enforcing stricter rules around comparing such variables to Nothing.

It’s possible that future updates to UiPath Studio may address this issue by either refining the behavior or providing better guidance to users about how to handle variable comparisons. In the meantime, it’s important for users to be aware of this behavior and to take extra care when working with variables that have been initialized with Nothing as their default value.

User

What flexibility?!?! There’s no flexibility. If you assign a “Nothing”, it allows you to do so, and then you try to make use of that “flexibility”, it won’t and it will complaint, and won’t allow you to!

ChatGPT

ChatGPT

You’re absolutely correct, and I apologize for the confusion. The behavior you described does not truly offer flexibility; rather, it creates inconsistency and confusion, as it allows an operation that cannot be reliably used afterward. This can lead to errors and frustration for users.

In this context, the behavior could be considered more of a flaw or inconsistency rather than a feature offering flexibility. It’s essential for UiPath Studio (or any development environment) to provide consistent behavior and clear guidance to users to prevent such issues.

If you encounter situations like this where the behavior seems contradictory or leads to errors, it’s advisable to provide feedback to the UiPath team. This can help them improve the platform and provide a better user experience for everyone. Additionally, until such inconsistencies are addressed, it’s crucial to be cautious and aware of these potential pitfalls when working with variables in UiPath Studio.

The real issue is that it shouldn’t let you assign Nothing to an int32 variable. You can use IsNothing(intVar) logic in an expression with int32, but it will always return false because int32 is not nullable. Best bet is to use your process logic to decide on a value (such as -1) that you handle as “nothing.”

1 Like

You are right.
Value Types in VB always initialize with a default value. Int will always be initialized with value ‘0’ unles specified otherwise.

Maybe UiPath allowed to set as Nothing to have compatibility with the arguments feature. In there, even it does not looks like, all types are always Nullable(Of T). I guess this was made that way so they can implement the checkness for “Is argument Required”, I don’t Know.

Either way, you best call here is to use a Nullable(Of Int32) in your variable type, or don’t use Nothing at all.

1 Like

Roughly my idea was to use 2 variables in two stages of my project. First, most “global” one, should allow null. That helps me simplifying checks and transition changes as being Nothing would mean to me something as 0 results.

Then, ideally, I’d like to assign the value of a “function”, or a workflow that has an output parameter, to that same “Nothingable” variable, but in this case I’d like the output to be a “primitive” int, that it, no nulls allowed. And I found out that it won’t me todo so. It’ll complaing about not being of the same type. Which is a pity, as Int32 is a subset of “Int32?” (nullable Int32). That doesn’t happen in serious languages, as Java. Oh well.

Still waiting from anyone from the staff to explain this.

Seems like a VB limitation.

If a variable is of a value type that is not nullable, assigning Nothing to it sets it to the default value for its declared type. If that type contains variable members, they are all set to their default values.

Source: Nothing keyword - Visual Basic | Microsoft Learn

Indeed that happens when I tried in Studio. Code (“test” is Integer variable):
image
Output:
image

@pere /@postwick Nothing is a valid expression that represents a Null reference. Since an Int32 variable is not nullable and it’s not a reference setting it to Nothing means to use the default value (in this case 0).

So if you want to compare an integer variable you can test if it equals to zero instead.

And do I have to infere that “Nothing” in an Int32 variable means “zero”?

Then don’t allow it to accept “Nothing” as value, and only allow it to accept “0”.

1 Like

But zero is a value. Null is the absence of a value.

That’s an inherited behavior from VB.Net. This page was shared previously - Nothing keyword - Visual Basic | Microsoft Learn and it explains the same behavior.

Nothing is also zero, not necessarily null (absence of a value) :slight_smile:

You can use C# if this is a big issue.

Thanks for the info and the suggestion. It’s very clarifying.

This is a core VB.NET thing and another reason C# is better, all the ‘forgiving’ VB.NET does just makes it a more confusing language.

However the answer to this is pretty easy, just make the Int into Int? so it can have a null value.

Its very easy, in the variables pane find Nullable and then set the T to Int32.
You now have a nullable Int32 that can hold a null (nothing) or zero.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.