Click here to Skip to main content
14,660,264 members
Home » Discussions » C#
   

C#

 
GeneralRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:13
professionaljkirkerx3-Sep-20 6:13 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Luc Pattyn3-Sep-20 1:36
sitebuilderLuc Pattyn3-Sep-20 1:36 
GeneralRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:12
professionaljkirkerx3-Sep-20 6:12 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
jkirkerx3-Sep-20 6:31
professionaljkirkerx3-Sep-20 6:31 
AnswerRe: CsvHelper, EBay Order Csv file, not sure how to handle header with quotes and first line of commas Pin
Gerry Schmitz3-Sep-20 10:23
mveGerry Schmitz3-Sep-20 10:23 
QuestionConnect POST server call with HttpListener C# Pin
jdamiancabello2-Sep-20 0:21
Memberjdamiancabello2-Sep-20 0:21 
AnswerRe: Connect POST server call with HttpListener C# Pin
Afzaal Ahmad Zeeshan3-Sep-20 5:19
mvaAfzaal Ahmad Zeeshan3-Sep-20 5:19 
QuestionInheritance problem Pin
Croccodillo19711-Sep-20 5:22
MemberCroccodillo19711-Sep-20 5:22 
Hello all,

I'm just learning to use C# Inheritance.
It is quite clear to me (I've worked with C++ in the past).
I've a problem with two classes.

So, imagine a base class, which contain a sub-class; the sub-class is used to collect data.
I used a sub-class because I need to write and read it from a disk, using a XmlSerializer.

The base class has the needed public Save/Load members, that serialize the sub-class.

I now have to Inherit the base class multiple times; the only differnece in the derived classes is the sub-class, that have a slightly different format for each derived class.

I created few derived classes, re-definind into them the sub-class wth the keyboard "new".

Unfortunately, everytime I call the Save/Load members (defined in the base class) from the derived classes, these methods actually save ansd load the sub-class defined into the base class, not the one defined into the calling derived class.

What I'm doing wrong?

Here below an excerpt of my code, with the definition of the two classes (heavily simplified, of course):

public class BaseClass
{
    public class Data
    {
        public int data1 = 1;
        public int data2 = 1;
        public string Data3 = "BaseString";
    }
    public Data data = new Data();

    public bool Save(string Filename)
    {
        var xs = new XmlSerializer(Data.GetType());
        using (TextWriter sw = new StreamWriter(Filename))
        {
            xs.Serialize(sw, Obj);
        }
        return File.Exists(Filename);
    }

    public static T Load<T>(Filename)
    {
        Object rslt;

        var xs = new XmlSerializer(typeof(T));
        using (var sr = new StreamReader(FileName))
        {
            rslt = (T)xs.Deserialize(sr);
        }
        return (T)rslt;
     }
}

public DerivedClass : BaseClass
{
    public new class Data
    {
        public int data1 = 2;
        public int data2 = 2;
        public string Data3 = "DerivedString";
        public int Data4 = 2;
    }
    public new Data data = new Data();
}


Now, using them as follows gives me the error that the base class Data are saved:

DerivedClass MyClass = new DerivedClass();
MyClass.Save("Test.xml"); // This saves the base class Data,  not the derived one

Any clue?
I know this is probably quite trivial, but please help me.
Thank you in advance.

Ciao,
Giovanni
AnswerRe: Inheritance problem Pin
Richard Deeming1-Sep-20 7:26
mveRichard Deeming1-Sep-20 7:26 
GeneralRe: Inheritance problem Pin
Croccodillo19711-Sep-20 21:58
MemberCroccodillo19711-Sep-20 21:58 
AnswerRe: Inheritance problem Pin
BillWoodruff9-Sep-20 21:16
mveBillWoodruff9-Sep-20 21:16 
QuestionHow to reliably exit Outlook using Interop.Outlook? Pin
Server Automator30-Aug-20 14:43
professionalServer Automator30-Aug-20 14:43 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Gerry Schmitz31-Aug-20 4:50
mveGerry Schmitz31-Aug-20 4:50 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Dave Kreskowiak31-Aug-20 12:12
mveDave Kreskowiak31-Aug-20 12:12 
AnswerRe: How to reliably exit Outlook using Interop.Outlook? Pin
Mycroft Holmes31-Aug-20 12:15
professionalMycroft Holmes31-Aug-20 12:15 
Questionfind TIMES in string of text Pin
free-pizza30-Aug-20 13:52
Memberfree-pizza30-Aug-20 13:52 
AnswerRe: find TIMES in string of text Pin
Richard Andrew x6430-Aug-20 15:03
professionalRichard Andrew x6430-Aug-20 15:03 
GeneralRe: find TIMES in string of text Pin
free-pizza30-Aug-20 16:26
Memberfree-pizza30-Aug-20 16:26 
AnswerRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 19:47
mveOriginalGriff30-Aug-20 19:47 
JokeRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 19:59
professionalPeter_in_278030-Aug-20 19:59 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:08
mveOriginalGriff30-Aug-20 20:08 
GeneralRe: find TIMES in string of text Pin
OriginalGriff30-Aug-20 20:11
mveOriginalGriff30-Aug-20 20:11 
GeneralRe: find TIMES in string of text Pin
Peter_in_278030-Aug-20 20:43
professionalPeter_in_278030-Aug-20 20:43 
GeneralRe: find TIMES in string of text Pin
free-pizza31-Aug-20 3:52
Memberfree-pizza31-Aug-20 3:52 
GeneralRe: find TIMES in string of text Pin
OriginalGriff31-Aug-20 4:08
mveOriginalGriff31-Aug-20 4:08 

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.