Everything saved dissapeared why?

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 572 Views - Last Post: 04 June 2019 - 10:41 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Everything saved dissapeared why?

Posted 03 June 2019 - 11:36 AM

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public class Bok : IEquatable<Bok>
{
    public Bok(string partName, string partTitel, string partRoman, int partId)
    {
        Serie = partName;
        Titel = partTitel;
        Roman = partRoman;
        Märke = partId;
    }

    public Bok()
    {
    }

    public string Serie { get; set; }

    public string Titel { get; set; }

    public string Roman { get; set; }

    public int Märke { get; set; }

    public override string ToString()
    {
        return "Författaren: " + Serie  + "  Titel: " + Titel + " Boktyp: " + Roman +"    Utgivningsår:  " + Märke;
    }
    public override bool Equals(object obj)
    {
        if (obj == null) return false;
        Bok objAsPart = obj as Bok;
        if (objAsPart == null) return false;
        else return Equals(objAsPart);
    }
    public override int GetHashCode()
    {
        return Märke;
    }
    public bool Equals(Bok other)
    {
        if (other == null) return false;
        return (this.Märke.Equals(other.Märke));
    }
    // Should also override == and != operators.

}
    public class Författare : Bok
{
    public string Roman;
    public Författare(string Serie, string Titel, string Roman, int Märke) :
        base(Serie, Titel, Roman, Märke)

    { Roman = "Roman"; }
}

public class Titel : Bok
{
    public string Tidskrift;
    public Titel(string Serie, string Titel,string Roman, int Märke) :
        base(Serie,Titel, Roman, Märke)

    { Tidskrift = "Tidskrift"; }
}
public class Utg : Bok
{
    public string Novellsamling;
    public Utg(string Serie, string Titel,string Roman, int Märke) :
        base(Serie, Titel, Roman, Märke)

    { Novellsamling = "Novellsamling"; }
}
public class Example
{
    public static void Main()
    {
        do
        {
            // Create a list of parts.
            List<Bok> parts = new List<Bok>();
            Console.WriteLine("Välkommen");
            Console.WriteLine("[1] Registrera bok");
            Console.WriteLine("[2] Visa böcker");
            Console.WriteLine("[ESC] Avsluta");
            string Read = Console.ReadLine();
            Int32.TryParse(Read, out int P);
            switch (P)
            {
                case 1:
                    Författare Författaren = new Författare("Skriv in författaren: ", "Författaren är: ", "Boktypen är: ", 150);
                    Titel Titeln = new Titel("Skriv in Titel", "Titeln är: ", "QQ", 150);

                    // Add parts to the list.
                    Console.WriteLine("Vilken Boktyp är detta? ");
                    Console.WriteLine("[1] Roman");
                    Console.WriteLine("[2] Tidskeift");
                    Console.WriteLine("[3] Novellsamling");

                    string reader = Console.ReadLine();
                    Int32.TryParse(reader, out int W);

                    if (W == 1)
                    {
                        Console.WriteLine("ROMAN .....");
                        Console.WriteLine(Författaren.Serie);
                        Console.WriteLine(Titeln.Serie);
                        parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Roman = "Roman ", Märke = 1234 });
                    }
                    // Write out the parts in the list. This will call the overridden ToString method
                    // in the Part class.
                    //Console.WriteLine();
                    foreach (Bok aPart in parts)
                    {
                        Console.WriteLine(aPart);
                    }

                    break;
                case 2:
                    foreach (Bok aPart in parts)
                    {
                        Console.WriteLine(aPart);
                    }
                    break;
            } 
        }
        while (Console.ReadKey().Key != ConsoleKey.Escape);
    }
}


Everything I have saved in this list disappears when I run 2 for showing everything list contains, why is that?

Is This A Good Question/Topic? 0
  • +

Replies To: Everything saved dissapeared why?

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 11:39 AM

Put a breakpoint on line 123 and see what 'parts' contains.
Was This Post Helpful? 0
  • +
  • -

#3 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 11:39 AM

is it because of the wrong do while loop that ends the program before it repeats itself?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 11:48 AM

That is not responsive to what I asked about stepping through it.

If you did step through it you would notice something interesting happening at the top of each loop iteration.
Was This Post Helpful? 0
  • +
  • -

#5 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 11:59 AM

I did step through but didn't sem to find what was the problem. What happened through the loop?
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 11:59 AM

Really? Line 84 didn't sort of jump out as being a problem?
Was This Post Helpful? 0
  • +
  • -

#7 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:03 PM

why would line 84 be problem?

It is a List

This post has been edited by Realme: 03 June 2019 - 12:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:05 PM

*sigh* That's what I am trying to get you to understand.

What happens when the do-while loop happens for a second, third, or fourth iteration?

If you did put a breakpoint drag the 'parts' object into a watch list (or mouse over it) and see what happens. Should be pretty clear if you added items to it.
Was This Post Helpful? 0
  • +
  • -

#9 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,108
  • Joined: 12-January 10

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:08 PM

because you are setting the list to new -- in other words deleting anything you had in there prior
Was This Post Helpful? 1
  • +
  • -

#10 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:12 PM

second or third iteration should save more stuff into it.

I cant see what is the problem

Daren, really good reply thanks.

that was really helpful.

Do you have any suggestions on what I can set the list to?
Was This Post Helpful? 0
  • +
  • -

#11 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15111
  • View blog
  • Posts: 60,456
  • Joined: 12-June 08

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:14 PM

When the loop starts it goes to 81, right? It proceeds to go down line by line again. Including the line 84..

What happens on line 84? A collection is instantiated. Do you believe the data sticks around each iteration after a 'new' is called?

So if you do not want a new list to be instantiated each iteration, what do you think you should do with line 84?
Was This Post Helpful? 2
  • +
  • -

#12 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:17 PM

Thanks Darren really helpful !! I just put Do tag under beneath the list tag and it worked!
Was This Post Helpful? 1
  • +
  • -

#13 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,108
  • Joined: 12-January 10

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:29 PM

read modi's last reply -- it is very helpful in understanding why that gets erased
Was This Post Helpful? 0
  • +
  • -

#14 Realme   User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 147
  • Joined: 22-May 19

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 12:32 PM

yes you are right Modi, I realized that by your writing
Was This Post Helpful? 0
  • +
  • -

#15 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,678
  • Joined: 05-May 12

Re: Everything saved dissapeared why?

Posted 03 June 2019 - 01:21 PM

You may have fixed your list adding issue, but now you are back again to your basic problem in your other thread: Accesing Constructor C#.

Namely:
parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Roman = "Roman ", Märke = 1234 });



What happens when W == 2? You are back to trying to figuring out how to add a Titel to represent a "Tidskeift".

(Also, although your code states that the following is true:
Författare IS-A Bok
Titel IS-A Bok
Utg IS-A Bok

The reverse is not necessary true. A Bok is not a Titel from C#'s point of view. You'll need to keep that in mind when you finish out the implementation of your IEquatable<Bok> implemenation.)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2