label scope, goto statement question
hi, i have come from vb6 and then to vb.net and then to C#.
i understand why you should not be able to jump into for loops and such with loop counters, but what about if {} statements? For example, the following code:
Code:
if(something1) {
action1:
// do stuff
} else if(something2) {
action2:
if(flag) goto action1;
// do stuff ...
}
does not work. Is there some hack around this or what would break if the compliler allowed this to happen?
Many thanks in advance :bigyello: ,
John
Re: label scope, goto statement question
Use function calls. I don't believe GoTo exists in VB.NET for that matter.
Re: label scope, goto statement question
i cannot wrap my code in a subroutine because i have about 20 local variables i need to access. also, i do not have any loops, sorry if that was unclear, i just have an if{} statement. Also, GoTo does exist in VB.NET, this code does what i would like to do in C#:
VB Code:
If True Then
GoTo xxx
Else
xxx: MessageBox.Show("hello")
End If
Re: label scope, goto statement question
Re: label scope, goto statement question
IMHO, stay away from the goto statement, unless you really need it. It's considered bad practice, but only becuase it's misused. In the case you describe, I'd say it's misused :)
Quote:
i cannot wrap my code in a subroutine because i have about 20 local variables i need to access
Just pass the arguments into the method you're calling.
Mike
Re: label scope, goto statement question
Quote:
Originally Posted by zildjohn01
i cannot wrap my code in a subroutine because i have about 20 local variables i need to access.
You probably need to restructure your code.
Maybe if we could see the entire method and what its doing we could suggest an alternative.
Re: label scope, goto statement question
well, my code is to draw a midi music note in a list box. if the event is between 0x80 and 0x8F the note is turning off. if the event is between 0x90 and 0x9F the note is turning on. BUT if the event is 'note on' and the 'volume' field = 0 then the note is actually turning off. my code is basically like this:
Code:
if(event.Data[0] <= 0x8F) {
drawNoteOff:
// draw note off data
} else if(event.Data[0] <= 0x9F) {
if(event.Data[2] == 0)
goto drawNoteOff;
// draw note on data;
} else if // ...
maybe there is some kind of hard JMP statement or a unsafe 'goto'?
Re: label scope, goto statement question
Well.... I assume event.Data array is the information you need to draw your notes so ....
Code:
//..
if(event.Data[0] <= 0x8F) {
DrawNoteOff(event.Data);
} else if(event.Data[0] <= 0x9F) {
if(event.Data[2] == 0)
DrawNoteOff(event.Data);
} else if // ...
//...
private static void DrawOnNote(byte[] Data);
{
// Handle On Note Drawing Here
}
private static void DrawOffNote(byte[] Data);
{
// Handle Off note drawing Here
}
Re: label scope, goto statement question
I think you should definately restructure your code. How do you ever troubleshoot that...
I have no idea what exactly you are trying to accomplish, but if you have multiple conditions, why don't you do something like this:
Code:
if(event.Data[0] <= 0x8F || (event.Data[0] <= 0x9F && event.Data[2] == 0)) {
drawNoteOff();
}