开发者

How can I set values for variables that are declared on one line?

I have a list of variables that are being created on one line.

Dim strFirstname, strMiddleName, strLastName As String

Is it possible to set the values for all开发者_开发问答 of them at once? I know this doesn't work, but this is what I'm trying to do :

Dim strFirstname, strMiddleName, strLastName As String = ""


I liked the examples, but I just needed to set the values so that the compiler would leave me alone. I have another function that sets all the values that I'm passing these strings into. I wanted to set them to pretty much nothing, just so it would leave me alone. I ended up using this:

Dim strFirstname, strMiddleName, strLastName As New String(String.Empty)


If you want different values, this should work:

Dim strFirstname As String = "First name", strMiddleName  As String = "middle name", strLastName As String = "last name"


If you truely want one line instantiating them all, create an object to hold this data instead, e.g.

...
person.Firstname
person.MiddleName
person.LastName
...

Then populate them in the constructor, e.g.

Dim person As New Person("first","middle","last")

Seems like a cleaner option if your objects are all related to a specific idea/entity.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