468,771 Members | 1,974 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,771 developers. It's quick & easy.

Virtual functions, calling inherited class. Quicky.

I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual methods
and the compiler complains because Ive put the :base(foo) after the function
header.
I want to call the BaseResult class to let it write shared data before I let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}
Nov 16 '05 #1
4 1442
Claire,

The "SubClass() : BaseClass()" syntax is used only for constructors. It's a
way to indicate which base class constructor to use before entering the
subclass constructor, which is necessary since the base class is
instantiated before any subclass code runs (including its constructor).

For methods, you can simply call the base class method from within the
subclass method. e.g.:

public override void Write(System.IO.BinaryWriter binarywriter)
{
base(binarywriter);
}

You can run other code before/after/instead of the base method call as
desired.

HTH,
Nicole

"Claire" <bl****@blahhhhh.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual
methods
and the compiler complains because Ive put the :base(foo) after the
function
header.
I want to call the BaseResult class to let it write shared data before I
let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}

Nov 16 '05 #2
Claire,

The "SubClass() : BaseClass()" syntax is used only for constructors. It's a
way to indicate which base class constructor to use before entering the
subclass constructor, which is necessary since the base class is
instantiated before any subclass code runs (including its constructor).

For methods, you can simply call the base class method from within the
subclass method. e.g.:

public override void Write(System.IO.BinaryWriter binarywriter)
{
base.Write(binarywriter);
}

You can run other code before/after/instead of the base method call as
desired.

HTH,
Nicole
"Claire" <bl****@blahhhhh.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual
methods
and the compiler complains because Ive put the :base(foo) after the
function
header.
I want to call the BaseResult class to let it write shared data before I
let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}

Nov 16 '05 #3
Claire wrote:
I'm having real brain failure today.
I've done this lots of times with constructors but not with virtual methods
and the compiler complains because Ive put the :base(foo) after the function
header.
I want to call the BaseResult class to let it write shared data before I let
the derived class write its own data. What am I doing wrong please.

public abstract class BaseResult

{

public virtual void Write(System.IO.BinaryWriter binarywriter)

{

WriteMyData()

}

}

public class MimicResult : BaseResult

{

public override void Write(System.IO.BinaryWriter binarywriter) :
base(binarywriter) /*compiler complains*/

{

// Do I do something here?
}

}


Yes :)

public override void Write(...)
{
base.Write(...)
etc()
}

David Logan
Nov 16 '05 #4
Thanks David and Nicole,
think I'll have an early night tonight

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

99 posts views Thread by David MacQuigg | last post: by
4 posts views Thread by vijay | last post: by
5 posts views Thread by Ryan Faulkner | last post: by
6 posts views Thread by Alden Pierre | last post: by
3 posts views Thread by kikazaru | last post: by
7 posts views Thread by Markus Svilans | last post: by
23 posts views Thread by Dave Rahardja | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.