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

Quote:
quote:Originally posted by BrianWren
 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
Yes, I kept that in mind. I have 9 elements, so the highest index would be No 8.

I can try to take the expressions out and assign
Code:
indOne = num \ 9
indTwo = num Mod 9
and then write
Code:
If vari(indOne, indTwo) > 0 Then
But keeping in mind that before trying with Variant I used Array(1..9, 1..9) (and I had extra "+1" for each expression for element index, of course) it worked fine, I doubt it will solve anything.

Thanks for trying though! :)
Janis

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

The statement, indTwo = num Mod 9 says “Asign the remainder after dividing num by 9 to the variable ‘indTwo’.” Naturally, this absolutely guarantees that indTwo will never be larger than 8, it seems like overkill if there is no chance that num will be larger than 8.

I presume you have a loop going from 0 to 80, and you are using the \ and the Mod to extract the indices that you are after. I recomend:
Code:
    For indOne = LBound(vari, 1) to UBound(vari, 1)     ' Access each dimension-1 array.

        For indTwo = LBound(vari, 2) to UBound(vari, 2) ' Access each dimension-2 element.

            SomVal = vari(indOne, indTwo)

        Next indTwo

    Next indOne

This will accomodate unequal-length arrays as the second dimension, and is guaranteed to access every element in your array.

What is it that you are trying to do, in broader scope? What do you do with these values?
 
Old January 10th, 2005, 05:51 PM
Authorized User
 
Join Date: Mar 2004
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much for your help, Brian! :) You're very knowing and a good teacher.

Yes, you understand exactly what I have - 81 text boxes which are a control array with indexes from 0 to 80 and I were looping around them. :)

I looked again at the debug information. It turned out that the correct way of writing an element of a Variant array is vari(i)(j) instead of vari(i, j), that was the problem.

So, now I just had to walk trough the whole code and change vari(i, j) to vari(i)(j), and reducing the index variables (expressions) by 1 (as previously I had the indexes 1..9 as was defined at the beginning. (But I kept that in mind, so incorrect indexes was not the problem.)

Also, I removed "Private" from all procedures. They basically were before all procedures that are responses to changes of form controls. I don't really understand where you put that Private and where not, and I thought maybe when I assign that Variant a value (of array of arrays of zeros) in one Private subprocedure (Form_Onload), it loose this value when exits the procedure. So, I removed that "Private" thing too.

Even when it apparently was not the problem, it working fine as it is, so I probably won't write those back. :)

Thanks!
Janis

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

The difference between (x, x) and (x)(x) was a good catch. That distinction varies from language to language. I've been using JavaScript, and SQL, and I get mixed up from all that.

The purpose of Private is to make a function or a variable accesible only to the module in which it is declared. In practice it does not make a lot of difference, but in returning later to code you wrote, it makes the intention clearer sometimes.

It is pretty useful in classes. Class-scope (analagous to 'module-scope,' but for classes) variables, and Functions and Subs (termed “methods” in classes) declared with Private do not show up in the drop-down list (the intelli-sense list) when you are using the class.

A form's module is, in fact, a specialized class. Subs that interact only with the form itself are good candidates for Private (which is what VB makes them, by default), though you can explicitly declare them Public, and call them from outside the form.

Also, if you have, for instance, a module named Fifi, and it has a private sub called Alpha, and a public sub named Beta, anywhere in code you can start out with “Fifi.”, and an intelli-sense list of just “Beta” will appear (Even within the module Fifi). It is not common to precede the names of routines with the name of the module in which they reside, but you can. And when you do, only the Public items will show in the intelli-sense list.

Lastly, if you write a routine that should only be used by items in a particular module, and you forget where you are and try to use it outside that module, if it is declared Private it will generate a compilation error.

This has been a fun challenge, Janis. Thanks for sticking with it.





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.