简体   繁体   中英

VB.Net Code grouping indentation with a colon :

So in the code I work on right now my predecessor has written these weird colons before some items of a group box in VB.Net Forms. It looks like this:

myGroupBox.Text = "MyGroupBox"
myGroupBox.Bounds = New Rectangle( ... )
: firstGrpBxElement.Text = "First"
: firstGrpBxElement.Bounds = New Rectangle( ... )
: scndGrpBxElement.Text = "Second"
: scndGrpBxElement.Bounds = New Rectangle( ... )
: thirdGrpBxElement.Text = "Third"
: thirdGrpBxElement.Bounds = New Rectangle( ... )
myGroupBox.Height = thirdGrpBxElement.Bottom + Padding

... where the = New Rectangle(... ) mean proper Rectangles. The ... is just to make this easier to read (and the coordinates arent important in this case, I suppose).

What do these colons mean? Is it just for readabilities sake, or should I use them? Because in the context I understood them so far, they seem to be deleted by IntelliSense once I added them to the code manually (I added code in between).

I couldnt find anything on Google (probably because I dont know how they are called), so any help would be appreciated. Thank you!

Colons can be used in VB.NET to combine statements on the same line.

For example, this is valid:

Dim a = 1 : Dim b = 2

and is the same as

Dim a = 1
Dim b = 2

Maybe the code was previously used like

If a = 1 Then b = 2 : c = 3

This "saves" the If..End If block, so is identical to

If a = 1 Then
    b = 2
    c = 3
End If

Given that the statements you have there would mean that the first statement is empty, the colon is redundant and hence is removed by IntelliSense.

I would avoid those concatenations in any case, because they decrease the readability a lot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM