Click here to Skip to main content
15,881,803 members
Home / Discussions / ASP.NET
   

ASP.NET

 
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 
GeneralRe: How do I change this code using Inheritance? Pin
Richard Deeming13-Dec-19 0:58
mveRichard Deeming13-Dec-19 0:58 
Make Person.GetObjectData a virtual method. Change Keycard.GetObjectData to override the method instead of shadowing the method, and have it call the base method.
C#
[Serializable()]
public class Person : ISerializable
{
    ...
    public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("Name", name);
    }
    ...
}

[Serializable()]
public class Keycard : Person
{
    ...
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue("Keynumber", mykey);
    }
    ...
}
Your code will now work.

However, you're creating Stream objects, which should be disposed of when you're finished with them. The simplest way to do that is with a using block[^].
C#
Keycard k1 = new Keycard("John", 123);
BinaryFormatter bf = new BinaryFormatter();

using (Stream stream = File.Open("KeycardData.dat", FileMode.Create))
{
    bf.Serialize(stream, k1);
}

k1 = null;

using (Stream stream = File.Open("KeycardData.dat", FileMode.Open))
{
    k1 = (Keycard)bf.Deserialize(stream);
}

Console.WriteLine(k1);
Console.ReadLine();




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

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 
QuestionShould I begin with razor pages or mvc? Pin
Dawud Ahmad29-Nov-19 6:29
Dawud Ahmad29-Nov-19 6:29 

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.