C
C#2y ago
dusk

❔ 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
canton7
canton72y ago
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
dusk
duskOP2y ago
thank you
dusk
duskOP2y ago
so why does this not work?
canton7
canton72y ago
GetIsOn is a method, so you need to invoke it. GetIsOn()
dusk
duskOP2y ago
you're right sorry
Jaiganésh
Jaiganésh2y ago
A property is a better choice here though.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server