|
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
|
|
|
|
|
Inheriting Keycard from Person really makes no sense. A Keycard is a piece of plastic at best, not a human being.
|
|
|
|
|
I know, but it is a requirement for a project I am making at my university.
My teacher has told me to find help to solve these problems online, and that is why I am here.
It was also his requirement to put binary code on a web page, and I still think it sounds illogical. But upon reading your comments, I am happy that I am not the only one who thinks this way.
I have spent 2 weeks now trying to put binary code inside a web page, and I thought it was me who had overlooked something...
Anyway, I just need help fixing the System.Runtime.Serialization.SerializationException error (the one I mention in the previous post), and then I hopefully won't have to ask for anymore help.
I know as programmers it must be annoying having to create solutions that do not make sense
modified 13-Dec-19 6:42am.
|
|
|
|
|
Marc Hede wrote: It was also his requirement to put binary code on a web page I think you need to ask him to explain what that means. As it stands it means nothing.
|
|
|
|
|
I want to create histogram with Machine names on X axis and observations on Y axis using canvas.js.
Thanks.
|
|
|
|
|
I want to create a time machine.
|
|
|
|
|
That's simple:
Step 1: Go back in time and give yourself the plans to build a time machine.
Step 0: Build the time machine.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
There you will find plenty of samples:
canvasJS Code Samples[^]
We cannot really help you because:- you did not ask any question (you just stated what is your requirement without further information);
- we don't know the data you are dealing with.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
I have been trying to experiment with XML and Binary files.
I did so by re-writing a code I made in an ASP NET Web Application.
Keycard.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace Keycard
{
[Serializable()]
public class Keycard : ISerializable
{
protected string name;
protected int mykey;
public Keycard(string name, int mykey)
{
this.Name = name;
this.Mykey = mykey;
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Mykey
{
get { return mykey; }
set { mykey = value; }
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
info.AddValue("Name", name);
info.AddValue("Keynumber", mykey);
}
public Keycard(SerializationInfo info, StreamingContext context)
{
Name = (string)info.GetValue("Name", typeof(string));
Mykey = (int)info.GetValue("Keynumber", typeof(int));
}
public override string ToString()
{
return "Name: " + name + " ---- " + " Keynumber: " + mykey;
}
}
}
index.aspx.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Keycard
{
public partial class Index : System.Web.UI.Page
{
public void Main(string[] args)
{
Keycard d1 = new Keycard("John", 102030);
Stream stream = File.Open("KeycardData.dat",
FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, d1);
stream.Close();
d1 = null;
stream = File.Open("KeycardData.dat", FileMode.Open);
bf = new BinaryFormatter();
d1 = (Keycard)bf.Deserialize(stream);
stream.Close();
Console.WriteLine(d1.ToString());
XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
using(TextWriter tw = new StreamWriter(@"L\C#\keycards.xml"))
{
serializer.Serialize(tw, d1);
}
d1 = null;
XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
TextReader reader = new StreamReader(@"L\C#\keycards.xml");
object obj = deserializer.Deserialize(reader);
d1 = (Keycard)obj;
reader.Close();
Console.WriteLine(d1.ToString());
}
}
}
I am not getting any error messages at all, and when I press IIS Express, I get my index page, which just has a listbox, but of course there is no data in that listbox.
I can not get any data output, and it does not save any XML file in the folder that I specified. I think the reason might be that I am trying to use this code in a Web Application, but I have never worked with any other types of projects, so I am not sure what I should use instead.
Do you guys think this is the reason? Or could there be another reason as to why I am not getting my data to show up?
As I said, I am getting new error messages, so I have no idea where the problem comes from.
|
|
|
|
|
It looks like you are trying to run a Console application inside a web page, which will never work. Create a simple Console application and things should work out for you. You can also step through your code with the debugger in order to examine the variables at each step.
|
|
|
|
|
Thanks.
I got the binary working.
With XML I get an error "cannot be serialized because it does not have a parameterless constructor" at this part:
XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
Not sure what to change/add.
This is how the code looks now after creating it inside a console application.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Keycard
{
class Class1
{
public static void Main(string[] args)
{
Keycard d1 = new Keycard("John", 102030);
Stream stream = File.Open("KeycardData.dat",
FileMode.Create);
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, d1);
stream.Close();
d1 = null;
stream = File.Open("KeycardData.dat", FileMode.Open);
bf = new BinaryFormatter();
d1 = (Keycard)bf.Deserialize(stream);
stream.Close();
Console.WriteLine(d1.ToString());
XmlSerializer serializer = new XmlSerializer(typeof(Keycard));
using (TextWriter tw = new StreamWriter(@"C\Brugere\Marc8\source\repos\keycards.xml"))
{
serializer.Serialize(tw, d1);
}
d1 = null;
XmlSerializer deserializer = new XmlSerializer(typeof(Keycard));
TextReader reader = new StreamReader(@"C\Brugere\Marc8\source\repos\keycards.xml");
object obj = deserializer.Deserialize(reader);
d1 = (Keycard)obj;
reader.Close();
Console.WriteLine(d1.ToString());
}
}
}
|
|
|
|
|
You need to add the parameterless constructor to your Keycard class as directed by the error message:
public Keycard()
{
this.Name = null;
this.Mykey = null;
}
|
|
|
|
|
I'm looking for an article or sample project that secures a web site, both API and client using .net core 3 and razor pages. The example should NOT be using EntityFramework.
Almost every article/sample I have found is either using EF or is based on core 2.2, I have a folder full of samples where I have downloaded the code, checked the packages and abandoned the project because EF is in there.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
For the clarity...
If you've got .net core 2.2 sample project without EF, what's the problem to refer to .net core 3.0? There's not much changes for ASP.NET projects: What's new in .NET Core 3.0 | Microsoft Docs
Quote: One of the biggest enhancements is support for Windows desktop applications (Windows only). By using the .NET Core 3.0 SDK component Windows Desktop, you can port your Windows Forms and Windows Presentation Foundation (WPF) applications.
|
|
|
|
|
It is an ASP.NeT core razor pages solution. I have three projects in my solution "OdeToFood"
1. OdeToFood (Original Project)
2. OdeToFood.Data (.Net Core Class Library)
3. OdeToFood.Core (.Net Core Class Library)
In OdeToFood.Core, when I use the following using directive in a class in OdeToFood.Core:
using OdeToFood.Data
it works perfect.
Similarly, when I use the following in a class file in OdeToFood.Data
using OdeToFood.Core
it works perfect.
But if I go to the startup.cs file in the "OdeToFood" Project and use
using OdeToFood.Data
It gives an error: The Type or namespace 'Core' does not exist in the namespace "OdeToFood." (Are you missing an assembly reference?)
|
|
|
|
|
Please clarify the references for each project.
Technically, you should reference both Core and Data projects in your main project.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
It sounds like you have a circular reference - OTF.Core depends on OTF.Data , and OTF.Data depends on OTF.Core .
You should refactor your code to remove that cycle from your dependency graph.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I know html, css some fundamentals of js and C#, and I have decided to delve into the world of asp.net. I have learnt that there are two options to begin with, razor pages and mvc. But Razor pages seem to have very little future at present.
Which is better, Razor pages or MVC to begin with? I am only 19 and I want to devote my life to coding
|
|
|
|
|
It sort of depends on your level of experience. If you are fairly skilled at building websites then go straight to MVC. If you are still a little unsure of things then start with Razor (as am I).
|
|
|
|
|
Whichever one you pick, there'll be a new "best" option every 5 years or so.
Concentrate on the fundamentals, and you should be able to transfer your skills to the next "Shiny New Thing" relatively easily.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am very new to ASP.net and C#, so I am trying my best, but there are stille many things that confuse me, especially because when I am looking at various examples online they give me different answers to the same solution, which just makes it even more confusing.
I made a person class, with two subclasses (driver and admin), but I can not get the ArrayList to show up when I run my Index file. I only get a parse error.
What do I need to change to make this work?
My person class
public class Person
{
public Person(string firstName, string lastName, int age, string email)
{
FirstName = firstName;
LastName = lastName;
Age = age;
Email = email;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Email { get; }
public virtual bool ChangeEmail(string email)
{
Email = email;
return true;
}
public override string ToString()
{
return $"Name: {FirstName} {LastName}, Age: {Age}, E-mail: {Email}";
}
}
My subclasses
public class Driver : Person
{
private string v1;
private string v2;
private string v3;
private string v4;
private string v5;
public Driver(string firstName, string lastName, int age, string email, int
licenceNumber)
: base(firstName, lastName, age, email)
{
LicenceNumber = licenceNumber;
}
public Driver(string v1, string v2, string v3, string v4, string v5)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
}
public int LicenceNumber { get; set; }
public override string ToString()
{
return $"Role: Traindriver, LicenceNumber: {LicenceNumber}, " + base.ToString();
}
}
public class Admin : Person
{
public Admin(string firstName, string lastName, int age, string email)
: base(firstName, lastName, age, email)
{
}
public override bool ChangeEmail(string email)
{
if (!email.EndsWith("@pig.dk", StringComparison.InvariantCultureIgnoreCase))
return false;
return base.ChangeEmail(email);
}
public override string ToString()
{
return $"Role: Admin, " + base.ToString();
}
}
}
My ArrayList
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
public object ListBoxResults { get; private set; }
public object DriverListBox { get; private set; }
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ArrayList person = new ArrayList();
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
My Index file
<pre><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs"
Inherits="ThePig.index" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBoxResults" runat="server" Height="800px"
Width="600px"></asp:ListBox>
</div>
</form>
</body>
</html>
|
|
|
|
|
Please indicate the exact error message, as well as the line which is triggering it. "Not working" is not precise enough.
"Five fruits and vegetables a day? What a joke!
Personally, after the third watermelon, I'm full."
|
|
|
|
|
You have a control with the ID ListBoxResults , which will automatically generate a field in your class:
private ListBox ListBoxResults; You have also declared a property called ListBoxResults in your code-behind.
I'd be extremely surprised if that code compiled; if it did, your references to ListBoxResults in your Page_Load method would be referring to the property, not the control.
Remove the property and your code should start working:
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Okay, I removed the property and also changed "ListBoxResults" to simply "ListBox".
There are some problems remaining.
I will list the errors here (sorry for not doing so at first)
ListBox.Items.Add(d1.ToString());
ListBox.Items.Add(d2.ToString());
ListBox.Items.Add(a1.ToString());
Here I get the error: "An object reference is required for the non-static field, method, or property."
If I remember correctly, a solution to this would be to make a static ArrayList, but I am not sure how I can do it for this code.
Error no. 2
<pre lang="c#">public virtual bool ChangeEmail(string email)
{
Email = email;
return true;
}
Error: "Property or indexer cannot be assigned to -- it is read only"
Error no. 3
public Driver(string v1, string v2, string v3, string v4, string v5)
{
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
this.v5 = v5;
}
Error message: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'
Thanks a ton for being patient with me, you guys.
|
|
|
|
|
Member 14671427 wrote: and also changed "ListBoxResults" to simply "ListBox"
Which is not what I said, and is the cause of the "object reference required" errors.
Remove the property; leave the Page_Load method as I showed you:
namespace Pig
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Driver d1 = new Driver("Hans", "Christensen", 32, "hans@pig.dk", 123456);
Driver d2 = new Driver("Peter", "Jensen", 40, "peter@mail.dk", 123456);
Admin a1 = new Admin("Lene", "Maarud", 55, "lene@pig.dk");
ListBoxResults.Items.Add(d1.ToString());
ListBoxResults.Items.Add(d2.ToString());
ListBoxResults.Items.Add(a1.ToString());
}
}
}
Member 14671427 wrote: Error: "Property or indexer cannot be assigned to -- it is read only"
You haven't added a setter to the Email property, so you can't set it.
If you don't want to be able to change it from outside of the class, then make the setter private :
public string Email { get; private set; }
Member 14671427 wrote: There is no argument given that corresponds to the required formal parameter of 'firstName' of 'Person.Person (string, string, int, string)'
Your Person constructor requires four arguments. Every constructor of a class derived from Person must pass those four arguments into the base constructor.
public Driver(string v1, string v2, string v3, string v4, string v5)
: base("??? FIRST NAME ???", "??? LAST NAME ???", 0, "??? EMAIL ???")
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|