Shane Da Silva
Shane Da Silva

Reputation: 268

Pass variables from function to function

I'm just learning Objective-C and have a what I'm sure is a pretty basic question here. This is a function I've created that will simple hold variables from some user input.

- (void)standardDatabaseWithName:(NSString*)name 
                            host:(NSString*)host 
                        username:(NSString*)username 
                        password:(NSString*)password
                        database:(NSString*)database
                            port:(NSInteger*)port {

  NSString *myName = name;
  NSString *myHost = host;
  NSString *myUsername = username;
  NSString *myPassword = password;
  NSString *myDatabase = database;
  NSInteger *myPort = port;
}

Below is a seperate function where I want to create a new var with that information and the from there use it was what I need.

- (void)insertStandardConnection {
    NSString name = [NewDbModalView standardDatabaseWithName:myName];
    NSString host = [NewDbModalView standardDatabaseWithName:myHost]; 
}

So this attempt didn't work for me. Any advice here guys? At this point I've been left scratching my head.

Upvotes: 1

Views: 651

Answers (4)

Chris Lucian
Chris Lucian

Reputation: 1013

In your .h file you should declare properties

    @interface standardDatabaseWithNameObject : NSObject{
    NSString *myName;
    NSString *myHost;
    NSString *myUsername ;
    NSString *myPassword;
    NSString *myDatabase;
    NSInteger *myPort;
    }

    @property (nonatomic, retain) NSString *myName;
    @property (nonatomic, retain) NSString *myHost;
    @property (nonatomic, retain) NSString *myUsername ;
    @property (nonatomic, retain) NSString *myPassword;
    @property (nonatomic, retain) NSString *myDatabase;
    @property (nonatomic, retain) NSInteger *myPort;
etc...

Then in your implementation you need to synthesize the properties and they will be available for use:

@synthesize myName, myHost, myUsername, myPassword, myDatabase, myPort;


- (void)standardDatabaseWithName:(NSString*)name 
                            host:(NSString*)host 
                        username:(NSString*)username 
                        password:(NSString*)password
                        database:(NSString*)database
                            port:(NSInteger*)port {

  myName = name;
  myHost = host;
  myUsername = username;
  myPassword = password;
  myDatabase = database;
  myPort = port;
}

- (void)insertStandardConnection {
    NSString name = myName;
    NSString host = myHost; 
}

- (void) dealloc
{
[myName release];
[myHost release];
[myUsername release];
[myPassword release];
[myDatabase release];
[myPort release];

}

Good Luck

Upvotes: 0

Richard
Richard

Reputation: 3386

The variables you declare in standardDatabaseWithName:... are local to that method. You need to store (copies of) that data inside the object. Although, looking at your code, I wonder if you're trying to set default values? In that case, you might want static global variables to take the values, and then you'd need -(void)standardDatabaseWithName:... to become +(void)setStandardDatabaseName:(NSString *)name ....

Upvotes: 0

Jeff Kelley
Jeff Kelley

Reputation: 19071

Every time you call the method, you have to provide all of the variables:

[NewDbModalView standardDatabaseWithName:myName host:myHost username:aUsername password:aPassword database:aDatabase port:aPort];

So, when you call [NewDbModalView standardDatabaseWithName:myName], that method doesn’t exist.

Upvotes: 0

Aaron Saunders
Aaron Saunders

Reputation: 33345

I would suggest creating an object to hold all of that information and just pass the object around

Upvotes: 2

Related Questions