Share via


HOWTO override a method in generated code partial class (not inherited)

Question

Saturday, December 7, 2013 4:05 PM

I am trying to override a method in a typed Dataset that was generated from Dataset Designer but cannot override it because it was defined in the generated partial class.

My project is an ExelWorkbook project.

A Possible solution is to set the "Generates Double Derived"= true from the Dataset designer but I cannot see how to do this. Is it possible? 

or, does anyone know how to override a method that was defined in generated code partial class (not inherited)?

All replies (8)

Saturday, December 7, 2013 4:11 PM

Please verify my account so I can insert some links to context info.


Saturday, December 7, 2013 10:56 PM

All partial does is allow you to have parts of the class in more then one file:

http://msdn.microsoft.com/en-us/library/wbx7zzdd.aspx

http://msdn.microsoft.com/en-us/library/wa80x488.aspx

If you compile it, there is not a bit difference to a class defined in a single file. And it still follows all the limitations:

As usual it can only have one base class (denoting it on any one part is enough), but implement many interface. You overwrite base class methods the same you would overwite them in a normal class.

What exactly is your problem? Can you use codeblocks to post your code?

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Saturday, December 7, 2013 11:05 PM

I overwrote ToString in a partial class, then inherited that partial class and overrode ToString again:

//implictly inherits from object
partial class baseclass
{
    public override string ToString()
    {
        return "This is baseclass string";
    }
}

class derived : baseclass
{
    public override string ToString()
    {
        return "Here is derived class";
    }
}

If I actually had other parts in the partial class, I could override them just as well.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Saturday, December 7, 2013 11:55 PM

You might have mistaken a partial function with overriding a function. Those two have nothing to do with each other. Overriding is something you do with inheritance, nothing else.

With a partial function one part of the code can define the signature. The signature is all that is needed to call it. Another part of the class may code out the function.

If no part of a partial class defines the code of a partial function, the function and all it's calls are removed at compile time. Due to this uncertainty of it's existance in the compiled code they must be strictly private and cannot have out parameters.

This works a bit like having a delegate that you call like this:

if(someDelegate != null)
  someDelegage();

Except the check is done at compile time so there is no execution overhead if no code is given.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Monday, December 9, 2013 4:13 PM

Dataset designer generates the Dataset in 2 partial files.  1) the one that the developer is not supposed to changes because it is regenerated and overlayed and 2) the one that the devloper adds code too.  The problem is that I need to override the code that has been defined in the generated part. I cannot overide these methods, i can only overide the methods that the generated partial inherited.

I see is DSL design doc that the "Generates Double Derived"= true is intended for these cases. 


Monday, December 9, 2013 4:36 PM

Files are not partial. Classes are. Functions can be. I asume you meant:
2 files, containing one part of the partial class each. Something similiar is done in WPF and WinForms, so it's a general designer technique.

Chances are that if you set this property/atribute/whatever to false the Designer part will only provide the Signature of this partial function allowing you to provide the implementation.
But I do not know because I never used the Dataset designer.

This is not overriding. Overriding is done in inheritance, nowhere else. It has a fixed meaning at does not describe what you try to do. If you keep using it, it will confues people trying to help you.

There is a standing rule that there can be only one implimentation for each function in each class. So the only way you ever get to define those functions is if:

a) they are partial

b) the designer provides the signature

c) the [whatever you posted when set to false] forces the designer to not provide the implementation.

Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.


Wednesday, December 11, 2013 5:06 PM

Yes, I understand how partial classes work.

The question is 'how to override a method generated by dataset designer'.


Wednesday, December 11, 2013 11:39 PM

Please post the code that the designer has generated and tell us what you want to do by overriding it.

Paul Linton