Results 1 to 5 of 5

Thread: declare and assign variables

  1. #1

    Thread Starter
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    declare and assign variables

    i try to use this, but it doesn't work, for example:

    Code:
    Public Class Form1
         Dim num1 As Integer
         num1 = 0
    End Class
    why that code is error? can somebody explain this?

  2. #2
    Member
    Join Date
    Sep 2012
    Location
    Sweden
    Posts
    44

    Re: declare and assign variables

    You can't change values of variables outside of subs.
    this works:
    Code:
    Public Class Form1
         Dim num1 As Integer = 0
    End Class
    If you want to change the value after you have declared it, you need to do that inside a sub

  3. #3

    Thread Starter
    Lively Member chipp's Avatar
    Join Date
    May 2012
    Posts
    78

    Re: declare and assign variables

    why i can't assign the value after i declare it and why it can only be initialized? is it for avoid the variable changed outside the subroutine (that would probably make some errors?)

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

    Re: declare and assign variables

    Quote Originally Posted by chipp View Post
    why i can't assign the value after i declare it and why it can only be initialized? is it for avoid the variable changed outside the subroutine (that would probably make some errors?)
    Because that's what the authors decided. Given that, even if you could perform a separate assignment, it could only be to a variable that you had just declared anyway, why is it a problem to combine the two? Basically, every line directly in the body of a type has to be a declaration. It can be the declaration of another type, a variable, a property, a method or whatever, but it must be a declaration. If you want to execute code other than as part of a declaration then do it in the type constructor or, if it's a form, in the Load event handler.
    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

  5. #5
    Hyperactive Member Jenova's Avatar
    Join Date
    Feb 2006
    Location
    Googleplex
    Posts
    413

    Re: declare and assign variables

    If you want to assign a value immediately after you have declared the variable why don't you just assign a default value when you declare it.

    Code:
    Dim myNumber As Integer = 0
    If you need to change it again at any point after this then this would only logically be done in a routine, like Erik said.

    Hope this helps out,

    Jenova

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