Results 1 to 13 of 13

Thread: Ignore errors in Private Sub

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Ignore errors in Private Sub

    My Code Is:

    Code:
    Public Class Form1
        Dim FreeSpace As String = "Panel16.Location"
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim button As String = "button1.location"
            Dim panel As String = "panel1.location"
            Call MoveButton()
        End Sub
    
        Private Sub MoveButton()
            button = FreeSpace
        End Sub
    End Class
    I get an error in my private sub named "MoveButton" because "button" is not declared. I didn't Dim "button" in that sub because when I use the Call command in another sub, I want to use what "button" is "dim" there. How do i ignore the error in the private sub "MoveButton"? Thanks..

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Ignore errors in Private Sub

    You don't ever ignore an error. It means you've got it wrong. It won't run or if it does it'll either crash or worse!

    Firstly, don't use variable names that make no sense. You shouldn't use 'button' and 'panel' in the first place because it's confusing enough when the Type name is the same as one of your variables but to use it for something that isn't in fact a button or a panel is just plain silly.

    Secondly the value of any variable declared within a Sub is available to that Sub only. Them's the rules and you just can't ignore them.

    And finally, even if everything does run all this program does is to assign a String Variable with a value which could be achieved in one line ...

    vb.net Code:
    1. Dim button As String = "Panel16.Location"

    ... and is completely without function. You might just as well make button = "jam on toast" or "I Heart NY"

    I'm not going to write this for you because frankly if you don't understand what's wrong there's really no point in your continuing with VB. I will give you a hint, however, ....

    vb.net Code:
    1. Private Sub MoveButton() 'those parentheses look terribly empty!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Ignore errors in Private Sub

    Yes, you should not ignore errors, errors mean that somethign is wrong and ignoring them will not make them go away.

    If you need to dim an object in one routine and use that same object in another routine then the common way is to pass a reference to the object as a parameter to the routine where you need to use it. The other method is to Dim the object at the top of the code so as to make it visible to the entire class but this method should be used only when the logic requires it. Passing as a parameter will work in most cases.

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Ignore errors in Private Sub

    I didn't Dim "button" in that sub because when I use the Call command in another sub, I want to use what "button" is "dim" there.
    To achieve that, you have to pass that variable ByRef not ByVal, Ex
    Code:
        Private Sub Sub1()
            Dim strButton As String = "Initial value"
            Call Sub2(strButton)
            MessageBox.Show(strButton) ' this will show "A new value assigned in the Sub2" because the variable passed ByRef to the Sub2
        End Sub
    
        Private Sub Sub2(ByRef v As string)' if you replace ByRef with ByVal, the value of strButton will not changed in he caller sub (Sub1)
           v = "A new value assigned in the Sub2"
        End Sub



  5. #5
    Fanatic Member
    Join Date
    Dec 2007
    Location
    West Yorkshire, UK
    Posts
    791

    Re: Ignore errors in Private Sub

    Quote Originally Posted by dunfiddlin View Post
    vb.net Code:
    1. Private Sub MoveButton() 'those parentheses look terribly empty!
    I see what you are doing there Dunfiddlin, and of course, I agree with your methods. However, you have already noted that the OP has very little idea of programming VB. A further clue such as
    ByVal/ByRef??????
    would be better to give them a clue for what to search Google for.
    EricJohanssen, I hope you follow Dunfiddlin's clue and my expansion of it and do not just use 4x2y's code, say "Wow, it works!" and leave it at that. Investigate ByVal and ByRef and see how and why they work the way they do.
    As an alternative, you can define the variable button (It has already been mentioned another name would be much MUCH better) outside the sub as a global variable. It would then be available to all the subs in the class.

  6. #6
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Ignore errors in Private Sub

    Quote Originally Posted by españolito View Post
    do not just use 4x2y's code, say "Wow, it works!" and leave it at that. Investigate ByVal and ByRef and see how and why they work the way they do.
    The example i gave uses generic code and sub names not his original names in order to not just Copy & Paste



  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Ignore errors in Private Sub

    Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?

    @Op

    What are you really trying to do ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: Ignore errors in Private Sub

    Quote Originally Posted by Niya View Post
    Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?

    @Op

    What are you really trying to do ?
    I was trying to make a slider puzzle game, but the way I was planning it in my head was probably a really bad and hard way of doing it. Firstly, the "panel" isn't really a panel, it's a picture box. The reason I can't define the button and "panel" above the class is because I want to use the call event in the click events for every buttons(1–8) and in each sub and define what "button" and "panel " means. So for button1, when using the call event, "button" should be "translated" to "button1. Location"... And "FreeSpace" Would be the location where a button could be moved.

    If someone could help me with this then maybe my silly idea will work, and if it's a completely retarded idea, please tell me

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Ignore errors in Private Sub

    You can use the same sub to handle click event for various button then use sender parameter to know which button fires the event, Ex
    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click ' Add up to 8
            Dim btnSender As Button = DirectCast(sender, Button)
    
            MoveButton(btnSender)
        End Sub
    
    
        Private Sub MoveButton(ByVal btn As Button)
            MessageBox.Show("You have clicked on the " & btn.Text)
            btn.Left = btn.Left + 4
        End Sub



  10. #10
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Ignore errors in Private Sub

    Double post...



  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Ignore errors in Private Sub

    Quote Originally Posted by Niya View Post
    Why has no one mentioned the fact that he is trying to move a button to occupy the space of a panel by manipulating strings ?
    Ahem ....

    and is completely without function. You might just as well make button = "jam on toast" or "I Heart NY"
    Just saying is all!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: Ignore errors in Private Sub

    Your crafty use of the English language takes getting used to
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  13. #13
    Junior Member
    Join Date
    Sep 2012
    Posts
    17

    Re: Ignore errors in Private Sub

    Thank you

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