C
C#12mo ago
drewhosick

accessing an instance of a class in another class

How do you access an instance of a class variable or method if you need to access it from another class? I created the instance in the main program. Do I have to pass it all as arguments and then assign it to another instance of the class created in the method of the other class or is there a better way?
11 Replies
Pobiega
Pobiega12mo ago
you need a reference to the instance in question Something somewhere must be aware of both instances and be able to pass one to the other, or otherwise exposing them to eachother
Servator
Servator12mo ago
Could you show us what you have tried ?
drewhosick
drewhosickOP12mo ago
This is the Program.cs
namespace RacingRiot
{
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car();
Car aiCar = new Car();
Race race = new Race();

Start.StartMessage();
race.DrawTrack();
Thread.Sleep(5000);
}
}
}
namespace RacingRiot
{
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car();
Car aiCar = new Car();
Race race = new Race();

Start.StartMessage();
race.DrawTrack();
Thread.Sleep(5000);
}
}
}
Car.cs Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
public class Car
{
public int behindCar = 0;
public int frontCar = 100;
public string trackBehindCar = "";
public string trackFrontCar = "";
public string spaceBehindCar = "";
public string spaceFrontCar = "";
public void DrawCar()
{
trackBehindCar = "";
trackFrontCar = "";
string spaceBehindCar = "";

for (int i = 0; i < behindCar; i++)
{
trackBehindCar += "_";
spaceBehindCar += " ";
}
for (int i = 0; i < frontCar; i++)
{
trackFrontCar += "_";
}
Console.WriteLine($"{spaceBehindCar}.-'--`-._\n{trackBehindCar}'-O---O--'{trackFrontCar}\n");
}

public void advanceCar()
{
behindCar++;
frontCar--;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
public class Car
{
public int behindCar = 0;
public int frontCar = 100;
public string trackBehindCar = "";
public string trackFrontCar = "";
public string spaceBehindCar = "";
public string spaceFrontCar = "";
public void DrawCar()
{
trackBehindCar = "";
trackFrontCar = "";
string spaceBehindCar = "";

for (int i = 0; i < behindCar; i++)
{
trackBehindCar += "_";
spaceBehindCar += " ";
}
for (int i = 0; i < frontCar; i++)
{
trackFrontCar += "_";
}
Console.WriteLine($"{spaceBehindCar}.-'--`-._\n{trackBehindCar}'-O---O--'{trackFrontCar}\n");
}

public void advanceCar()
{
behindCar++;
frontCar--;
}
}
}
Race.cs Class where I'm having issues. And I know I can't say playerCar.DrawCar() cause it's an instance from Program.CS. Just trying to figure out the best way to pass it over.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack()
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack()
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
Finally, though it's not really important to the discussion Start.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
static class Start
{
static public void StartMessage()
{
Console.WriteLine("Welcome to Racing Riot. The Racing Game where you try to be the first to the Finish Line.\n\n");
Console.WriteLine("Try to beat the other racers on the screen by taking less guesses to guess the random numbers between 1-100. For every one you get wrong the other cars move ahead by 5. For every good guess you " +
"move ahead by 25");
Console.WriteLine("Are you ready? y/n");
char startPlay = Console.ReadKey().KeyChar;



if ((startPlay == 'y') || (startPlay == 'Y'))
{
Console.WriteLine("\nAlright Let's get started.");
Thread.Sleep(5000);
Console.Clear();
}
else if ((startPlay == 'n') || (startPlay == 'N'))
{
Console.WriteLine("\nAlright, maybe next time. Have a good day. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
else
{
Console.WriteLine("\nYou chose an invalid response. Sorry. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
}

}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RacingRiot
{
static class Start
{
static public void StartMessage()
{
Console.WriteLine("Welcome to Racing Riot. The Racing Game where you try to be the first to the Finish Line.\n\n");
Console.WriteLine("Try to beat the other racers on the screen by taking less guesses to guess the random numbers between 1-100. For every one you get wrong the other cars move ahead by 5. For every good guess you " +
"move ahead by 25");
Console.WriteLine("Are you ready? y/n");
char startPlay = Console.ReadKey().KeyChar;



if ((startPlay == 'y') || (startPlay == 'Y'))
{
Console.WriteLine("\nAlright Let's get started.");
Thread.Sleep(5000);
Console.Clear();
}
else if ((startPlay == 'n') || (startPlay == 'N'))
{
Console.WriteLine("\nAlright, maybe next time. Have a good day. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
else
{
Console.WriteLine("\nYou chose an invalid response. Sorry. Goodbye.");
Thread.Sleep(5000);
Console.Clear();
}
}

}
}
Angius
Angius12mo ago
You can pass the car to the Race class via a constructor, or you can pass it to the DrawTrack() method directly
drewhosick
drewhosickOP12mo ago
as an argument? Would you then have to copy it into a new instance of the Car Class inside that Race Class or can you just access the instances directly from the arguments passed in? Sorry, I still find the class thing a little confusing so I want to get it right. and thanks for your help.
Pobiega
Pobiega12mo ago
Classes are "pass by reference", meaning if you pass it in and store it as a variable inside the race, its just a reference pointing to the same car
ZacharyPatten
ZacharyPatten12mo ago
I would recommend you use Console.ReadKey().Key instead of Console.ReadKey().KeyChar. It gives you a nice enum to deal with rather than char As for your issue about using classes from other classes, a quick glance at your code makes me think you should move the declaration of the car objects from Program into Race before
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car(); // <---
Car aiCar = new Car(); // <---
internal class Program
{
static void Main(string[] args)
{
Car playerCar = new Car(); // <---
Car aiCar = new Car(); // <---
after
public class Race
{
public Car playerCar { get; } = new Car(); // <---
public Car aiCar { get; } = new Car(); // <---
public class Race
{
public Car playerCar { get; } = new Car(); // <---
public Car aiCar { get; } = new Car(); // <---
but there are usually numerous ways to handle any given topic in code, so just passing a reference as was mentioned works too
drewhosick
drewhosickOP12mo ago
Ok so kind of like a pointer?
Pobiega
Pobiega12mo ago
exactly like a pointer
drewhosick
drewhosickOP12mo ago
ah ok. Thanks all So like this right? It seems to work so I assume it's right
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack(Car playerCar, Car aiCar)
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace RacingRiot
{
public class Race
{
public void DrawTrack(Car playerCar, Car aiCar)
{
Console.WriteLine("\nYou\n");
playerCar.DrawCar();
Console.WriteLine("\n\nopponent\n");
aiCar.DrawCar();
}
}
}
Pobiega
Pobiega12mo ago
Yup
Want results from more Discord servers?
Add your server