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

C#

 
AnswerRe: How to create a colletion of properties in a C# class Pin
Abhinav S2-Jan-13 15:42
Abhinav S2-Jan-13 15:42 
AnswerRe: How to create a colletion of properties in a C# class Pin
OriginalGriff2-Jan-13 21:16
mveOriginalGriff2-Jan-13 21:16 
GeneralRe: How to create a colletion of properties in a C# class Pin
bnath0013-Jan-13 5:59
bnath0013-Jan-13 5:59 
GeneralRe: How to create a colletion of properties in a C# class Pin
OriginalGriff3-Jan-13 8:24
mveOriginalGriff3-Jan-13 8:24 
GeneralIf I use Dictionary, I am not able to serialize it Pin
bnath0013-Jan-13 9:08
bnath0013-Jan-13 9:08 
GeneralRe: If I use Dictionary, I am not able to serialize it Pin
OriginalGriff3-Jan-13 9:50
mveOriginalGriff3-Jan-13 9:50 
GeneralRe: If I use Dictionary, I am not able to serialize it Pin
bnath0013-Jan-13 10:44
bnath0013-Jan-13 10:44 
QuestionCA2214 Do not call overridable methods in constructors Pin
DaveyM692-Jan-13 12:35
professionalDaveyM692-Jan-13 12:35 
Maybe I just need sleep to recover from recent festivities - but I can't think of a way to fix this code to comply with the FX Cop warning.
The only thing that comes to mind is to create protected setters (or setter methods) in the base class and call those from the derived constructors...
C#
public abstract class BaseClass
{
    private int id;
        
    // CA2214 Do not call overridable methods in constructors
    protected BaseClass()
    {
        Initialize(out id); // Initialize derived class with it's own specific data and get common data back here for initialization
    }

    public int Id { get { return id; } }

    protected abstract void Initialize(out int id);
}
public class ConcreteA : BaseClass
{
    public ConcreteA()
        : base()
    { }

    protected override void Initialize(out int id)
    {
        ConcreteAIntializationData data = NativeMethods.ConcreteAInitialize();
        id = data.Id;
    }
}
public class ConcreteB : BaseClass
{
    private string name;

    public ConcreteB()
        : base()
    { }

    protected override void Initialize(out int id)
    {
        ConcreteBIntializationData data = NativeMethods.ConcreteBInitialize();
        id = data.Id;
        name = data.name;
    }
}

public static class NativeMethods
{
    // In the real code this is an external C++ dll call
    public static ConcreteAIntializationData ConcreteAInitialize()
    {
        return new ConcreteAIntializationData();
    }
    // In the real code this is an external C++ dll call
    public static ConcreteBIntializationData ConcreteBInitialize()
    {
        return new ConcreteBIntializationData();
    }
}
// In the real code this is a C++ structure
public class ConcreteAIntializationData
{
    private int id;

    // In the real code this struct is filled by unmanaged code.
    public ConcreteAIntializationData()
    {
        id = 1;
    }

    public int Id
    {
        get { return id; }
    }
}
public class ConcreteBIntializationData
{
    private int id;
    public string name;

    // In the real code this struct is filled by unmanaged code.
    public ConcreteBIntializationData()
    {
        id = 2;
        name = "Name";
    }

    public int Id
    {
        get { return id; }
    }
    public string Name
    {
        get { return name; }
    }
}

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)



GeneralRe: CA2214 Do not call overridable methods in constructors Pin
PIEBALDconsult2-Jan-13 13:38
mvePIEBALDconsult2-Jan-13 13:38 
AnswerRe: CA2214 Do not call overridable methods in constructors Pin
Alan N2-Jan-13 14:28
Alan N2-Jan-13 14:28 
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 
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 

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.