Results 1 to 8 of 8

Thread: [RESOLVED] Try/Catch

  1. #1

    Thread Starter
    Member ZenCAD1960's Avatar
    Join Date
    Jan 2008
    Posts
    33

    Resolved [RESOLVED] Try/Catch

    ok can I....

    Try

    Something

    Catch

    "I want to call a private sub here"

    end try

    can this be done?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Try/Catch

    Of course it can be done

    VB.Net Code:
    1. Try
    2. 'Some code that could throw an exception
    3. Catch Ex As Exception
    4. CallSomeSub(Ex)
    5. End Try
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member ZenCAD1960's Avatar
    Join Date
    Jan 2008
    Posts
    33

    Re: Try/Catch

    yeah will I get an error when I do this..here is the code...

    Code:
        Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim arrPan As Panel() = {Panel3, Panel4, Panel5, Panel6}
                Panel2.Top = 25
                Panel2.Dock = DockStyle.Bottom
                arrPan(panState).Dock = DockStyle.None
                arrPan(panState).Top = 2000
                panState = panState + 1
                arrPan(panState).Top = 25
                arrPan(panState).Dock = DockStyle.Fill
            Catch ex As Exception
                Call Panel_Rest(ex) '<--------  Problem Line
                'panState = Nothing
                '   Panel1.Visible = True
                '  Panel2.Dock = DockStyle.None
                ' Panel2.Top = 2000
            End Try
        End Sub
        Public Sub Panel_Rest(ByVal sender As System.Object, ByVal e As System.EventArgs)
            panState = Nothing
            Panel1.Visible = True
            Panel2.Dock = DockStyle.None
            Panel2.Top = 2000
        End Sub
    help would be great benifitial to my mental health! LOL

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Try/Catch

    You're not matching the methods signature.
    VB.Net Code:
    1. Panel_Rest(ByVal sender As System.Object, ByVal e As System.EventArgs)
    As you can see, it expects two arguments, an object and an instance of the EventArgs class.
    This method looks like an eventhandler and they should not be called directly. Altough IF you where to call this directly you must pass an object and an eventargs instance to it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Member ZenCAD1960's Avatar
    Join Date
    Jan 2008
    Posts
    33

    Re: Try/Catch

    ok I got it. I am not sure what but when I deleted the ByVal items in the Private Sub Panel_Rest line, it now works.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Try/Catch

    That Panel_Rest method has the signature of an event handler. If you're not using it to handle any events then what are those arguments for? Also, why are you passing the Exception object when you call it if you aren't using it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member ZenCAD1960's Avatar
    Join Date
    Jan 2008
    Posts
    33

    Re: [RESOLVED] Try/Catch

    Quote Originally Posted by jmcilhinney
    That Panel_Rest method has the signature of an event handler. If you're not using it to handle any events then what are those arguments for? Also, why are you passing the Exception object when you call it if you aren't using it?
    I will have a few subs calling the Reset_Panel sub. It sort of resets the form with the beginning panels I have setup. Button One click event has an array of 4 panels. There will be like 4 more button click evens with as much as 15 panels in an array. Once the user sees these via the code in the click event, when the get past the '.length' it would create an exception, at which point I send that to the Reset to reset the panel back to the original where they can choose anothe button with different panels.

    I am a noob here. I remember this kind of structure from BV6 many years ago although new to VB.Net and the try/catch argument. Can this be done another way to simplify the code?

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Try/Catch

    I don't really understand your explanation but basically you shouldn't be calling event handlers directly from code. If you want to execute the same code from multiple places then put it in its own method and call that method from multiple places.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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