How to make a multiple variable definition in VB.net

Asked

Viewed 141 times

1

Hello, normally I program on the web I am venturing into VB.net (by free and spontaneous pressure...) and both in Typescript and Javascript and even in PHP, I have the habit of making a multiple definition of variables. Example:

var a, b, c;
// definição singular de dados
a = '';
b = '';
c = '';
// definição múltipla de dados
a = b = c = '';

I understand that I can not directly compare Javascript with VB.net because JS is not a strongly typed language but, such a definition also works in VB. My problem is that when I have a component that an attribute of it of type string, and I make such a statement, it presents me a boleano value, like this:

Public Class Foo
    Public text As String
End Class

Dim a, b, c As New Foo
' definição singular de dados
a.text = ""
b.text = ""
c.text = ""
' definição múltipla de dados
a.text = b.text = c.text = ""

In my opinion (logically speaking) the language should define the c.text as an empty string, then the b.text as the value of c.text and at the end do the same with a.text, thus, how can I make a multiple set as in the example above, having the same value for the three variables?

2 answers

2

It is not possible to do this in VB.NET, the most that gives is to put everything in the same line, but they will be completely separate assignments.

In fact it is almost always something that should not be done because it is to save typing and not because it should be all the same thing, only a coincidence makes it the same thing.

1


The way it is in your question, can not, because as you said yourself can not compare with other languages, but, there are means of assignments that will work as equal assignments for each instance created:

Dim a, b, c As New Foo With {.text = ""}

or even initialize the property .text with "" thus:

Public Class Foo
    Public text As String = ""
End Class

believe to be the best forms of attribution when one wants to work with variables with the same type, but, I particularly like to put each separate statement, but, nothing prevents to do so by this basic example of your question.

Running in Netfiddle

  • 1

    got it... well I use it in cases that in 1 action I have to change several components at the same time, ex: 3 inputbox that when a checkbox is marked, it will enable the 3 (ie inputbox.Enable = true and if uncheck it already removes the 3.

  • @Leandroluk correct, but, VB.NET does not do this, maybe you need to create methods or auxiliary codes to assign this way, just like this in the question is not really language. Who knows one day, they’ve put so many things they don’t need ... !!! kkkk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.