|
-
Apr 17th, 2003, 09:55 AM
#1
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 10:00 AM
#2
Member
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.
-
Apr 17th, 2003, 10:07 AM
#3
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 10:11 AM
#4
Lively Member
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.
-
Apr 17th, 2003, 10:16 AM
#5
Thread Starter
Addicted Member
How would I declare the variable as a member of the class?
Thanks!
Chong
-
Apr 17th, 2003, 10:20 AM
#6
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.
-
Apr 17th, 2003, 10:25 AM
#7
Fanatic Member
VB Code:
Private intNumbRank As Integer = CInt("3")
Private aryRank() As Integer
Then in a sub:
VB Code:
ReDim aryRank(intNumbRank)
-
Apr 17th, 2003, 10:33 AM
#8
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 10:45 AM
#9
You would declare it as a dynamic array in the general section:
VB Code:
Dim intNumbRank As Integer = 0
Dim aryRank() As Integer
Then at runtime you would set the elements using redim as VBCrazyCoder showed:
VB Code:
intNumbRank=intNumbRank.Parse(txtRank.Text)
ReDim aryRank(intNumbRank)
-
Apr 17th, 2003, 10:49 AM
#10
Thread Starter
Addicted Member
Yes, I see that this method will work too. Thanks everyone for helping me out.
Chong
-
Apr 17th, 2003, 02:35 PM
#11
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 02:48 PM
#12
Fanatic Member
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:
Private arrlist As New ArrayList(10)
Public Sub mySub()
arrlist.Add("myString")
End Sub
Hope this helps!
-
Apr 17th, 2003, 02:53 PM
#13
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.
-
Apr 17th, 2003, 02:53 PM
#14
Thread Starter
Addicted Member
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
-
Apr 17th, 2003, 02:56 PM
#15
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|