PDA

View Full Version : [SOLVED] Effect of "Else:"



Cyberdude
06-25-2005, 01:35 PM
I have the code:

If "A" = "B" _
Then Msgbox "A=B" _
Else Msgbox "A<>B"

then I decide to add a line:
I
f "A" = "B" _
Then
Msgbox "A=B"
Msgbox "More stuff"
Else: Msgbox "A<>B"
End If

Because I forgot to move the Msgbox following the Else to the next line, VBE adds a colon following the Else.
My question is, what is the effect of the added colon? I tried to set up a test to determine the answer, but everything I tried indicated that the colon has no effect.
I'm missing something, but just can't figure it out. It looks like the Else has been converted to a label. :dunno

Norie
06-25-2005, 01:52 PM
Cyberdude

I think the main effect of the colon is to make the code harder to understand and the flow of the code harder to follow.

Andy Pope
06-25-2005, 02:15 PM
Hi,

You went from single line syntax to block form syntax. Therefore the code following the Else and before the End If needed to be converted to block form. The colon does this.
You could have stay in single line form by placing a colon between your 2 msgbox statements.


If "A" = "A" Then MsgBox "A=A": MsgBox "More stuff" Else MsgBox "A<>A"
I'm with Norie on this one though, I find using and then later reading colons to terminate multiple commands on a single line confusing.

johnske
06-25-2005, 02:21 PM
Hi Cyber,

Effectively, the colon means "end of line" ... so whatever is written after a colon COULD (if you choose) be written on a new line.

Regards,
John :)

Cyberdude
06-25-2005, 03:30 PM
Well, that explains why my tests told me nothing. Still, one wonders why the VBE writers chose to add the colon under these circumstances. Why not trigger an error??
Thanks, gang.

Bob Phillips
06-25-2005, 03:43 PM
Well, that explains why my tests told me nothing. Still, one wonders why the VBE writers chose to add the colon under these circumstances. Why not trigger an error??
Thanks, gang.

Because it is forgiving and is trying to work with you.

Similalrly, if you type Endif, it converts it to End If, and adds a quotes at the end of the line if there is one missing,

It's a feature, quite a useful one for once.

Cyberdude
06-25-2005, 08:24 PM
Hard to believe, xld, but you're probably right.
Thanx for the insight.