 |
| Beginning VB 6 For coders who are new to Visual Basic, working in VB version 6 (not .NET). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Beginning VB 6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

January 5th, 2005, 03:11 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by BrianWren
There is no way to have a constant array. You can have an array that you [u]use</u> as an array of constants, but the program will be willing to change the values. (You will get an error message anytime you try to change the value of an item declared as a constant.)
|
But I was not planning to change that value.
Quote:
|
quote:What is your declaration for vari?
|
As I wrote in my previous post, it's Array(1..9,1..9). :)
===================
Well, thank you for the explanations! :) I'm wondering why you're still replying...
Apparently it's not a good idea to mix idea of a constant and value, as I'm doing. You take it as a constant, which is a particular defined object in the programming language and have specific properties and application.
All the time I was talking about a value, that is, how to correctly write a value of a specific type of data. Particularly a value of 2-dimentional array.
When you write
Code:
Dim s As String
s = "AbcDe"
, with this operation you set a value for a variable. The value itself ("AbcDe") from [u]my</u> point of view is also a constant. Not the VB constant which you specifically define as the language object Constant, but simply a normal constant, a specific value.
I wanted to know, how to correctly write a value of type Array (1..9, 1..9), so I can to assigne it (the value) to my variable (of type Array (1..9, 1..9)).
As I see it, the mistake must be that I use the type I use. I should use simply Variant instead. I'll tell you what happened, when I try this.
Thanks,
Janis
|
|

January 5th, 2005, 03:56 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
OK
|
|

January 6th, 2005, 07:14 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well, I don't know if this works, because, when I wrote
Code:
Dim vari As Variant
instead of
Code:
Dim vari(1 To 9, 1 To 9) As Byte
,
then I get TypeMismatch error on the line
Janis
|
|

January 6th, 2005, 08:19 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
When you first create a variant, it is initialized to NULL.
If you try to address that as an array, VB has no way to assess the indices.
But, if you have the statement
Code:
vari = Array( _
Array(4, 0, 0, 0, 0, 0, 0, 0, 2), _
Array(0, 0, 0, 7, 0, 0, 0, 0, 0), _
Array(5, 1, 6, 4, 0, 0, 0, 9, 0), _
Array(0, 0, 0, 5, 0, 8, 0, 0, 0), _
Array(0, 0, 7, 0, 0, 0, 9, 0, 0), _
Array(8, 0, 0, 0, 0, 2, 0, 0, 5), _
Array(7, 2, 1, 9, 0, 0, 5, 0, 6), _
Array(0, 0, 0, 2, 0, 0, 0, 0, 0), _
Array(6, 0, 0, 0, 0, 0, 0, 0, 7) _
)
the Array function transforms vari into a data type called Variant(array). Then VB does know how to interpret the indices.
After having run that assignment using Array( Array( , , , ...), ...), the statement â vari(i, j) = 0 â should work, as long as i <= 8, and j <= 8. (If higher or lower, you will get a subscript out of range for this particular array.)
BTW: If you are using Option Base 1 in the module, that would be <= 9 . . .
|
|

January 7th, 2005, 11:51 AM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by BrianWren
the Array function transforms vari into a data type called Variant(array). Then VB does know how to interpret the indices.
|
So, you're saying that I should at first define it as Variant(array(1..9,1..9)) rather than Variant?
Janis
|
|

January 7th, 2005, 01:42 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
No. There are 2 steps. (In VB.NET you can declare a variable and fill it in one step. Thatâs a welcome addition indeed!)
Code:
Dim v As Variant ' Step 1. Now you have a Variant
' to receive the array
v = Array(Array(...), Array(...)) ' Step 2. Create the array.
v(1, 5) = 15 ' Now you can [u]address</u> your array.
BTW: in JavaScript, once you create an array, merely addressing any element causes the array to be sized large enough to have that element. If you make a 3 element array, and then have the statement abc = vArr[50], you will now have a 51 element array; no âSubscript out of rangeâ error. Can introduce some perplexing erroneous behavior...
|
|

January 7th, 2005, 04:03 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
ok,
I defined it on the beginning of the script.
Code:
Dim vari As Variant
Then inside procedure Form_Load() I assigned the array.
Code:
vari = Array( _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0), _
Array(0, 0, 0, 0, 0, 0, 0, 0, 0))
When I run the program, it returns an error "Run-time error '9': Subscript out of range" in another subprocedure on line
Code:
If vari(num \ 9, num Mod 9) > 0 Then
When I hover the mouse, it says that num = 0.
Janis
|
|

January 7th, 2005, 04:29 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Have you set Option Base 1? If not, this will be a 0-based array. The second dimension will have elements 0 to 8.
If you do have Option Base 1 set, then there will be no element 0.
Try highlighting vari, right-clicking the highlit item, and adding it to the watchlist. Then in the watch window, click the [+] to expand the array, and see what the element numbers are.
|
|

January 7th, 2005, 09:06 PM
|
|
Authorized User
|
|
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
No, I have not changed no Option Base. And I added it to Watches, it showed that the the first element is No. 0.
Janis
|
|

January 10th, 2005, 02:26 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
|
|
Right. So then the highest index is 7 for 8 elements.
I would get rid of the Mod. This is just a way to be absolutely sure you never exceed an array's boundaries, but you should be able to make your code protect against that.
num \ 9 is integer division, again, a way to try to be sure to not exceed an array's boundaries.
Set up two variables (like indOne and indTwo), and use
Code:
If vari(indOne, indTwo) > 0 Then
|
|
 |