+ Reply to Thread
Results 1 to 4 of 4

Thread: VB.NET from beginner to advanced programmer Part 3 - Variables

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    VB.NET from beginner to advanced programmer Part 3 - Variables

    Welcome to the VB.NET tutorial series: "VB.NET from beginner to advanced programmer" which will take you from the very beginning to be a good programmer. VB.NET is a good first language for new programmers so this 21 part long series is written for completely beginners but it will also works perfectly fine if you already know another programming language.


    VB.NET from beginner to advanced programmer
    1. Introduction and Installation
    2. Objects and Events
    3. Variables
    4. The basic data types
    5. Logical Operators
    6. Relational Operators
    7. If statements Then
    8. Arithmetical Operators
    9. Loops Part 1
    10. Arrays
    11. Loops Part 2
    12. Try Catch statements
    13. Subs and Functions
    14. Difference between Scopes
    15. Select Statements
    16. Multidimensional arrays
    17. Structures
    18. Classes
    19. Enumerations
    20. Advanced Comments
    21. Compiling Directives




    So now I will teach you about something called variables and constants, we'll start with variables.

    Variables


    Variables are places where you can store values and then at anytime go and check what value that is in there. We can compare it with a box where we can store some papers with information.



    Declaration:

    In VB.NET you need(like in most programming languages) declare the variable first. If we uses our box model, it's the same as creating the box. We can't put something in a box that doesn't exists.

    This is the syntax for declaring a variable:

    Code:
    <scope> <variable_name> As [New] <type>

    <scope>
    It is from where we can access the variable, this is more deeply covered in a later part.


    <variable name>
    It is the name of the variable, this should have the right prefix depending on variable type so you know what it is. For example if the type is integer we should add "n" at the beginning.


    (New)
    It's optional to add "New" here. If you do, the variable's value will be a new instance of the variable's type, or else the variable will just be empty until we give it a value. All types can't use the "New" option.


    <type>
    This is the type of the variable, this means what sort of values which can be stored in the variable, integer and date is an example of two different types.




    Here comes an example:

    Code:
    Dim nInput as Integer
    Dim is the local scope, it's used inside event block and sub/functions.
    nInput is the name, the n is so we know it's an integer but it doesn't have any effect really. We could name a variable nInput even though it's not an integer but do not do that.
    And at the end we make it the type Integer. This means we can store Integer values in it.


    Look at this example:

    Code:
    Dim nInput
    Here we didn't declared what type it was. Then it automatically gets the type Object. The object type can store any value. Remember it's always better to set the variable type if you know what values that should be stored in it. If you have the type object it needs to be able to store everything and could therefor take more space even though it's empty.

    The above code should actually got the prefix "o" since it's an object:

    Code:
    Dim oInput










    It's also possible to declare more then one variable at the time by using commas, you can set the type of all variables one by one or in groups:


    One by one:
    Code:
    Dim a As Integer, b As Single, c As Double
    a will be declared as an Integer, b as Single and c as double.


    In a group:
    Code:
    Dim a, b, c As Integer
    Here all three variables will be declared as Integers.


    You can do a little like you want, as you can see in this example:
    Code:
    Dim a As Double, b, c As Integer, d, e, f As Date, g
    a will be a Double.
    b and c will be Integers
    d, e and f will be declared as Dates
    and g will be an Object since we haven't wrote its type.




    Set and get Values

    When we want to store a value in the variable we write the variable name an equals to sign and the the value:

    Code:
    <variable_name>=<value>

    It could look like this:

    Code:
    nRooms=6
    In the above example the variable nRooms gets the value of 6.

    It's also possible to assign a value to a variable when it's created, like this:
    Code:
    Dim nRooms as Integer = 6



    To get the value from a variable we just simply writes its name where we want to access the value of it. Here's an example:

    Code:
    Messagebox.Show(sMessage)
    In the above example a messagebox will be showed with the value from the string variable called sMessage.




    Don't get too confused when you got a variable on both sides of an equals sign:

    Code:
    nValue1=nValue2
    REMEMBER: The variable is to the left and the value to the right. In this case the variable called nValue1 will get the same value as nValue2.



    Constants

    Constants are like variables but they can not change their value, you set it when you declare the constant. In our box model it would be like building a glass box without a hole around some papers, we cannot add or change what's in it but we can see everything through the glass.

    Here's an example:

    Code:
    Const Pi as single = 3.14


    See you in Part 4, then we will learn us about some basic data types.
    Last edited by Vswe; 03-21-2010 at 03:24 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: VB.NET from beginner to advanced programmer Part 3 - Variables

    Very nice. You may want to talk about how and where variables store information in memory (unless that is already in a later part). +rep

  4. #3
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: VB.NET from beginner to advanced programmer Part 3 - Variables

    I don't think I've mentioned that. You'll probably find a few things missing, it's hard to remember to write about everything.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: VB.NET from beginner to advanced programmer Part 3 - Variables

    I'd seen Dim a lot, but didn't know it defined a scope. Nice. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 3
    Last Post: 11-02-2009, 05:37 PM
  2. Replies: 1
    Last Post: 11-02-2009, 06:05 AM
  3. Replies: 1
    Last Post: 11-02-2009, 05:46 AM
  4. VB.NET from beginner to advanced programmer Part 10 - Arrays
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-02-2009, 05:45 AM
  5. Replies: 1
    Last Post: 11-02-2009, 05:43 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts