❔ accessing object from a class inside of another class
I got this class called "Engine"(image 1) that has the object "isOn" which indicates whether the engine is on or off. I got another class called "Car"(image 2) which consists of several objects as well as on object of type Engine;
I'm trying to create a method in class Car that gives me the engine's isOn.
I don't know how to refer to it. this is how I tried to do this(image 3) but it doesn't work.
7 Replies
private bool isOn
-- the private
there means that only methods inside the Engine
class can access it
If you want to be able to access it from other classes, use public
instead of private
thank you
so why does this not work?
GetIsOn
is a method, so you need to invoke it. GetIsOn()
you're right sorry
A property is a better choice here though.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.