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 December 31st, 2004, 01:19 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default Assigning value to array within array

Hi!

I wanted to assign value (in the code of my Visual Basic program) to a variable whose type is array within array.

I have quite a lot of elements there, so it's not very easy to do it like this:
Code:
arrVar(0,0) = 2
arrVar(0,1) = 0
arrVar(0,2) = 1
..
arrVar(0,10) = 0
arrVar(1,0) = 3
arrVar(1,1) = 1
arrVar(1,2) = 0
..
arrVar(7,9) = 8
arrVar(7,10) = 2
I wanted to do it with one assignment operator instead. I tried this, but it didn't work:
Code:
arrVar = Array((2, 0, 1, .., 0), (3, 1, 0, ..), .., (.., 8, 2))
It says a closing bracked is required right after the first digit (2).

So, what is the correct way to do it, can anyone tell, please?

Thanks!
Janis


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

This is pretty ugly, but it does do the job. Maybe it will fit your needs.

    Dim TheMain As Variant
    Dim TheEach As Variant

    TheEach = Array(2, 0, 1)
    TheMain = Array(TheEach)

    TheEach = Array(3, 1, 0)
    TheMain = Array(TheMain(0), TheEach)

    TheEach = Array(8, 2)
    TheMain = Array(TheMain(0), TheMain(1), TheEach)
 
Old January 3rd, 2005, 07:12 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Now that I think about it, I think you can do this:

arrVar = Array(Array(2, 0, 1, .., 0), _
                   Array(3, 1, 0, ..), _
                   Array(..), Array(.., 8, 2) _
              )

Yes. I just tried, and you can indeed!
 
Old January 3rd, 2005, 07:40 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeey! Thank you very much, Brian! It works. :)

Why did they have to require that "array" there?! What else could it be without "array"?!

Janis

Update:
Sheeeesh!
Doesn't work. It says "Can't assign to array"...
Grrr... I've assigned it:
Code:
Dim vari(1 To 9, 1 To 9) As Byte
and then did as you wrote. It even accepted its syntax, so I thought it'd be OK. But when I run, it gives that error! :(
 
Old January 4th, 2005, 12:15 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Each argument in the outer Array() needs to be an array when you have a multi-dimensional array.

The (2, 0, 1, .., 0) and the (3, 1, 0, ..) and the (.., 8, 2) that you had were all lists, but not [u]arrays</u>.

Putting the Array() function there for each argument returned an array as the argument to the outer Array() function.

What is your actual code?
 
Old January 4th, 2005, 01:24 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Erm... What do you mean by a list? Is it some kind of a pre-defined language construction?
If so, how can I write a value for an array rather than for a list?

I have a 2-dimentional array (as I showed before, when I defined the type with "Dim vari(1 To 9, 1 To 9) As Byte"). And which is, as I understand, basically array(s) within array.

So, I have like a table with rows (the outer array, each element of it is a row) and columns (all first elements of sub-arrays form the first column etc.).

Now, I want to assign a value to this object. And I wanted to know how I can write a value (a constant) for such an object, so I can use it in the assignment construct (<something1> = <something2>).

Janis

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

That was “plain speak...” In LISP, lists are a very important, technically defined item; but not in VB.

In Array() you can specify anything for each argument that a Variant can be, including objects like references to Forms, recordsets, etc.

But a Variant cannot be (1, 2, 3). That isn’t actually a thing that VB recognizes; it is—in fact—a syntax error, given that it has no context in which to be understood by VB.

But Array(1, 2, 3) is not a list; it is a Function call, with an argument list. (Context is determinative...) It returns an Variant(Array), which is something that a Variant can be. So that returned value can be used as an argument for the Array() function.

What I did was (in essence):
Code:
    Dim A       As Variant
    Dim B       As Variant
    Dim C       As Variant
    Dim TheMain As Variant

    A       = Array(2, 0, 1, ..., 0)
    B       = Array(3, 1, 0, ...)
    C       = Array(..., 8, 2)

    TheMain = Array(A, B, C)
