Results 1 to 6 of 6

Thread: Variable not declared error

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Variable not declared error

    I get an error about my variable not being declared, yet when it runs I get no build error, and my button's backcolor is changed.

    Its easier to see an example.


    SAVE ME!! I'm going to have like 10 of these errors and the lecturers will have my ass on a stick.
    Attached Files Attached Files
    Don't Rate my posts.

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    VB Code:
    1. #Region " Windows Form Designer generated code "
    2.  
    3.     Public Sub New()
    4.         MyBase.New()
    5.         MyVar = Color.Red
    6.  
    7.         'This call is required by the Windows Form Designer.
    8.         InitializeComponent()
    9.  
    10.         'Add any initialization after the InitializeComponent() call
    11.  
    12.     End Sub
    13.  
    14.     'Form overrides dispose to clean up the component list.
    15.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    16.         If disposing Then
    17.             If Not (components Is Nothing) Then
    18.                 components.Dispose()
    19.             End If
    20.         End If
    21.         MyBase.Dispose(disposing)
    22.     End Sub
    23.  
    24.     'Required by the Windows Form Designer
    25.     Private components As System.ComponentModel.IContainer
    26.  
    27.     [B]'NOTE: The following procedure is required by the Windows Form Designer
    28.     'It can be modified using the Windows Form Designer.  
    29.     'Do not modify it using the code editor.[/B]
    30.     Friend WithEvents Button1 As System.Windows.Forms.Button
    31.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    32.         Me.Button1 = New System.Windows.Forms.Button
    33.         Me.SuspendLayout()
    34.         '
    35.         'Button1
    36.         '
    37.         Me.Button1.Location = New System.Drawing.Point(152, 144)
    38.         Me.Button1.Name = "Button1"
    39.         Me.Button1.Size = New System.Drawing.Size(64, 24)
    40.         Me.Button1.TabIndex = 0
    41.         Me.Button1.Text = "asdas"
    42.         Me.Button1.BackColor = MyVar
    43.  
    44.         '
    45.         'Form1
    46.         '
    47.         Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    48.         Me.ClientSize = New System.Drawing.Size(292, 266)
    49.         Me.Controls.Add(Me.Button1)
    50.         Me.Name = "Form1"
    51.         Me.Text = "Form1"
    52.         Me.ResumeLayout(False)
    53.  
    54.     End Sub
    55.  
    56. #End Region

    Why do you need to do it this way? just Do what you need to do in the forms Constructor.

    My guess is the the error is a flaw, or something that wasnt expected my the forms designer because it says explicitly not to change any of the code/
    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

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It's probably because you have your code BEFORE the InitializeComponent call. See the comment below that call:
    'Add any initialization after the InitializeComponent() call

    See if this gets rid of the error:
    VB Code:
    1. Public Sub New()
    2.         MyBase.New()
    3.  
    4.         'This call is required by the Windows Form Designer.
    5.         InitializeComponent()
    6.  
    7.         'Add any initialization after the InitializeComponent() call
    8.         MyVar = Color.Red
    9.  
    10.     End Sub

  4. #4

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Originally posted by Edneeis
    It's probably because you have your code BEFORE the InitializeComponent call. See the comment below that call:
    'Add any initialization after the InitializeComponent() call

    See if this gets rid of the error:
    VB Code:
    1. Public Sub New()
    2.         MyBase.New()
    3.  
    4.         'This call is required by the Windows Form Designer.
    5.         InitializeComponent()
    6.  
    7.         'Add any initialization after the InitializeComponent() call
    8.         MyVar = Color.Red
    9.  
    10.     End Sub
    This seems to have solved the problem.


    And I have no idea why we have to do it like this... it really could be done in form load, but apparently the head of the course is abit of a dick.
    Don't Rate my posts.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    It makes sense that it is in the constructor which gets called when the form gets created.

  6. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    "And I have no idea why we have to do it like this... it really could be done in form load, but apparently the head of the course is abit of a dick. "


    or perhaps your instructor thinks this is the best way to get you to learn what you have learned
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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