'How to search inside a list and how to call my ToString function? C#
I'm currently studying the basics of C# and have an assignment where I am supposed to create a librarian that have various methods. Everything is fine and my code is working with some of the methods, but I can't figure out how to 1. Call my ToString method in "Tidskrift" from another method.I've tried creating an instance of "Tidskrift" but it just complains that there is "no argument given that responds to the required formal parameters." I've tried adding them with "Tidskrift(TitelData, FörfattarData, ÅrtalsData)" but then it says that these do not exist in the current context. I've tried defining them again in the method that calls this class but I'm doing something wrong.
My other issue about how to create a method that allows the user to search my list, I have no idea how to write, and I'm having a hard time finding the answer on Google.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Text;
namespace BokHyllan
{
public class Biblo
{
private List<Bok> bokLista = new List<Bok>();
public void skapaBok()
{
Console.Clear();
Console.WriteLine("\n\t Var god mata in titel: ");
Console.WriteLine("\n\t");
string TitelData = Console.ReadLine();
Console.WriteLine("\n\t Var god mata in författare: ");
Console.WriteLine("\n\t");
string FörfattarData = Console.ReadLine();
Console.WriteLine("\n\t Var god mata in årtal: ");
Console.WriteLine("\n\t");
if (Int32.TryParse(Console.ReadLine(), out int ÅrtalsData)) ;
else
{
Console.WriteLine("Felaktig årtal, använd enbart siffror"); //Bryter loopen vid felaktig input.
Console.ReadLine();
}
Console.Clear();
Console.WriteLine("\n\t Ange boktyp: ");
Console.WriteLine(
"\n\t Var god mata in boktyp: " +
"\n\t[1] Tidskrift " +
"\n\t[2] Roman " +
"\n\t[3] Novell"
);
if (Int32.TryParse(Console.ReadLine(), out int typVal)) // Konverterar input till int.
{
if (typVal == 1)
{
Tidskrift nyTidskrift = new Tidskrift(TitelData, FörfattarData, ÅrtalsData); // Genererar ett objekt med underklassen tidskrift.
bokLista.Add(nyTidskrift);
Console.Clear();
Console.WriteLine("\n\t Titel: "
+ TitelData);
Console.WriteLine("\n\t Författare: "
+ FörfattarData);
Console.WriteLine("\n\tÅrtal: "
+ ÅrtalsData);
Console.WriteLine("\n\tTyp: "
+ nyTidskrift.Typ);
}
else if (typVal == 2)
{
Roman nyRoman = new Roman(TitelData, FörfattarData, ÅrtalsData); // Genererar ett objekt med underklassen roman.
bokLista.Add(nyRoman);
Console.Clear();
Console.WriteLine("\n\tTitel: "
+ TitelData);
Console.WriteLine("\n\tFörfattare: "
+ FörfattarData);
Console.WriteLine("\n\tÅrtal: "
+ ÅrtalsData);
Console.WriteLine("\n\tTyp: "
+ nyRoman.Typ);
}
else if (typVal == 3)
{
Novell nyNovell = new Novell(TitelData, FörfattarData, ÅrtalsData); // Genererar ett objekt med underklassen novel.
bokLista.Add(nyNovell);
Console.Clear();
Console.WriteLine("\n\tTitel: "
+ TitelData);
Console.WriteLine("\n\tFörfattare: "
+ FörfattarData);
Console.WriteLine("\n\tÅrtal: "
+ ÅrtalsData);
Console.WriteLine("\n\tTyp: "
+ nyNovell.Typ);
}
else
{
Console.WriteLine("Felaktigt inmatning, var god välj 1-3");
Console.ReadLine();
}
Console.ReadLine();
}
}
public void VisaBoker()
{
foreach (Bok item in bokLista)
{
Tidskrift ts = new Tidskrift(TitelData, FörfattarData, ÅrtalsData);
Console.WriteLine("\n\t Titel: "
+ item.Titel);
Console.WriteLine("\n\t Författare: "
+ item.Författare);
Console.WriteLine("\n\t Årtal: "
+ item.Årtal);
Console.WriteLine("\n\t Typ: "
+ item.Typ + "\n\t");
Console.WriteLine("\n\t**\n\t");
}
}
public void SokBok()
{
}
public void RaderaBoker()
{
bokLista.Clear();
}
}
public class Bok // Genererar bokklassen.
{
public string Titel;
public string Författare;
public int Årtal;
public string Typ;
public Bok(string TitelData, string FörfattarData, int ÅrtalsData) // Konstruktor som håller information för klassen.
{
Titel = TitelData;
Författare = FörfattarData;
Årtal = ÅrtalsData;
}
}
public class Tidskrift : Bok // Underklass ett.
{
public Tidskrift(string TitelData, string FörfattarData, int ÅrtalsData) : base(TitelData, FörfattarData, ÅrtalsData)
{
Typ = "Tidskrift";
}
public override string ToString()
{
return Typ;
}
}
public class Roman : Bok // Underklass två.
{
public Roman(string TitelData, string FörfattarData, int ÅrtalsData) : base(TitelData, FörfattarData, ÅrtalsData)
{
Typ = "Roman";
}
}
public class Novell : Bok // Underklass tre.
{
public Novell(string TitelData, string FörfattarData, int ÅrtalsData) : base(TitelData, FörfattarData, ÅrtalsData)
{
Typ = "Novell";
}
}
class Program
{
static void Main(string[] args)
{
bool isRunning = true;
Biblo bb = new Biblo();
while (isRunning)
{
Console.Clear();
Console.WriteLine("\n\t Välkommen till Stadsbibliotekets klassifikationssystem");
Console.WriteLine("\n\t[1] Registrera en ny bok");
Console.WriteLine("\n\t[2] Visa alla böcker");
Console.WriteLine("\n\t[3] Rensa bibliotek");
Console.WriteLine("\n\t[4] Avsluta");
if (Int32.TryParse(Console.ReadLine(), out int menyVal))
{
switch (menyVal)
{
case 1: // Lägger till ny bok med användarens input.
bb.skapaBok();
break;
case 2: // Tar fram alla sparade böcker.
Console.Clear();
bb.VisaBoker();
Console.ReadLine();
break;
case 3:
Console.Clear();
bb.RaderaBoker();
Console.WriteLine("Information borttagen");
Console.ReadLine();
break;
case 4: // Stänger av programmet.
Console.Clear();
Console.WriteLine("\n\t Tack för du använde Stadsbibliotekets klassifikationssystem");
Console.ReadLine();
isRunning = false;
break;
}
}
else
{
Console.WriteLine("Felaktig inmatning, var god välj 1-4");
Console.ReadLine();
}
}
}
}
}
I'm at a loss and have spent two days trying to figure this out. Hope someone can help!
Solution 1:[1]
I'll deal with the ToString query but see my comment about what to do on your search query...
Your school class is probably trying to teach you polymorphism.
A Tidskrift is-a Bok:
public class Tidskrift : Bok
Your list is full of Boks:
List<Bok> bokLista = new List<Bok>();
..
Tidskrift nyTidskrift = new Tidskrift(TitelData, FörfattarData, ÅrtalsData);
bokLista.Add(nyTidskrift);
..
Roman nyRoman = new Roman(TitelData, FörfattarData, ÅrtalsData);
bokLista.Add(nyRoman);
Because, at a basic level, a Roman or a Tidskrift is a Bok, they're allowed in your List. Everything in your List will look like a Bok but actually, they will still be a Tidskrift or Roman etc at heart. Whatever they were when they went into the Bok list, they still are.. But unless you cast them back to what they really are, you can only interact with them as if they are a Bok, using facilities (proeprties and methods) that are available on a Bok (or something further up the inheritance chain, like GetHashcode, coming from Object)
You have overriden ToString in Tidskrift:
public override string ToString()
{
return Typ;
}
This means if you call ToString on one of the Boks and that Bok is actually a Tidskrift then the overridden ToString will be called.
If the thing inside the Bok is actually a Roman, the default ToString will be called (it will be BokHyllan.Roman probably). because Roman does not (yet) override ToString()
This means, if you want to call ToString, just call it on your Bok item:
public void VisaBoker()
{
foreach (Bok item in bokLista)
{
Console.WriteLine("ToString Says: " + item.ToString());
Console.WriteLine("\n\t Titel: "
...
ToString is guaranteed to exist for every Bok, because a Bok is an Object*, and Object has a ToString.. but what ToString does on a case-by-case basis will depend on whether the thing that the Bok truly is (a Tidskrift, Roman or Novell) has got an override of ToString or not. If it does have one, it is used. If it doesn't have one, Bok's one is tried (and because Bok doesn't actually have one, Object's one is used; C# goes up the inheritance chain from what the thing truly is, trying to find a ToString it can use. The first one it finds, is used)
- Truly is a Tiskrift
- ToString found on Tidskrift - use it
- Truly is a Roman
- Roman has no ToString - try Bok
- Bok has no ToString - try Object
- Object has a ToString - use it
- Bok has no ToString - try Object
- Roman has no ToString - try Bok
- Truly is a Novell
- Novell has no ToString - try Bok
- Bok has no ToString - try Object
- Object has a ToString - use it
- Bok has no ToString - try Object
- Novell has no ToString - try Bok
* everything is ultimately an Object; even if it doesn't say class Bok : object. If you've said something like class Tisdkrift:Bok{ that means Tidskrift is a Bok. If you've said class Bok{ that means the same as class Bok:object{. Everything ultimately is something that [is something that..] is an object
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
