PDA

Click to See Complete Forum and Search --> : label scope, goto statement question


zildjohn01
May 5th, 2005, 08:09 PM
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:


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

Pirate
May 5th, 2005, 10:20 PM
Can't you wrap the "Action1" in a method and call it whenever you like ?? I think this is the best way other than using [break or continue] in your loop which might not fit in your set up .

mendhak
May 7th, 2005, 02:44 AM
Use function calls. I don't believe GoTo exists in VB.NET for that matter.

zildjohn01
May 10th, 2005, 06:16 PM
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#:
If True Then
GoTo xxx
Else
xxx: MessageBox.Show("hello")
End If

zildjohn01
May 15th, 2005, 01:59 PM
anyone?

Mike Hildner
May 15th, 2005, 02:11 PM
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 :)

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

<ABX
May 20th, 2005, 12:22 AM
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.

zildjohn01
May 20th, 2005, 01:51 PM
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:
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'?

<ABX
May 20th, 2005, 04:59 PM
Well.... I assume event.Data array is the information you need to draw your notes so ....



//..

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
}

hellswraith
May 21st, 2005, 04:28 PM
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:

if(event.Data[0] <= 0x8F || (event.Data[0] <= 0x9F && event.Data[2] == 0)) {
drawNoteOff();
}