Heya guys,

I have a very small snippet that drives me crazy right now.

int arg1;
int expr1 = arg1 = 16;

That silly online translators giving me the following output

Dim arg1 As Integer
Dim expr1 As Integer = InlineAssignHelper(arg1, 16)

Great but what should i write in the "InlineAssignHelper" function? What should that function return?

Dani AI

Generated

Short version: C# lets an assignment be an expression (it returns the assigned value); VB.NET treats assignment as a statement and does not return a value. Converters that try to preserve the C# expression form often insert an InlineAssignHelper that assigns via ByRef and returns the value. Implementing that helper is simple, but it's only a translation trick — in plain VB it's usually clearer to perform two separate assignments as suggested.

Here is a straightforward, reusable implementation you can drop into a module or class:

Public Module InlineAssignHelpers
    Public Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
        target = value
        Return value
    End Function
End Module

What it does: it sets the variable passed ByRef and returns the same value, so an expression context can receive the assigned value. Important cautions: the target must be an assignable variable (some expressions and compiler-generated temporaries may not be accepted), using this pattern harms readability, and it’s non‑idiomatic VB. For simple cases — two variables receiving the same literal — prefer separate assignments (clearer and direct). Use the helper only when you must preserve single-expression semantics from C# (or when an automated translator produced it), and always test edge cases (properties/expressions that can’t be passed ByRef, and any code that relies on evaluation order or thread-safety).

As noted, refactoring to explicit assignments is the best approach; , if you still need the helper for a converted expression, the implementation above answers “what should it return.”

Recommended Answers

All 2 Replies

what should i write in the "InlineAssignHelper" function?

Nothing. Don't use it.

int arg1;
int expr1 = arg1 = 16;

is a very stupid way to set initial values to variables. It's the same as:

int arg1 = 16;
int expr1 = 16;

and now the code is trivial to translate:

Dim arg1 As Integer = 16
Dim expr1 As Integer = 16

HTH

Knew C#'ers are weird :p
Thank you, couldn't really see any sense in that snippet at all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.