How to use the object type correctly

G

gazza67

I wonder if someone could tell me how to do what i am trying. Basically
I have a class pointed to by an object type variable which will point
to a class to be set at runtime. I want to call methods from the class
that the object var is pointing to but dont know how to go about it.

Cheers
Gary

using System;

namespace carsCarsCars
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Toyota xyz794 = new Toyota();
Toyota xyz795 = new Toyota();
Nissan ttt214 = new Nissan();
Toyota tyz794 = new Toyota();

Person George = new Person("George", xyz794, xyz795);
Person Albert = new Person("Albert", ttt214, tyz794);
// -----------------------------------------------------
// George.carOne.printMake(); // this line doesnt compile
// ------------------------------------------------------
}
}
public class Person
{
string name;
public object carOne = new object();
public object carTwo = new object();
public Person(string theName, object theCarOne, object theCarTwo)
{
name = theName;
carOne = theCarOne;
carTwo = theCarTwo;
}
}
class Car
{
public int numberOfWheels=4;
}
class Toyota : Car
{
public void printMake() {
Console.WriteLine("toyota");
}
}
class Nissan : Car
{
public void printMake() {
Console.WriteLine("nissan");
}
}

}
 
J

Jon Skeet [C# MVP]

gazza67 said:
I wonder if someone could tell me how to do what i am trying. Basically
I have a class pointed to by an object type variable which will point
to a class to be set at runtime. I want to call methods from the class
that the object var is pointing to but dont know how to go about it.

Well, because carOne and carTwo are only known to be objects, you can't
call any method on them which isn't declared in object. In this case,
you want to call printMake, which is declared indepedently in two
classes. Three options:

1) Create an interface with the printMake method in, which both Toyota
and Nissan implement. Make carOne and carTwo variables of the interface
type.

2) Add printMake to the Car class as an abstract method, making Car
itself abstract. Make carOne and carTwo Car variables.

3) Use reflection to call the printMake method (not recommended - if
you pass something which doesn't have a printMake method, you won't
know until runtime).
 
G

Guest

Slightly different take than Jon's (although his is better engineering by
using an interface), but I think what you want is something as follows (NOTE:
Have NOT actually compiled this so may well have some errors).

using System;

namespace carsCarsCars
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Toyota xyz794 = new Toyota();
Toyota xyz795 = new Toyota();
Nissan ttt214 = new Nissan();
Toyota tyz794 = new Toyota();

Person George = new Person("George", xyz794, xyz795);
Person Albert = new Person("Albert", ttt214, tyz794);

George.carOne.printMake(); // Should compile now
}
}
public class Person
{
string name;
public Car carOne; // NOTE: Type is Car, not object
public Car carTwo; // Ditto

// Params theCarOne and theCarTwo are also Car not object

public Person(string theName, Car theCarOne, Car theCarTwo)
{
name = theName;
carOne = theCarOne;
carTwo = theCarTwo;
}
}
class Car
{
public int numberOfWheels=4;
abstract void printMake(); // All descendents must override!
}
class Toyota : Car
{
public override void printMake() {
Console.WriteLine("toyota");
}
}
class Nissan : Car
{
public override void printMake() {
Console.WriteLine("nissan");
}
}

}
 
G

gazza67

To John and Jon,

Thanks a lot guys responses were helpful. I think I will use Johns
solution (which compiled after a few minor changes) - thanks again.

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top