I’m really new to c# programming,so please correct me if I am wrong at my terminologies.
I would just like to ask, if it is possible to use 2 different classes from the same object.the code goes like this:
//from the Animal Class;
public void Run()
{
int counter = 0;
string input = "Beagle";
string input2 = "Fox Terrier";
Dog[] al = new Dog[10];
do
{
al[counter] = new Dog();
al[counter].validateAnimal(input);
al[counter] = new Breed();
Breed br = (Breed)al[counter];
br.validateBreed(input2);
}while(counter < 10)
}
//Dog Class
//Dog : Animal
public void validateDog(string In)
{
if(In == "Beagle")
{
Console.WriteLine("Ok");
}
}
//breed class
// Breed : Dog
public void validateBreed(string In)
{
if(In == "Fox Terrier")
{
Console.WriteLine("Great");
}
}
What I want to do is run both the validateDog and validateBreed while it is being run in the main class. I also need to have Breed be a derived class of Dog,and Dog be a derived class of animal. There seems to be no syntax error. but the validateBreed doesn’t show.
What seems to be the problem?
This is a wrong requirement. Breed is not a ‘kind of dog’, breed is a
property of Dog.Instead of creating a derived class, you should create a property
BreedinDogclass.You could list all available breeds in an
enumor define a class for it.Notice that breed is being passed in the constructor and cannot be modified outside
Dogclass since dogs usually don’t change breeds.UPDATE:
You should post your whole code. Hard to tell what you’re doing now.
From your sample this should work:
as both methods are public.
You might want to define a virtual method
validateis base class and override it in derived classes.