Click here to Skip to main content
14,447,096 members
   

ASP.NET

 
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 
GeneralRe: How do I change this code using Inheritance? Pin
Richard Deeming13-Dec-19 1:58
communityengineerRichard Deeming13-Dec-19 1: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.
[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[^].
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 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 
QuestionShould I begin with razor pages or mvc? Pin
Dawud Ahmad29-Nov-19 7:29
MemberDawud Ahmad29-Nov-19 7: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.