Real Software Forums
http://forums.realsoftware.com/

Creating A Global Class
http://forums.realsoftware.com/viewtopic.php?f=13&t=45024
Page 1 of 1

Author:  RBnoob2012 [ Sat Aug 18, 2012 12:06 pm ]
Post subject:  Creating A Global Class

Hello,

I'm creating my first RealBasic app using the 30-day trial. I love RB so far and feel like this is the future for me. Access to Mac is the big draw.

I'm coming from 20 years of VB6 and 1 year of VB2010. I am trying to create a Global Class and I am failing miserably. I fear that I am doing something from rote and so do not see the error I am creating.

I have looked at dozens of examples, but often "where" in a project's code windows something is taking place isn't obvious to me.

So far I have been able to draw five images to the screen, in different locations, scaling them as necessary. So I'm having some success.

Where I'm failing is creating a Class that holds all of the info I need to reference the 24 Icons that are being drawn across the screen. I've thought about using a Structure because that is almost exactly like a User-Defined-Type from my VB6 days. But I feel like that's "old school" and I need to move to Classes.

Because I'm afraid I'm missing a step because of my VB background, I thought I would explain it step-by-step and hope that someone might take the time to help me see what I'm not seeing.

I'm trying to create a class I can call from anywhere in any action, event, everything.

1. From the Project Tab I clicked: Add Class

2. Class1 appeared on the list of elements in my Project.

3. I changed its name to IconClass under the Property Value section of the Screen.
It has no Interfaces, and Super is <None> (though None does not appear in the Property Value window).

4. I double-clicked the IconClass in the Project Windows and was transported to the IconClass tab window.

5. I selected the Add Property button and entered X1 as the Declaration, <as> Integer <=> 0.

6. I repeated step 5 seven more times:
<Declaration> Y1 <as> Integer <=> 0
<Declaration> X2 <as> Integer <=> 0
<Declaration> Y2 <as> Integer <=> 0
<Declaration> Selected <as> Boolean <=> False
<Declaration> ToolTip <as> String <=> ""
<Declaration> DstWth <as> Integer <=> 0
<Declaration> DstHgt <as> Integer <=> 0

For all 8 Properties, the Global GLOBE tab is selected.

No other code exists under this InitClass tab.

7. From my Internet searches, I believe that to create a Global variable I have to do so in a module. From the Project Tab I selected Add A Module. In the Property Value window I changed Module1 to InitClasses because I figured I would do all the initializing from there for all global classes still to be created.

8. I double-clicked InitClasses and was transported to the InitClasses tab code section.

9. Under the InitClasses tab, I Add Method, naming it SetUpClasses with no arguments and no return type.

10. In the SetUpClasses code window I entered:

<Sub SetUpClasses>
dim MenuIcons(-1) as IconClass

reDim MenuIcons(24) as IconClass

Dim MN as Integer
for MN = 0 to 24
MenuIcons(MN) = New IconClass
next mn

11. In my Draw module's SetUpIcons method (which I haven't described here) I wrote code that gives each MenuIcon index its values.
EXAMPLE: MenuIcons(0).X1 = 10

12. In my Draw module's DrawScreen method (also not described here) I reference MenuIcons from a loop:
EXAMPLE: X1 = MenuIcons(T).X1

13. In my form's code window in the Open sub I entered:
Call SetUpClasses
Call SetUpIcons

14. When I RUN the application I get this error:
Draw.DrawScreen, line 99 ISSUE: This method or property does not exist
X1 = MenuIcons(T).X1

Note that "MenuIcons" is highlighted in YELLOW

The error is a compiler error, I'm not even getting to the first line of my project's code.

Can anyone please tell me what I'm missing? I'm hopelessly confused.

Kind regards, and thank you in advance,
RBnoob2012

Author:  ktekinay [ Sat Aug 18, 2012 12:41 pm ]
Post subject:  Re: Creating A Global Class

You're almost there. The part you missed is that variables declared within a method are local to that method, and go out of scope (vanish) as soon as the method ends. Once your SetUpClasses method is done, MenuIcons is gone so there is nothing for the rest of the app to access.

Instead, add a property to your InitClasses module, MenuIcons() As IconClass, and set its scope to either Global or Public.. Your code can start with the "redim" statement.

As for naming, may I suggest "Globals" as a name for the module as that is where you can keep all of your app-wide properties, and "Init" or "Initialize" as the method name.

I suggest you set the scope of that module's methods and properties to "Public" rather than "Global" to avoid later conflicts and make your code clearer. With Global, other code and access the method or property "MyStuff" as simply MyStuff. With Public, you'd have to use "MyModule.MyStuff".

Author:  RBnoob2012 [ Sat Aug 18, 2012 1:31 pm ]
Post subject:  Re: Creating A Global Class

ktekinay wrote:

<SNIP>

add a property to your InitClasses module, MenuIcons() As IconClass, and set its scope to either Global or Public.. Your code can start with the "redim" statement.

<SNIP>


ktekinay,
First, thank you for taking the time to help me. It would have never occurred to me to make this a Property. Thank you.

I did what you said. In my InitClasses module I added a Property
<Declaration> MenuIcons() <as> IconClass <=>

I left <=> blank.

I made sure that the Global GLOBE icon is selected.

I left this code in the Sub SetUpClasses

ReDim MenuIcons(24) as IconClass
Dim MN as Integer
For MN = 0 to 24
MenuIcons(MN) = New IconClass
Next MN

... and I got a syntax error. Then I realized that you might mean that I should put that code into the code window below the new Property you told me to create.

So I moved it to the Property Window and RAN the app.

An OutOfBoundsException error occurred in SetUpIcons at the first appearance of: MenuIcons(0).X1 = 10

Am I still doing something wrong? As I said before, so often WHERE something goes is not obvious to me.

Regards,
RBnoob2012

Author:  ktekinay [ Sat Aug 18, 2012 1:42 pm ]
Post subject:  Re: Creating A Global Class

RBnoob2012 wrote:
... and I got a syntax error. Then I realized that you might mean that I should put that code into the code window below the new Property you told me to create.

When you create a property, there is space to type below the declaration, but that isn't code, it can be used for a comment or notes, so your code was in the right place.

Your syntax error is in the redim statement, which should read simply:
redim MenuIcons( 24 )

"As" doesn't belong in a redim statement as you cannot change the type of a variable or property after it's declared, so redim is only for changing the size of an array.

By the way, you can also define the property as "MenuIcons( 24 ) <as> IconClass" and skip the redim statement entirely.

Author:  RBnoob2012 [ Sat Aug 18, 2012 3:16 pm ]
Post subject:  Re: Creating A Global Class

Mr. Tekinay,

It worked. Thank you very much.

Warm Regards,
RBnoob2012

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/