CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20

Thread: Form1 Error

  1. #16
    Join Date
    Apr 2007
    Posts
    43

    Re: Form1 Error

    That didn't help I'm afraid... Thanks Any way.

    Any more suggestions?

    Thanks in Advance,

    Tim

  2. #17
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Form1 Error

    Looking at the code carefully, I have seen that you are assigning values to the variables outside of any code block. Which is not a good practice and is not supported by VB.

    Do not assign anything to any variables in the declaration part of your code. Change the following two lines
    Code:
    Dim changeamount As Integer = amountbox.Text
    
    Dim keypad As Boolean = True
    To
    Code:
    Dim changeamount As Integer
    
    Dim keypad As Boolean
    This code is in Form1.

    Make sure that you don't assign anything to the variables that are created outside of the functions/subs/properties.

  3. #18
    Join Date
    Apr 2007
    Posts
    43

    Re: Form1 Error

    I'm only in year 10 learning this. This assignment is due VERY Soon, i need help with the form1 error.

    The previous suggestion didn't help

    Thanks,

    Tim

  4. #19
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Form1 Error

    Quote Originally Posted by PCgeek
    I'm only in year 10 learning this. This assignment is due VERY Soon, i need help with the form1 error.

    The previous suggestion didn't help

    Thanks,

    Tim
    Did you try the suggestions mentioned in my previous post?

  5. #20
    Join Date
    Nov 2005
    Location
    Omaha, Nebraska, USA
    Posts
    696

    Re: Form1 Error

    aniskhan's and Shuja Ali's comments highlight and solve your problem.

    When the program creates Form1, it goes into the Sub New, which is defined as:
    Code:
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
        End Sub
    Calling MyBase.New() makes it go through any variables that are declared with values.
    Calling InitializeComponent() creates all the controls for the form.

    The fact that they are called in that order means that MyBase.New() executes the line:
    Code:
    Dim changeamount As Integer = amountbox.Text
    But amountbox is not yet created. InitializeComponent() is what creates it.

    That's why you're getting the error.

    So, if you wanted changeamount to start with the value in that textbox, you'd have to change:
    Code:
        Dim changeamount As Integer = amountbox.Text
    To:
    Code:
        Dim changeamount As Integer
    Then put "changeamount = amountbox.Text" in Sub New, after the InitializeComponent() call. But since that textbox's text property is defaulted to a blank string, that would still give you an error, so just don't set changeamount.

    Only changing that one line of code allowed it to run for me.

Page 2 of 2 FirstFirst 12

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

Featured