Reputation: 885
Here is my code (just the classes outside of Main Program class).
It has 2 classes: Vehicle which has just some attributes, and Car which inherits Vehicle and has more functionality like constructor for Car "Car()", method to print info about specific car "PrintCarInfo()" and static method for Car class to print the number of created instances of Car.
public class Vehicle
{
protected double speed;
protected int wheels = 4;
protected string color;
protected string manufacturer;
}
public class Car : Vehicle
{
static int carCounter = 0;
public Car(double speed, string color, string manufacturer)
{
this.speed = speed;
this.color = color;
this.manufacturer = manufacturer;
Interlocked.Increment(ref carCounter);
}
public void PrintCarInfo()
{
Console.WriteLine("Speed of car is {0}", speed);
Console.WriteLine("Car has {0} wheels", wheels);
Console.WriteLine("Car is {0}", color);
Console.WriteLine("Car was made by {0}", manufacturer);
Console.WriteLine();
}
public static void NumberOfCars()
{
Console.WriteLine("Number of cars created: {0}", carCounter);
Console.WriteLine();
}
After i've created a new Car instance: Car car1 = new Car(120, "Red", "Porsche");
, how do i print that specific instance's name inside PrintCarInfo() -method?
Currently the PrintCarInfo() -method prints the speed, wheels, color and manufacturer of the car, but i want to print the name of that specific instance before them.
Something like: Console.WriteLine("Info about {0}", "Insert instance reference here")
I want to avoid giving the instance as parameter for the method e.g car1.PrintCarInfo(car1);
How do i reference the created instance? (car1 in this case)
I tried playing around with object carObject;
but without success.
Upvotes: 0
Views: 1592
Reputation: 4339
As written in my comments:
I don't think that'll work as easily as you might hope for. There is nameof() but you'd have to use this at the call to PrintCarInfo, not inside it. The other simple solution is to give the car a name (just like it has a speed).
As far as I know there's no way of using nameof inside a called function. I know .net has some crazy stuff with attributes but I've never heard of something like this.
Op said they went with giving each car a name which is a perfectly fine if not the best solution for this problem.
Upvotes: 1
Reputation: 170
Use a virtual property/method to return the required label. Reference the virtual property/method in the output section of the base class. Then it'll pick up the right instance's label. Example:
class Program
{
static void Main(string[] args)
{
Vehicle v = new Vehicle();
Car c = new Car();
Console.WriteLine("Testing Vehicle base output");
v.PrintInfo();
Console.WriteLine("Testing Car inherited output");
c.PrintInfo();
return;
}
}
class Vehicle
{
public virtual string MyTypeName() { return "Vehicle"; }
public void PrintInfo()
{
Console.WriteLine(string.Format("Type: {0}", this.MyTypeName()));
}
}
class Car : Vehicle
{
public override string MyTypeName() { return "Car"; }
}
This results in the following output
Testing Vehicle base output
Type: Vehicle
Testing Car inherited output
Type: Car
Upvotes: 0