Re: Help with Variable Scope
Re: Help with Variable Scope
- Subject: Re: Help with Variable Scope
- From: Ondra Cada <email@hidden>
- Date: Sat, 17 Nov 2001 15:45:32 +0100
Graham,
>
>>>>> Graham Wihlidal (GW) wrote at Sat, 17 Nov 2001 03:52:28 -0700:
GW> I have an NSWindowController class and in the header I put:
GW>
GW> IBOutlet NSButton *ConnectBtn;
First of all, do please conform to conventions. Variables should *ALWAYS*
begin by a lowercase.
GW> I set things up and connect the outlet and it compiles fine, but the
GW> class doesn't seem to know WHERE this variable is? I am trying to call
GW> this from another class:
The class does know; the another one does not. Ever heard of "encapsulation"?
GW> mSessionWin = [[SessionWin alloc] init];
GW> [[mSessionWin window] center];
GW> [[mSessionWin window] makeKeyAndOrderFront:[mSessionWin window]];
GW> [[mSessionWin ConnectBtn] setEnabled:FALSE];
If you want to access a class property, you have to make the appropriate
accessor method:
@interface SessionWin:... {
IBOutlet NSButton *connectBtn;
}
-(NSButton*)connectBtn;
@end
...
@implementation SessionWin
-(NSButton*)connectBtn { return connectBtn; }
@end
GW> I can access that variable the way it is set up INSIDE the window
GW> controller class and it works perfectly. What am I doing wrong?
Forgetting that class variables are accessible only from that class' methods.
(You could make a property accessible directly from outside via the @public
directive, but that's quite a bad habit: don't.)
GW> this just a scope problem and I have to make wrapper functions like
GW> "ChangeStateOfConnectBtn" and access it locally?
That's even better solution than the simple accessor I've recommended above.
Just don't use the name ChangeStateOfConnectBtn -- just like variables,
method/message names begin by lowercase by convention:
changeStateOfConnectBtn would be better, and changeStateOfConnectionButton
even better (since this is the way how message names are composed in
OpenStep).
---
Ondra Cada
OCSoftware: email@hidden
http://www.ocs.cz
2K Development: email@hidden
http://www.2kdevelopment.cz
private email@hidden
http://www.ocs.cz/oc