Results 1 to 3 of 3

Thread: General Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2000
    Location
    México, D.F.
    Posts
    64

    Red face

    I'm learning VB, how can I declare and use arrays of variables.




  2. #2
    Member
    Join Date
    Aug 2000
    Posts
    60

    Cool

    Hi there, newbie

    In VB you do not HAVE to declare variables, but it is highly recommended that you do.

    Here's how you do it, in the sub or function. i.e.
    Private Sub Form_Load()

    you use the DIM statement to declare any variables that you are planning on using, i.e.

    If you want to count from 1 to 400 then you would declare a variable to store the number that the counter was on, so you would declare a variable, say CounterPosition and the data type would need to be either a long or an integer.

    Code:

    Dim CounterPosition As Long

    that is it, simple hey.

    Arrays are a variable with many parts to it that can hold seperate bits on information. If you have 5 Computers with and you wanted to store in a variable how much ram each of them had, you would use an array. To declare an array you do the same as if you were declaring a regular variable but this time you need to say how many items you want to store in it.

    Code:

    Dim AmountOfRam(4) As Long


    Because the base of an array is 0 (i.e it starts at 0) then number or items in the array that we need it our highest number i.e 5 minus 1 = 4.

    Now you have declared it, heres how to use it.

    To set the value for the first computer use the code below:

    Code:
    AmountOfRam(0) = 32


    for the second
    Code:
    AmountOfRam(1) = 64


    You get the idea, and to retieve the data for machine 1
    Code:

    MsgBox AmountOfRam(0)

    This would display 32 in a message box


    Hope this is some help to you, if you want any more help just contact me.

    Grant French
    -----------------------------------------------
    E-Mail: [email protected]
    ICQ: 33122184

  3. #3
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Lightbulb

    gfrench said only something about the DIM statement, but there are some other ways too.
    You can also you the PUBLIC statement in the declaration section of the form. like this:

    Code:
    Option Explicit
    PUBLIC CounterPosition as Long
    
    Private Sub Form_Load()
    [And you go on...]
    The interesting thing of that is that the variable is declared for the whole form. When you use the DIM statement, and you put some data in the variable, then you will lose the data when you go out of that SUB:

    Code:
    Private Sub Form_Load()
    
    DIM CounterPosition as Long
    CounterPosition = 45
    Msgbox CounterPosition
    
    End Sub
    
    Private Sub Command1_Load()
    
    MsgBox CounterPosition
    
    End Sub
    In the first sub the messagebox will show the number 45, but in the second one, it will be 0 because you used the DIM statement and it loses his data when you close the sub

    When you use the public Statement:

    Code:
    Public CounterPosition as long
    
    Private Sub Form_Load()
    
    CounterPosition=45
    MsgBox CounterPosition
    
    End Sub
    
    
    Private Sub Commmand1_Click
    
    Msgbox CounterPosition
    
    End Sub
    Now, in the second SUB, counterposition will still be 45, because you publict him for the whole form.

    PS: when you want to declare a variable for your whole project, you have to use the public statement too, but you have to write your declaration in a Module

    I hope you understood this,
    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

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