Skip to content

Object to type

Bryan Austin

Asked Jan 24, 2007, 7:47 AM EST2 answers
Share
LinkedInX
I have 2 data types in an object array. ExampleBase class ExtendedExample class that extends ExampleBase The the ExampleBase class has a virtual Writeout() method that returns a 'string 1'. The the ExtendedExample class also has virtual Writeout() method that returns 'string 2'. Now when I pull both of them out of the array and cast them both as ExampleBase and they both return 'String 1'. Even though when I use GetType() they return both ExampleBase and ExtendedExample respectively. I want to extend several classes off the ExampleBase class without having to write a lot of code assign a data type when I return the value of the Writeout() method. So is there any way to have it write out a different string even though I cast them as the same base type?

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    You should declare "public virtual string WriteOut()" in base class and "public override string WriteOut()" in Extended class.
    0
  • 0
    Silly me. Thank you, it works now.
    0