Visual Basic

Moderators: None (Apply to moderate this forum)
Number of threads: 17974
Number of posts: 55343

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
declaring public array Posted by DorisTan on 3 Jan 2005 at 8:48 PM
Hi;

i search thru the forum but failed to find the solution that i want...
i am using vb6, my problem is i would like to declare a public array with 2 dimension, but an error message prompted and tell me that it is not allowed to do so, so after searching the net i using the LET and GET method but i still not really know how to use it, can someone help? below is my code:

***********************************************************************
in my class1:

Private GraphicArr(1 To 2, 50) As String

Public Property Get GArray(ShpType As Integer, ShpNo As Integer) As String
GArray = GraphicArr(ShpType, ShpNo)
End Property

Public Property Let GArray(ShpType As Integer, ShpNo As Integer, Value As String)
GraphicArr(ShpType, ShpNo) = Value
End Property

***********************************************************************

i would like to know in my main form, how can i set the value by calling the GArray and how do i call back to retrieve the value?

really need your help, please......

Thanks :)

Report
Re: declaring public array Posted by Genjuro on 4 Jan 2005 at 2:25 AM
This message was edited by Genjuro at 2005-1-4 9:31:9

: Hi;
:
: i search thru the forum but failed to find the solution that i want...
: i am using vb6, my problem is i would like to declare a public array with 2 dimension, but an error message prompted and tell me that it is not allowed to do so, so after searching the net i using the LET and GET method but i still not really know how to use it, can someone help? below is my code:
:
: ***********************************************************************
: in my class1:
:
: Private GraphicArr(1 To 2, 50) As String
:
: Public Property Get GArray(ShpType As Integer, ShpNo As Integer) As String
: GArray = GraphicArr(ShpType, ShpNo)
: End Property
:
: Public Property Let GArray(ShpType As Integer, ShpNo As Integer, Value As String)
: GraphicArr(ShpType, ShpNo) = Value
: End Property
:
: ***********************************************************************
:
: i would like to know in my main form, how can i set the value by calling the GArray and how do i call back to retrieve the value?
:
: really need your help, please......
:
: Thanks :)

I am assuming that code is written inside a "class module" ("class1" hints to that), in a standard EXE project.

To use that array, first of all, you should declare your "class1" object, so put:
private GraphHelper as class1

in your "global declarations" section of the form.

Then you should instance it inside your Form_Load procedure:
Set GraphHelper = new class1


Once you do so, you can freely use your array.
VB will call let and get when you try to assign a value to an array item (Let) or read an item from the array (Get). if you're unsure of how it works, just put in the Get and Let procedures a breakpoint.

Anyway, if you write:
 GraphHelper.GArray(1, 2) = "MyValue"

VB, upon compiling, will roughly translate this to:
 GraphHelper.Let_GArray(1, 2, "MyValue")

which explains about the extra parameter in the Let property.

Writing:
 
Dim s as string
s = GraphHelper.GArray(1, 2)

will translate to:
 s = GraphHelper.Get_GArray(1, 2)


I hope it's clearer. Now for a few more hints:

- Using a standard module instead of a class module might be handier, since it means you don't have to declare and instance it (and use the "object.property" call either). Class modules are meant for classes - things you can have multiple "copies" of. You can simply cut and paste the code in your "class1" module (the same you posted here) into a standard module, and then forget about everything said above and use it like you would with any global array named GArray (well, except ReDim).

- The array declaration you posted is fine and well, but the second dimension has no lower bound. I'd declare it as "Private GraphicArr(1 To 2, 1 to 50) As String ". More like a style thing, but are you sure that lower array bounds are the ones you expect them to be? As a rule of thumb, when in doubt, always specify.

- Again for the array declaration: defining a lower bound of 1 for arrays is fine and well; however, it your program has a future, you'd better plan for VB.NET compatibility in advance and save pain later. In VB.NET ("VB7", although actually no VB at all), all arrays are zero-based, so it's safer to zero-base them ("Private GraphicArr(0 To 1, 0 to 49) As String "). While it's pretty painless for you to do so in VB6, it won't be as painless if you will ever need to find all the indexes in your code to decrease them by one to convert it to VB.NET. Again, it might not be your case. On a side note, I'm not sure about Option Base in VB.NET, so if you're using it, don't trust it too much.

EDIT: infidel pointed me out a copy-and-paste error :/
Report
Re: declaring public array Posted by infidel on 4 Jan 2005 at 9:01 AM
: Writing:
:
 
: Dim s as string
: s = GraphHelper.GArray(1, 2) = "MyValue"

: will translate to:
:
 s = GraphHelper.Get_GArray(1, 2)


Are you sure about that? Won't s hold a boolean value?


infidel

$ select * from users where clue > 0
no rows returned


Report
Re: declaring public array Posted by Genjuro on 4 Jan 2005 at 9:33 AM
: : Writing:
: :
 
: : Dim s as string
: : s = GraphHelper.GArray(1, 2) = "MyValue"

: : will translate to:
: :
 s = GraphHelper.Get_GArray(1, 2)

:
: Are you sure about that? Won't s hold a boolean value?
:
:
: infidel
:
:
: $ select * from users where clue > 0
: no rows returned
: 


Well, yes and no.
It would contain a boolean value converted to a string.

Actually, I edited the post... I made a copy-and-paste typo back there, thank you for pointing it out :/



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.