|
-
May 5th, 2005, 08:09 PM
#1
Thread Starter
Lively Member
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 ,
John
-
May 5th, 2005, 10:20 PM
#2
Sleep mode
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 .
-
May 7th, 2005, 02:44 AM
#3
Re: label scope, goto statement question
Use function calls. I don't believe GoTo exists in VB.NET for that matter.
-
May 10th, 2005, 06:16 PM
#4
Thread Starter
Lively Member
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
-
May 15th, 2005, 01:59 PM
#5
Thread Starter
Lively Member
Re: label scope, goto statement question
-
May 15th, 2005, 02:11 PM
#6
Frenzied Member
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 
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
-
May 20th, 2005, 12:22 AM
#7
Re: label scope, goto statement question
 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.
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
May 20th, 2005, 01:51 PM
#8
Thread Starter
Lively Member
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'?
-
May 20th, 2005, 04:59 PM
#9
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
}
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
May 21st, 2005, 04:28 PM
#10
PowerPoster
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();
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|