(Of course, the elipses won’t fly; they’re just indicators that more could go there)
By putting the Array(2, 0, 1, ..., 0) within the TheMain = Array() statement, you avoid having all those use-once-then-discard Variants hanging around, doing nothing.

The nice thing about using Array(Array(), Array(), ...) is that each inside array can be of a different length than the other inside arrays. That can be handy. If one outer element needs to be huge, that does not force the others to also be huge.

You know, recordsets have a method to return an array of the table that they are holding. You might want to look into this, as the logic is already built for you...

I don’t follow how a constant fits into the scheme here...
 
Old January 4th, 2005, 05:59 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not really sure, what a Variant is...

As for the constant. What I mean with that, is a value. A specific value cannot change (it is not a variable), it is a constant. Let me explain, how I understand it.

For example, there is a numeric value: 9, which means number 9. It is a constat. As is also "teepdowf93".

The first constant is of type Byte. So, you can say that 9 is a value (a constant) of type Byte. The second constant is of type String. It is a value/constant of type String.

Now, how can we distinct between these two types of values, that is, what is the correct form of writing them in VB's language, what's the syntax? The values of Byte must consist only of digits and that's it (plus the restriction on range of the numbers, but that's not a question of syntax, I presume). The values of String must be enclosed with quotation marks, and that's it. Everything between two quotation marks is considered a String value.

So, when you have a variable of type String you can write it in the form: <variable> = <value>, where <value> is correctly written constant of type String; and that will assign value <value> to the variable.

What I am trying to find out, is how do I write values of type of array within array (or two-dimentional array), so I can assign a value to my variable of type array within array, or specifically Array(1..9,1..9).

And I thought I already succeeded with your suggestion, as it accepted the syntax of that construction:
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))
But when I ran the program, it says "Can't assign to array"

Thanks,
Janis
 
Old January 4th, 2005, 07:35 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I started a new VB project,
and in the Form_Load event added:
Code:
Option Explicit

Private Sub Form_Load()

    Dim vari As Variant

    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) _
                )

End Sub
This ran fine (no error message, that is)...
Gotta run; more tomorrow.
 
Old January 5th, 2005, 01:21 PM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

When using computers to accomplish work, the medium imposes some constraints. One area where this is heavily felt is in the storage of values in memory. This is why there are various types of variables.

For instance, an integer is stored in 2 bytes of memory. This could allow for values between 0 and 65,535. But the highest bit is used to signify negative if it is 'on,' making the range -32,768 to +32,767.

Variants are a fairly sophisticated variable type. All variants take 16 bytes of memory, and the value held is one of several subtypes. (The program does most of the work to manage this.)
Variants can be NULL (which other variable types cannot be), Empty (which other variable types cannot be), Variant (Array), Variant (String), Variant (Integer), etc.

Only a Variant can handle the results of the Array() function.

When you Dim a variable without specifying a type, it almost always is a Variant. (There is a way to specify that variables beginning with a particular letter are of a certain type, but it is not good practice to use this ability. That, though, is why I say [u]almost</u> always a Variant.)

===================

When you create a constant in VB the type is handled automatically.
If you create constants through the use of Enum, the values will be Longs (4-byte integers).
If you just have
Code:
Const ALPHA = "AbcDe"
this will be a string.
If
Code:
Const A = 10
it is an Integer (2 bytes).
If
Code:
Const A = 100000
it is a Long (4 bytes).

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.

When you declare a variable, type String, and then assign a number to it, VB performs an implicit type conversion. So
Code:
    Dim s As String

    s = 10

will result in s holding "10".

Inasmuch as the code you provided does not work for you, but it does for me, I'm sure it is something at a higher level.

Could you expand the scope of your description of what you are doing?
Is this VB, or VBA behind another application?
Is this in a form, module, class?
What is your declaration for vari?
 . . . ? (Add what you think might be helpful.)

By The Way: I examined further, and the code not only ran, but examining the results shows that the values are what you would expect, and they are accessable.





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.