Results 1 to 10 of 10

Thread: label scope, goto statement question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    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

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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 .

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: label scope, goto statement question

    Use function calls. I don't believe GoTo exists in VB.NET for that matter.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    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:
    1. If True Then
    2.             GoTo xxx
    3.         Else
    4. xxx:        MessageBox.Show("hello")
    5.         End If

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    Re: label scope, goto statement question

    anyone?

  6. #6
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    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

  7. #7
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    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.
    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

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    84

    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'?

  9. #9
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622

    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

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

    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
  •  



Click Here to Expand Forum to Full Width