user11998
user11998

Reputation: 177

How do you reference variables from another class?

I am trying to call variables from another class into my function. I have tried numerous ways, and keep getting errors. Here's a snippit of my code:

- (void) planning: (Deliver*) m :(Car*) n{
    int cost = 0;
    Cost = Deliver.street - Car.avenue;

}

The problem is I want to get variables street and avenue from Deliver and Car class, but I get an error saying "property 'avenue' not found in class Car" on this line Cost = Deliver.street - Car.avenue; I also get this problem with Deliver too.

I have tried adding [Car avenue]; inside the function, but it still doesn't fix this problem. I have already added street and avenue in @property and @synthesis in Deliver and Car classes.

Any suggestions?

Upvotes: 0

Views: 92

Answers (1)

Seamus Campbell
Seamus Campbell

Reputation: 17906

- (void) planning: (Deliver*) m :(Car*) n{
    int cost = 0;
    cost = m.street - n.avenue;
}

There are a couple of things worth noting about your code snippet. First, it doesn't actually do anything, since it stores its work in a local variable (cost) and that variable is not returned; cost will be thrown away at the end of the method. Second, you haven't named your second method argument. More conventional is to use a name that describes the purpose of both the method and each of its arguments, like:

- (int) calculateCostToDeliverTo:(Deliver*)destination withCar:(Car*)car {
    int cost = destination.street - car.avenue;
    return cost;
}

Upvotes: 6

Related Questions