Click here to Skip to main content
14,459,650 members
   

ASP.NET

 
AnswerRe: How do I change this code using Inheritance? Pin
phil.o12-Dec-19 23:23
mvephil.o12-Dec-19 23:23 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede12-Dec-19 23:57
MemberMarc Hede12-Dec-19 23:57 
GeneralRe: How do I change this code using Inheritance? Pin
phil.o13-Dec-19 0:26
mvephil.o13-Dec-19 0:26 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede13-Dec-19 0:49
MemberMarc Hede13-Dec-19 0:49 
GeneralRe: How do I change this code using Inheritance? Pin
F-ES Sitecore13-Dec-19 0:59
mveF-ES Sitecore13-Dec-19 0:59 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede13-Dec-19 1:17
MemberMarc Hede13-Dec-19 1:17 
GeneralRe: How do I change this code using Inheritance? Pin
phil.o13-Dec-19 1:52
mvephil.o13-Dec-19 1:52 
GeneralRe: How do I change this code using Inheritance? Pin
F-ES Sitecore13-Dec-19 1:54
mveF-ES Sitecore13-Dec-19 1:54 
Ok, I understands what you're doing a bit better now. The GetObjectData method on Person is

public void GetObjectData(SerializationInfo info, StreamingContext context)
{

    info.AddValue("Name", name);

}


and on Keycard it is

public new void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("Keynumber", mykey);

}


The base class (Person) is a normal method and the inherited class (Keycard) is marked "new" which means you're breaking the inheritance chain and saying that the two methods may have the same name but they are their own thing. When you serialise your data as Person is the class that is ISerializable that is the class your GetObjectData is called on, the version on Keycard is never called. What you need to do is mark the version on Person as virtual and the one on Keycard as override. That means that even though GetObjectData is being called on Person, the method on Keycard will be called instead as that version overrides the one on Person. Now your keycard version is being called you can then call the version on Person yourself.

Person;

public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
    info.AddValue("Name", name);
}


Keycard;

public override void GetObjectData(SerializationInfo info, StreamingContext context)
{
    base.GetObjectData(info, context);
    info.AddValue("Keynumber", mykey);
}


Now when serialisation happens both GetObjectData methods will be called.

Your next problem is that when you deserialise Keycard the constructor called is

public Keycard(SerializationInfo info, StreamingContext context)
    : base ("unnamed")
{
    Mykey = (int)info.GetValue("Keynumber", typeof(int));
}


The problem here (admittedly I did say to code that but I didn't fully understand what you're trying to do) is that your Keycard constructor is called the non-ISerializable constructor on Person;

public Person(string name)
{
    this.Name = name;
}


So the keycard constructor is reading the Mykey value from the SerializationInfo object, but the Person constructor is hard-coding the name to "unnamed", it isn't reading from SerializationInfo. So you need to call the ISerializable constructor on Person instead;

public Keycard(SerializationInfo info, StreamingContext context)
    : base (info, context)
{
    Mykey = (int)info.GetValue("Keynumber", typeof(int));
}


Now both the Keycard and Person parts of your object will initialise themselves from the serialization info.
GeneralRe: How do I change this code using Inheritance? Pin
Richard Deeming13-Dec-19 1:58
communityengineerRichard Deeming13-Dec-19 1:58 
GeneralRe: How do I change this code using Inheritance? Pin
Richard MacCutchan13-Dec-19 1:00
communityengineerRichard MacCutchan13-Dec-19 1:00 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede13-Dec-19 1:25
MemberMarc Hede13-Dec-19 1:25 
GeneralRe: How do I change this code using Inheritance? Pin
Richard MacCutchan13-Dec-19 1:44
communityengineerRichard MacCutchan13-Dec-19 1:44 
QuestionHistogram in Asp.net using canvas js Pin
pooja thombare11-Dec-19 21:47
Memberpooja thombare11-Dec-19 21:47 
AnswerRe: Histogram in Asp.net using canvas js Pin
Richard MacCutchan11-Dec-19 22:16
communityengineerRichard MacCutchan11-Dec-19 22:16 
JokeRe: Histogram in Asp.net using canvas js Pin
Richard Deeming12-Dec-19 13:54
communityengineerRichard Deeming12-Dec-19 13:54 
AnswerRe: Histogram in Asp.net using canvas js Pin
phil.o12-Dec-19 0:05
mvephil.o12-Dec-19 0:05 
QuestionI can´t get any output from XML and Binary. Pin
Marc Hede8-Dec-19 4:51
MemberMarc Hede8-Dec-19 4:51 
AnswerRe: I can´t get any output from XML and Binary. Pin
Richard MacCutchan8-Dec-19 6:15
communityengineerRichard MacCutchan8-Dec-19 6:15 
GeneralRe: I can´t get any output from XML and Binary. Pin
Marc Hede8-Dec-19 8:17
MemberMarc Hede8-Dec-19 8:17 
GeneralRe: I can´t get any output from XML and Binary. Pin
Richard MacCutchan8-Dec-19 22:51
communityengineerRichard MacCutchan8-Dec-19 22:51 
QuestionExample Needed Pin
Mycroft Holmes30-Nov-19 13:02
MemberMycroft Holmes30-Nov-19 13:02 
QuestionRe: Example Needed Pin
Maciej Los3-Dec-19 2:53
mveMaciej Los3-Dec-19 2:53 
QuestionProblem with Using Directive Pin
Dawud Ahmad29-Nov-19 18:00
MemberDawud Ahmad29-Nov-19 18:00 
AnswerRe: Problem with Using Directive Pin
phil.o29-Nov-19 23:19
mvephil.o29-Nov-19 23:19 
SuggestionRe: Problem with Using Directive Pin
Richard Deeming3-Dec-19 1:47
communityengineerRichard Deeming3-Dec-19 1:47 

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.