5 Replies - 306 Views - Last Post: 06 June 2019 - 06:45 AM Rate Topic: -----

#1 Realme   User is offline

  • D.I.C Head

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

Accesing private List by method

Posted 06 June 2019 - 02:20 AM

hi,

I made a new class "library" in this class I have methods as following:

public class Bibliotekarie
{
    private List<Bok> parts = new List<Bok>();
    Författare Författaren = new Författare("Skriv in författaren: ", "Författaren är: ", "", "", "Boktypen är: ", 150);
    Utg Utgivning = new Utg("Skriv in Titel", "Titeln är: ", "QQ", "", "", 150);
    Titel Titeln = new Titel("Skriv in Titel", "Titeln är: ", "QQ", "", "", 150);
    public void LäggTillRoman()
    {
        Console.WriteLine(Författaren.Serie);
        Console.WriteLine(Titeln.Serie);
        Console.WriteLine("Skriv in utgivningsår: ");
        parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Roman = Författaren.Romanen, Märke = Int32.Parse(Console.ReadLine()) });
    }
    public void LäggTillNovell()
    {
        parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Novell = Utgivning.Novellsamling, Märke = Int32.Parse(Console.ReadLine()) });
    }
    public void LäggTillTidskrift()
    {
        parts.Add(new Bok() { Serie = Console.ReadLine(), Titel = Console.ReadLine(), Tidskrift = Titeln.Tidskriften, Märke = Int32.Parse(Console.ReadLine()) });
    }

    public void Utskrift()
    {
        foreach (Bok aPart in parts)
        {
            Console.WriteLine(aPart);
        }
    }


I made "Utskrift" that will write everything in private list, but when I make a method "Utskrift" to acces the utput nothing gets shown.

Why is that?

Is This A Good Question/Topic? 0
  • +

Replies To: Accesing private List by method

#2 Realme   User is offline

  • D.I.C Head

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

Re: Accesing private List by method

Posted 06 June 2019 - 02:36 AM

Okej, the problem is that it is being called as "Bibliotekarie bib = new bibliotekarie();"

later I have switch cases that does not get access to bib method, how can I solve this?

Okey problem solved, sorry for this post. Can be deleted
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: Accesing private List by method

Posted 06 June 2019 - 06:03 AM

Glad you figured that out.

It looks like you still have not figured out your instantiation problem for your list of books, though. It looks like you are still instantiating the base class instead of the descendant classes. (So much for a 150 IQ... Probably just like the "stable genius" in the White House.)
Was This Post Helpful? 0
  • +
  • -

#4 Realme   User is offline

  • D.I.C Head

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

Re: Accesing private List by method

Posted 06 June 2019 - 06:10 AM

It works perfectly fine so I don't see any problems. Maybe you can tell what exactly is the problem?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

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

Re: Accesing private List by method

Posted 06 June 2019 - 06:39 AM

You are not taking advantage of the inheritance which was the point of the assignment/homework.

Try declaring the Bok class as abstract. What that will do is prevent you from instantiating that base class, but still let you instantiate the descendant classes.

Consider how you would populate a List<Animal> of mammals and birds given this:
abstract class Animal
{
    public string Name { get; private set; }

    public Animal(string name)
    {
        Name = name;
    }
}

class Mammal : Animal
{
    public bool HasTail { get; private set; }

    public Mammal(string name, bool hasTail)
        : base(name)
    {
        HasTail = hasTail;
    }
}

class Bird : Animal
{
    public int WingSpan { get; private set; }

    public Bird(string name, int wingSpan)
       : base(name)
    {
        WingSpan = wingSpan;
    }
}


Was This Post Helpful? 1
  • +
  • -

#6 Realme   User is offline

  • D.I.C Head

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

Re: Accesing private List by method

Posted 06 June 2019 - 06:45 AM

Yes that is true, I only skipped one part and that was the abstract thing. But thanks, I will add this to my task tomorrow and practice on this one. I have already sent my today's task to teacher
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1