Multipul override.. virtual?

Y

Your_Persona

Is there a way to get effect1 with the method in effect2?

/////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// effect1
//daddy's name is Adam
Console.WriteLine(new grandParent().daddy());
//daddy's name is Bill who's daddy's name is Adam
Console.WriteLine(new parent().daddy());
//daddy's name is Charlie who's daddy's name is Bill
// who's daddy's name is Adam
Console.WriteLine(new child1().daddy());
//daddy's name is Chingy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child2().daddy());
//daddy's name is Champy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child3().daddy());


List<grandParent> aList = new List<grandParent>();
aList.Add(new grandParent());
aList.Add(new parent());
aList.Add(new child1());
aList.Add(new child2());
aList.Add(new child3());
// effect2
foreach (grandParent aObject in aList)
{ //daddy is Adam
Console.WriteLine(aObject.daddy()); //daddy is Adam
} //daddy is Adam

Console.WriteLine("Press the any key");
Console.ReadLine();
}
}
class grandParent
{
public virtual string daddy()
{
return " daddy's name is Adam";
}
}
class parent : grandParent
{
public virtual string daddy()
{
string ret = " daddy's name is Bill";
return ret + " who's " + base.daddy();
}
}
class child1 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Charlie";
return ret + " who's " + base.daddy();
}
}
class child2 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Chingy";
return ret + " who's " + base.daddy();
}
}
class child3 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Champy";
return ret + " who's " + base.daddy();
}
}

}
/////////////////////////////////////////////////////////////////////////////////////////////
OUTPUT
daddy's name is Adam
daddy's name is Bill who's daddy's name is Adam
daddy's name is Charlie who's daddy's name is Bill who's daddy's name
is Adam

daddy's name is Chingy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Champy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
////////////////////////////////////////////////////////////////////////////////////////////

I want to be able to invoke the child most method with out testing for
type, only knowing that the method is implemented.
any ideas?

thanks

Austin
 
G

Guest

Hi Your_Persona,

You need to change virtual to override. Replace your classes with the
following.

class grandParent
{
public virtual string daddy()
{
return " daddy's name is Adam";
}
}
class parent : grandParent
{
public override string daddy()
{
string ret = " daddy's name is Bill";
return ret + " who's " + base.daddy();
}
}
class child1 : parent
{
public override string daddy()
{
string ret = " daddy's name is Charlie";
return ret + " who's " + base.daddy();
}
}
class child2 : parent
{
public override string daddy()
{
string ret = " daddy's name is Chingy";
return ret + " who's " + base.daddy();
}
}
class child3 : parent
{
public override string daddy()
{
string ret = " daddy's name is Champy";
return ret + " who's " + base.daddy();
}
}

Cheers,
Chester

Your_Persona said:
Is there a way to get effect1 with the method in effect2?

/////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// effect1
//daddy's name is Adam
Console.WriteLine(new grandParent().daddy());
//daddy's name is Bill who's daddy's name is Adam
Console.WriteLine(new parent().daddy());
//daddy's name is Charlie who's daddy's name is Bill
// who's daddy's name is Adam
Console.WriteLine(new child1().daddy());
//daddy's name is Chingy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child2().daddy());
//daddy's name is Champy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child3().daddy());


List<grandParent> aList = new List<grandParent>();
aList.Add(new grandParent());
aList.Add(new parent());
aList.Add(new child1());
aList.Add(new child2());
aList.Add(new child3());
// effect2
foreach (grandParent aObject in aList)
{ //daddy is Adam
Console.WriteLine(aObject.daddy()); //daddy is Adam
} //daddy is Adam

Console.WriteLine("Press the any key");
Console.ReadLine();
}
}
class grandParent
{
public virtual string daddy()
{
return " daddy's name is Adam";
}
}
class parent : grandParent
{
public virtual string daddy()
{
string ret = " daddy's name is Bill";
return ret + " who's " + base.daddy();
}
}
class child1 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Charlie";
return ret + " who's " + base.daddy();
}
}
class child2 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Chingy";
return ret + " who's " + base.daddy();
}
}
class child3 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Champy";
return ret + " who's " + base.daddy();
}
}

}
/////////////////////////////////////////////////////////////////////////////////////////////
OUTPUT
daddy's name is Adam
daddy's name is Bill who's daddy's name is Adam
daddy's name is Charlie who's daddy's name is Bill who's daddy's name
is Adam

daddy's name is Chingy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Champy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
////////////////////////////////////////////////////////////////////////////////////////////

I want to be able to invoke the child most method with out testing for
type, only knowing that the method is implemented.
any ideas?

thanks

Austin
 
Y

Your_Persona

Thanks Chester!

Perfect ^^

Hi Your_Persona,

You need to change virtual to override. Replace your classes with the
following.

class grandParent
{
public virtual string daddy()
{
return " daddy's name is Adam";
}
}
class parent : grandParent
{
public override string daddy()
{
string ret = " daddy's name is Bill";
return ret + " who's " + base.daddy();
}
}
class child1 : parent
{
public override string daddy()
{
string ret = " daddy's name is Charlie";
return ret + " who's " + base.daddy();
}
}
class child2 : parent
{
public override string daddy()
{
string ret = " daddy's name is Chingy";
return ret + " who's " + base.daddy();
}
}
class child3 : parent
{
public override string daddy()
{
string ret = " daddy's name is Champy";
return ret + " who's " + base.daddy();
}
}

Cheers,
Chester



Your_Persona said:
Is there a way to get effect1 with the method in effect2?
///////////////////////////////////////////////////////////////////////////­//////////////////
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// effect1
//daddy's name is Adam
Console.WriteLine(new grandParent().daddy());
//daddy's name is Bill who's daddy's name is Adam
Console.WriteLine(new parent().daddy());
//daddy's name is Charlie who's daddy's name is Bill
// who's daddy's name is Adam
Console.WriteLine(new child1().daddy());
//daddy's name is Chingy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child2().daddy());
//daddy's name is Champy who's daddy's name is Bill
// who's dadd's name is Adam
Console.WriteLine(new child3().daddy());
List<grandParent> aList = new List<grandParent>();
aList.Add(new grandParent());
aList.Add(new parent());
aList.Add(new child1());
aList.Add(new child2());
aList.Add(new child3());
// effect2
foreach (grandParent aObject in aList)
{ //daddy is Adam
Console.WriteLine(aObject.daddy()); //daddy is Adam
} //daddy is Adam
Console.WriteLine("Press the any key");
Console.ReadLine();
}
}
class grandParent
{
public virtual string daddy()
{
return " daddy's name is Adam";
}
}
class parent : grandParent
{
public virtual string daddy()
{
string ret = " daddy's name is Bill";
return ret + " who's " + base.daddy();
}
}
class child1 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Charlie";
return ret + " who's " + base.daddy();
}
}
class child2 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Chingy";
return ret + " who's " + base.daddy();
}
}
class child3 : parent
{
public virtual string daddy()
{
string ret = " daddy's name is Champy";
return ret + " who's " + base.daddy();
}
}
}
///////////////////////////////////////////////////////////////////////////­//////////////////
OUTPUT
daddy's name is Adam
daddy's name is Bill who's daddy's name is Adam
daddy's name is Charlie who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Chingy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Champy who's daddy's name is Bill who's daddy's name
is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
daddy's name is Adam
///////////////////////////////////////////////////////////////////////////­/////////////////
I want to be able to invoke the child most method with out testing for
type, only knowing that the method is implemented.
any ideas?

Austin- Hide quoted text -- Show quoted text -
 

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