For what use LABELS (left of the code) in Visual Basic Code?

  • Thread starter Thread starter Andreas Klemt
  • Start date Start date
A

Andreas Klemt

Hello,

for what is the LABEL good when programming in visual studio like this:

Sub xxxx

Label1: xxxx
xxxx
xxx

Label2: xxxxx

End Sub

For what do I need or can use "Label1" and "Label2" in my Visual Studio Code
?

Thanks for any help an answers in advance!
Andreas
 
Labels are a bit of a leftover from VB 6, IMO. The idea is being able to use
Goto statements to go to a specific label. For example:

Public Sub TestGoto()

Dim DataSetNumber As Integer = 1

RetrieveData:
Dim ds As DataSet = GetData(DataSetNumber )

For x = 1 to 100

'Assume if there is a "duck" then you go to retrieve data again
If ds.Tables[0].Rows[1]["Animal"] = "duck"
DataSetNumber += 1
Goto RetrieveData
End If
Next x

End Sub

This is a very bad example, as I can see no real use for labels any more,
but it illustrates that you can use it for flow control. In general, you
will find that proper architecture will not lead you to solutions where you
use labels.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
A label can be used as a target for the "goto" or "gosub" statement (which
are generally avoided). It's perhaps also considered sometimes by some
people as a way to "document" things (?).

My personal opinion is that this is basically useless.

Patrice
 
At the risk of revealing my age... It's actually a hold over from a lot
further than VB6.

Can anyone say QuickBASIC (GW-BASIC if you include line numbers in that)
?!?!


Regards
Brian W



Cowboy (Gregory A. Beamer) said:
Labels are a bit of a leftover from VB 6, IMO. The idea is being able to use
Goto statements to go to a specific label. For example:

Public Sub TestGoto()

Dim DataSetNumber As Integer = 1

RetrieveData:
Dim ds As DataSet = GetData(DataSetNumber )

For x = 1 to 100

'Assume if there is a "duck" then you go to retrieve data again
If ds.Tables[0].Rows[1]["Animal"] = "duck"
DataSetNumber += 1
Goto RetrieveData
End If
Next x

End Sub

This is a very bad example, as I can see no real use for labels any more,
but it illustrates that you can use it for flow control. In general, you
will find that proper architecture will not lead you to solutions where you
use labels.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
Andreas Klemt said:
Hello,

for what is the LABEL good when programming in visual studio like this:

Sub xxxx

Label1: xxxx
xxxx
xxx

Label2: xxxxx

End Sub

For what do I need or can use "Label1" and "Label2" in my Visual Studio Code
?

Thanks for any help an answers in advance!
Andreas
 
Back
Top