By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,404 Members | 1,987 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,404 IT Pros & Developers. It's quick & easy.

One-Dimensional Arrays in VB6

P: 10
I am new to using Visual Basic and am trying to learn something about the 6.0 version. Having a problem in the area of one-dimensional arrays. After I create an array using a Dim statement and declare the values that I want, I assume that if I make another statement later it will replace the value of the original?

Dim strFamily(3) as String


strFamilyArray(1) = "Jeff"
strFamilyArray(2) = "Kay"
strFamilyArray(3) = "Billy"

--- strFamilyArray(2) = "Patrick"


In a similar light does this work using operators or what result should I expect?

Dim curPay(1 to 3) as Currency


curPay(1) = "500"
curPay(2) = "100"
curPay(3) = "300"

---curPay(2) = curPay(2) + 10 ---wasn't sure what value would be in the (2) spot with this statement, or if it would give an error message?

Thank you for your help, if you need any more info let me know~
Jan 22 '08 #1
Share this Question
Share on Google+
2 Replies


kadghar
Expert 100+
P: 1,295
(...)

--- strFamilyArray(2) = "Patrick"


In a similar light does this work using operators or what result should I expect?
(...)
curPay(2) = "100"
(...)
---curPay(2) = curPay(2) + 10 ---wasn't sure what value would be in the (2) spot with this statement, or if it would give an error message?
You should expect:

strFamilyArray(2) = "Patrick"
and
curPay(2) = 110,

It'll read the right part first, so it'll say CurPay(2) + 10 = 100 + 10 = 110 and then asign it to the left part.
You dont have to use " " in numeric values.

Give it a try by yourself.
HTH
Jan 22 '08 #2

Expert 5K+
P: 8,434
...if I make another statement later it will replace the value of the original?
...
curPay(2) = curPay(2) + 10
Yes, you're correct on both counts. An array variable works just the same as any other variable, so placing a value in it replaces what was there before.

And your "+ 10" sample is perfectly correct. In an assignment (=) statement, the right-hand side is evaluated first. So VB will take a copy of the current value from curPay(2), add 10 to this value, then look at the left-hand side to determine where to place the result. The fact that it happens to be a variable which was used in the calculation doesn't mean anything.

One thing I would point out. This line...
curPay(1) = "500"
while it may compile and work alright, is not technically correct. Numeric values do not use delimiters (quotes). This should be written as
curPay(1) = 500
In your version, VB has to do extra work at runtime to take the string value you've provided and convert it to a numeric value. It may not be a huge difference, but let's consider, for example, if you made a typo...
curPay(1) = "500x"
This will still compile alright. Any string is much like another, the compiler doesn't care what's in it. But when you actually try to execute that statement, you will hit a Type mismatch error.
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.