Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
M

Marc Hede

@Marc Hede
About
Posts
12
Topics
4
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How do you pause an animation?
    M Marc Hede

    I am making a live subway map, where I have divs representing trains, moving up and down the map. I had no problem making them move, but I would like them to stop for a few seconds at each station. Can someone tell me how I can include this in my code? What line do I need to add, and where?

    window.onload = () => {
    startSetTimeoutAnimation();
    };

    function startSetTimeoutAnimation() {
    const refreshRate = 1000 / 60;
    const maxXPosition = 400;
    let rect = document.getElementById('rect0');
    let speedX = 0.01;
    let positionX = 25;

    window.setInterval(() => {
      positionX = positionX + speedX;
      if (positionX > maxXPosition || positionX < 25) {
        speedX = speedX \* (-1);
      }
      rect.style.top = positionX + 'px';
    }, refreshRate);
    

    }

    JavaScript help question

  • How do I change this code using Inheritance?
    M Marc Hede

    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 :)

    ASP.NET question csharp linq xml json

  • How do I change this code using Inheritance?
    M Marc Hede

    Ah of course. Sometimes it surprises me how simple the solution is. I tend to overthink things :-D The only issue now is that I get a

    System.Runtime.Serialization.SerializationException

    at

    Mykey = (int)info.GetValue("Keynumber", typeof(int));

    Do I need to add Keynumber in my other class file?

    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 k1 = new Keycard("John", 123);

            Stream stream = File.Open("KeycardData.dat",
                FileMode.Create);
    
            BinaryFormatter bf = new BinaryFormatter();
    
            bf.Serialize(stream, k1);
            stream.Close();
    
            k1 = null;
    
            stream = File.Open("KeycardData.dat", FileMode.Open);
    
            bf = new BinaryFormatter();
    
            k1 = (Keycard)bf.Deserialize(stream);
            stream.Close();
            Console.WriteLine(k1.ToString());
            Console.ReadLine();
        }
    
    }
    

    }

    ASP.NET question csharp linq xml json

  • How do I change this code using Inheritance?
    M Marc Hede

    Hey Phil. Thanks a lot I almost got it to work. Only one error left thankfully.

    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 Person : ISerializable
    {
    protected string name;

        public Person(string name)
        {
            this.Name = name;
        }
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        public void GetObjectData(SerializationInfo info, StreamingContext context)
        {
    
            info.AddValue("Name", name);
    
        }
        public Person(SerializationInfo info, StreamingContext context)
        {
            Name = (string)info.GetValue("Name", typeof(string));
        }
    }
    \[Serializable()\]
    public class Keycard : Person
    {
        protected int mykey;
    
        public Keycard(string name)
            : base (name)
        {
            this.Mykey = -1;
        }
        public Keycard(string name, int mykey) : base(name)
        {
            this.Mykey = mykey;
        }
        public int Mykey
        {
            get { return mykey;  }
            set { mykey = value; }
        }
    
        public new void GetObjectData(SerializationInfo info, StreamingContext context)
        {
    
            info.AddValue("Keynumber", mykey);
    
        }
        public Keycard(SerializationInfo info, StreamingContext context)
        {
           Mykey = (int)info.GetValue("Keynumber", typeof(int));
        }
        public override string ToString()
        {
            return "Name: " + name + " ---- " + " Keynumber: " + mykey;
        }
    }
    

    }

    The only problem here is this line

    public Keycard(SerializationInfo info, StreamingContext context)

    I get the constructor error again. I understood your solution with the default name arguement, but how do I apply that logic here?

    ASP.NET question csharp linq xml json

  • How do I change this code using Inheritance?
    M Marc Hede

    Yeah, that is also how I thought I should do it. But I am getting a ton of errors when doing so. "Person does not contain a constructor that takes 0 arguements" "mykey does not exist within current context" "method must have a return type" This is how I wrote it

    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 Person : ISerializable
    {
    protected string name;

        public Person(string name)
        {
            this.Name = name;
        }
    
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    
        \[Serializable()\]
        public class Keycard : Person
        {
            protected int mykey;
    
            public Keycard(int mykey)
            {
                this.Mykey = mykey;
            }
            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 Person(SerializationInfo info, StreamingContext context)
        {
            Name = (string)info.GetValue("Name", typeof(string));
        }
        public Keycard(SerializationInfo info, StreamingContext context)
        {
            Mykey = (int)info.GetValue("Keynumber", typeof(int));
        }
    
        public override string ToString()
        {
            return "Name: " + name + " ---- " + " Keynumber: " + mykey;
        }
    }
    

    }

    ASP.NET question csharp linq xml json

  • How do I change this code using Inheritance?
    M Marc Hede

    I have finally gotten my code to work, but there is one thing left that I want to do. In my code I had made a Keycard class that had a name and a keynumber. What I would like to do is to change the Keycard class to Person Class, which keeps the Name string, and then add the Keycard class as a child to the Person class, which keeps the Mykey int. I have done inheritance before, but I have not tried doing it in a file that also uses Serialization. How should the code look? Also, a small bonus question. As you can see, my code gives a binary output. Like with XML, is there a way to show this binary output on a web application?

    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;
        }
    }
    

    }

    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);
    
    ASP.NET question csharp linq xml json

  • I can´t get any output from XML and Binary.
    M Marc Hede

    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());
        }
    
    }
    

    }

    ASP.NET csharp database linq design xml

  • I can´t get any output from XML and Binary.
    M Marc Hede

    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
    
    ASP.NET csharp database linq design xml

  • Newbie here. Why is my array list not working?
    M Marc Hede

    Ah, what an idiot I was. In Inhereits, I had written "index" instead of "Index". Thanks a lot for helping me =)

    ASP.NET csharp asp-net database design data-structures

  • Newbie here. Why is my array list not working?
    M Marc Hede

    Hi again and thanks once again. I did do ask you said. The reason why I tried playing around with the ListBox line is because I am getting an CS0103 error: "The name ListBoxResults does not exist in the current context", which I don't get, because I looked in my index file. It says

    I checked if the namespace was the issue, but that was not that case. However, the object reference error is gone, just like you said =)

    ASP.NET csharp asp-net database design data-structures

  • Newbie here. Why is my array list not working?
    M Marc Hede

    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

    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.

    ASP.NET csharp asp-net database design data-structures

  • Newbie here. Why is my array list not working?
    M Marc Hede

    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, "

    ASP.NET csharp asp-net database design data-structures
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups