Click here to Skip to main content
15,881,248 members
Home / Discussions / C#
   

C#

 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM692-Jan-13 22:33
professionalDaveyM692-Jan-13 22:33 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
Pete O'Hanlon2-Jan-13 22:53
mvePete O'Hanlon2-Jan-13 22:53 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
BobJanova3-Jan-13 1:36
BobJanova3-Jan-13 1:36 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
Pete O'Hanlon3-Jan-13 1:42
mvePete O'Hanlon3-Jan-13 1:42 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM693-Jan-13 3:22
professionalDaveyM693-Jan-13 3:22 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM693-Jan-13 12:10
professionalDaveyM693-Jan-13 12:10 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
jschell3-Jan-13 8:16
jschell3-Jan-13 8:16 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM693-Jan-13 11:36
professionalDaveyM693-Jan-13 11:36 
jschell wrote:
why would the base ctor care about child initialization values when all the base
ctor should be doing is initializing the base ctor

The child needs to self initialize with values retrieved from an unmanaged function call (the function is different for each child type) - then pass the common values to the base so it can initialize itself.

What I was trying to do was force a method to be called when the base constructor runs that

1. calls the unmanaged function (hence it needing to be in called from the child as only the child should know what unmanaged function to call, and therefore needs to be an abstract method)
2. the child initializes it's unique fields
3. passes the common values back into the base constructor (hence the out parameters)

Perhaps this code will make it clearer...
C#
public abstract class Base
{
    private int commonValue1;
    private int commonValue2;
    private int id;

    protected Base(int id)
    {
        this.id = id;
        Initialize(out commonValue1, out commonValue2); // CA2214
    }

    public int CommonValue1
    {
        get { return commonValue1; }
    }
    public int CommonValue2
    {
        get { return commonValue2; }
    }
    public int Id
    {
        get { return id; }
    }

    protected abstract void Initialize(out int commonValue1, out int commonValue2);
}

public class ChildA : Base
{
    public ChildA(int id)
        : base(id)
    { }

    protected override void Initialize(out int commonValue1, out int commonValue2)
    {
        DataA data;
        NativeMethods.UnmanagedFunctionGetChildAData(Id, out data);
        commonValue1 = data.Value1;
        commonValue2 = data.Value2;
    }
}

public class ChildB : Base
{
    private int childBValue;

    public ChildB(int id)
        : base(id)
    { }

    public int ChildBValue { get { return childBValue; } }

    protected override void Initialize(out int commonValue1, out int commonValue2)
    {
        DataB data;
        NativeMethods.UnmanagedFunctionGetChildBData(Id, out data);
        commonValue1 = data.Value1;
        commonValue2 = data.Value2;
        childBValue = data.Value3;
    }
}

public class ChildC : Base
{
    private int childCValue1;
    private int childCValue2;

    public ChildC(int id)
        : base(id)
    { }

    public int ChildCValue1 { get { return childCValue1; } }
    public int ChildCValue2 { get { return childCValue2; } }

    protected override void Initialize(out int commonValue1, out int commonValue2)
    {
        DataC data;
        NativeMethods.UnmanagedFunctionGetChildCData(Id, out data);
        commonValue1 = data.Value1;
        commonValue2 = data.Value2;
        childCValue1 = data.Value3;
        childCValue2 = data.Value4;
    }
}

[StructLayout(LayoutKind.Sequential)]
internal struct DataA
{
    private int value1;
    private int value2;

    public int Value1 { get { return value1; } }
    public int Value2 { get { return value2; } }
}

[StructLayout(LayoutKind.Sequential)]
internal struct DataB
{
    private int value1;
    private int value2;
    private int value3;

    public int Value1 { get { return value1; } }
    public int Value2 { get { return value2; } }
    public int Value3 { get { return value3; } }
}

[StructLayout(LayoutKind.Sequential)]
internal struct DataC
{
    private int value1;
    private int value2;
    private int value3;
    private int value4;

    public int Value1 { get { return value1; } }
    public int Value2 { get { return value2; } }
    public int Value3 { get { return value3; } }
    public int Value4 { get { return value4; } }
}

internal static class NativeMethods
{
    // this methods are just simulating the unmanaged functions that retrieve filled DataA/DataB/DataC
    public static void UnmanagedFunctionGetChildAData(int id, out DataA data)
    {
        data = new DataA();
    }
    public static void UnmanagedFunctionGetChildBData(int id, out DataB data)
    {
        data = new DataB();
    }
    public static void UnmanagedFunctionGetChildCData(int id, out DataC data)
    {
        data = new DataC();
    }
}

Dave

Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.
Astonish us. Be exceptional. (Pete O'Hanlon)

BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)




modified 3-Jan-13 18:03pm.

GeneralRe: CA2214 Do not call overridable methods in constructors Pin
Bernhard Hiller3-Jan-13 22:48
Bernhard Hiller3-Jan-13 22:48 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
jschell4-Jan-13 13:08
jschell4-Jan-13 13:08 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM695-Jan-13 3:00
professionalDaveyM695-Jan-13 3:00 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
jschell6-Jan-13 4:42
jschell6-Jan-13 4:42 
AnswerRe: CA2214 Do not call overridable methods in constructors Pin
BobJanova3-Jan-13 1:41
BobJanova3-Jan-13 1:41 
GeneralRe: CA2214 Do not call overridable methods in constructors Pin
DaveyM693-Jan-13 12:29
professionalDaveyM693-Jan-13 12:29 
QuestionBind listview row color to active index Pin
bornefalk2-Jan-13 9:50
bornefalk2-Jan-13 9:50 
QuestionUI Automation and MMC Pin
RonNYC22-Jan-13 5:57
RonNYC22-Jan-13 5:57 
Questionmove a newfolder form a system on other system on the network by c# Pin
a2bj1-Jan-13 23:39
a2bj1-Jan-13 23:39 
AnswerRe: move a newfolder form a system on other system on the network by c# Pin
Jibesh1-Jan-13 23:46
professionalJibesh1-Jan-13 23:46 
AnswerRe: move a newfolder form a system on other system on the network by c# Pin
Simon_Whale2-Jan-13 3:02
Simon_Whale2-Jan-13 3:02 
Questiondata Set Not Coming In GridView Pin
GugliMugli1-Jan-13 17:50
GugliMugli1-Jan-13 17:50 
AnswerRe: data Set Not Coming In GridView Pin
Jibesh1-Jan-13 21:25
professionalJibesh1-Jan-13 21:25 
GeneralRe: data Set Not Coming In GridView Pin
GugliMugli1-Jan-13 23:35
GugliMugli1-Jan-13 23:35 
GeneralRe: data Set Not Coming In GridView Pin
Jibesh1-Jan-13 23:36
professionalJibesh1-Jan-13 23:36 
GeneralRe: data Set Not Coming In GridView Pin
GugliMugli1-Jan-13 23:40
GugliMugli1-Jan-13 23:40 
GeneralRe: data Set Not Coming In GridView Pin
Jibesh1-Jan-13 23:42
professionalJibesh1-Jan-13 23:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.