Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Beginning VB 6
|
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
 
Old January 5th, 2005, 03:11 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

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

OK
 
Old January 6th, 2005, 07:14 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Code:
vari(i, j) = 0
Janis

 
Old January 6th, 2005, 08:19 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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 . . .
 
Old January 7th, 2005, 11:51 AM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old January 7th, 2005, 01:42 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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...
 
Old January 7th, 2005, 04:03 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old January 7th, 2005, 04:29 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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.
 
Old January 7th, 2005, 09:06 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old January 10th, 2005, 02:26 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convering a String Array to an Integer array nkrust C# 9 November 17th, 2010 12:02 PM
Problem assigning my Array values to rows - Please Edoloto Excel VBA 1 August 18th, 2008 04:13 AM
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 11:13 AM
error when sorting an Array of Array nancy VBScript 2 February 17th, 2005 12:57 PM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 03:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.