Click here to Skip to main content
16,020,519 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: How do I change this code using Inheritance? Pin
phil.o12-Dec-19 22:23
professionalphil.o12-Dec-19 22:23 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede12-Dec-19 22:57
Marc Hede12-Dec-19 22:57 
GeneralRe: How do I change this code using Inheritance? Pin
phil.o12-Dec-19 23:26
professionalphil.o12-Dec-19 23:26 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede12-Dec-19 23:49
Marc Hede12-Dec-19 23:49 
GeneralRe: How do I change this code using Inheritance? Pin
F-ES Sitecore12-Dec-19 23:59
professionalF-ES Sitecore12-Dec-19 23:59 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede13-Dec-19 0:17
Marc Hede13-Dec-19 0:17 
GeneralRe: How do I change this code using Inheritance? Pin
phil.o13-Dec-19 0:52
professionalphil.o13-Dec-19 0:52 
GeneralRe: How do I change this code using Inheritance? Pin
F-ES Sitecore13-Dec-19 0:54
professionalF-ES Sitecore13-Dec-19 0: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 0:58
mveRichard Deeming13-Dec-19 0:58 
GeneralRe: How do I change this code using Inheritance? Pin
Richard MacCutchan13-Dec-19 0:00
mveRichard MacCutchan13-Dec-19 0:00 
GeneralRe: How do I change this code using Inheritance? Pin
Marc Hede13-Dec-19 0:25
Marc Hede13-Dec-19 0:25 
GeneralRe: How do I change this code using Inheritance? Pin
Richard MacCutchan13-Dec-19 0:44
mveRichard MacCutchan13-Dec-19 0:44 
QuestionHistogram in Asp.net using canvas js Pin
pooja thombare11-Dec-19 20:47
pooja thombare11-Dec-19 20:47 
AnswerRe: Histogram in Asp.net using canvas js Pin
Richard MacCutchan11-Dec-19 21:16
mveRichard MacCutchan11-Dec-19 21:16 
JokeRe: Histogram in Asp.net using canvas js Pin
Richard Deeming12-Dec-19 12:54
mveRichard Deeming12-Dec-19 12:54 
AnswerRe: Histogram in Asp.net using canvas js Pin
phil.o11-Dec-19 23:05
professionalphil.o11-Dec-19 23:05 
QuestionI can´t get any output from XML and Binary. Pin
Marc Hede8-Dec-19 3:51
Marc Hede8-Dec-19 3:51 
AnswerRe: I can´t get any output from XML and Binary. Pin
Richard MacCutchan8-Dec-19 5:15
mveRichard MacCutchan8-Dec-19 5:15 
GeneralRe: I can´t get any output from XML and Binary. Pin
Marc Hede8-Dec-19 7:17
Marc Hede8-Dec-19 7:17 
GeneralRe: I can´t get any output from XML and Binary. Pin
Richard MacCutchan8-Dec-19 21:51
mveRichard MacCutchan8-Dec-19 21:51 
QuestionExample Needed Pin
Mycroft Holmes30-Nov-19 12:02
professionalMycroft Holmes30-Nov-19 12:02 
QuestionRe: Example Needed Pin
Maciej Los3-Dec-19 1:53
mveMaciej Los3-Dec-19 1:53 
QuestionProblem with Using Directive Pin
Dawud Ahmad29-Nov-19 17:00
Dawud Ahmad29-Nov-19 17:00 
AnswerRe: Problem with Using Directive Pin
phil.o29-Nov-19 22:19
professionalphil.o29-Nov-19 22:19 
SuggestionRe: Problem with Using Directive Pin
Richard Deeming3-Dec-19 0:47
mveRichard Deeming3-Dec-19 0: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.