Results 1 to 15 of 15

Thread: Declared array globally

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Declared array globally

    Why I can't I do the following and get it working?
    Dim intNumbRank As Integer = CInt(txtRank.Text())

    Dim aryRank(intNumbRank) As Integer
    I want to declare the Array as global for the Class or Form so I can use the array any where on the form. But I always get an blue underline under the variable intNumbRank inside the parenthesis. Please help me correct my code.

    Many thanks in advance!

    Chong

  2. #2
    Member JesusFreak's Avatar
    Join Date
    Feb 2003
    Location
    Headed for Heaven
    Posts
    57
    Try Declearing the Variable outside of the sub:


    Code:
    Dim myVariable as Integer
    
    Sub myCode
    
    End Sub
    That if you confess with your mouth, "Jesus is Lord," and believe in your heart that God raised him from the dead, you will be saved.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    The code was declared right below the Public Class Main where all my global variables are delcared. But somehow this array doesn't work.

    Chong

  4. #4
    Lively Member
    Join Date
    Feb 2003
    Location
    UK
    Posts
    95
    if you want to declare a variable that can be used across the class declare it as a member variable of that class. It can then be referred to anywhere in the class.


    i'd say don't use any global variables in your app, but that would no doubt cause a heated debate among others.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    How would I declare the variable as a member of the class?

    Thanks!

    Chong

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You can't have this as a general declaration:

    Dim intNumbRank As Integer = CInt(txtRank.Text())

    Because the value of txtRank.Text can't not be determined at that time. txtRank is not even loaded yet and you are trying to use its value. You'll have to use a dynamic array and set it at runtime to the number of elements you need.

  7. #7
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    VB Code:
    1. Private intNumbRank As Integer = CInt("3")
    2. Private aryRank() As Integer

    Then in a sub:
    VB Code:
    1. ReDim aryRank(intNumbRank)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219
    First, how do I set the dynamic array to a variable at run time?

    Second, I did try this:
    Dim intNumbRank As Integer = 0
    Dim aryRank(intNumbRank) As Integer
    but I still have the same problem.

    Chong

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You would declare it as a dynamic array in the general section:

    VB Code:
    1. Dim intNumbRank As Integer = 0
    2. Dim aryRank() As Integer

    Then at runtime you would set the elements using redim as VBCrazyCoder showed:

    VB Code:
    1. intNumbRank=intNumbRank.Parse(txtRank.Text)
    2. ReDim aryRank(intNumbRank)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    Yes, I see that this method will work too. Thanks everyone for helping me out.

    Chong

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question Dynamic array

    I just encounter another question regarding array. If I declared the array as:

    Dim aryRank() as Integer

    above the Windows Form Designer generated code and never ReDim the array in my Private Sub routine, would I be able to store data in the aryRank() array dynamically? For example, would I be able to do this:

    Dim i as Integer

    Private sub here()
    intSSN = Cint(EduRow("SSN"))
    aryRank(i) = intSSN
    i += 1
    End Sub

    I receive this error Object reference not set to an instance of an object when the program went pass the code aryRank(i) = intSSN in the program. What is this mean?

    I didn't want to ReDim because I don't know how many values will be stored in the array.

    Thanks!

    Chong

  12. #12
    Fanatic Member VBCrazyCoder's Avatar
    Join Date
    Apr 2003
    Posts
    681
    If you don't redim, then you need to specify the size of the array when it is declared, and therefore cannot dynamically set it's size.

    You might want to use an array list instead of an array, so that you can just add whatever you need to it and let it grow dynamically, or specify it's initial capacity when you create it (one of the agruments in it's constructor). This may suit your needs better.

    You would have:
    VB Code:
    1. Private arrlist As New ArrayList(10)
    2.  
    3. Public Sub mySub()
    4.     arrlist.Add("myString")
    5. End Sub

    Hope this helps!

  13. #13
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    You have to set the elements using redim. If you don't know how many values then you can keep rediming higher values, although if you want to append data or keep what is already in the array you need to add the preserve keyword. You can just increment it by one right before you add a new element.

    Also you can use an ArrayList instead add it will let you add and remove objects much like a collection would but without the key to index them by.

    Boy a guy goes to get a drink of water and someone beats him to the post. I knew I should have posted before I got up. Although you included code so yours is better than all mine.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Thumbs up

    Thanks! I saw that they are array and arry list but I wasn't sure the different is between them. Yes, perhaps array list will be more suited for me. I need to grow and shrink dynamically. However, what can I and can't I store in the array list?

    Chong

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Feb 2003
    Posts
    219

    Question

    With array list, will I be able to use the Array.Sort() function? Because once I finished storing the values, I want to sort the values from highest to lowest value.

    Chong

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