What I'm trying to do is get the values of an object in a given hashtable, thanks to its key. Here is my code:
Hashtable quotidiani = new Hashtable();
Giornale corriere = new Giornale();
corriere.nome("Corriere della Sera");
corriere.direttore("Ferruccio de Bortoli");
corriere.anno("1876");
quotidiani.Add("corriere", corriere);
Giornale repubblica = new Giornale();
repubblica.nome("la Repubblica");
repubblica.direttore("Ezio Mauro");
repubblica.anno("1976");
quotidiani.Add("repubblica", repubblica);
Giornale ilFatto = new Giornale();
ilFatto.nome("Il Fatto Quotidiano");
ilFatto.direttore("Antonio Padellaro");
ilFatto.anno("2009");
quotidiani.Add("ilfatto", ilFatto);
/*IDictionaryEnumerator enumeratore = quotidiani.GetEnumerator();
Console.WriteLine("Di quale quotidiano vuoi visionare le informazioni? (scrivi la chiave): ");
string answer = Console.ReadLine();
while (enumeratore.MoveNext()) {
if (quotidiani.ContainsKey(answer))
{
Giornale grl = (Giornale)enumeratore.Value;
Console.WriteLine("Nome: {0}, Direttore: {1}, Anno di fondazione: {2}", grl.getNome(), grl.getDirettore(), grl.getAnno());
Console.WriteLine("la chiave è presente");
Console.WriteLine(quotidiani[answer]);
break;
}
else {
Console.WriteLine("Chiave non esistente");
break;
}
class Giornale {
string nomeQuotidiano = "";
string dirQuotidiano = "";
string annoQuotidiano = "";
// Funzioni di assegnazione:
public void nome(string nQuotidiano) {
this.nomeQuotidiano = nQuotidiano;
}
public void direttore(string dQuotidiano) {
this.dirQuotidiano = dQuotidiano;
}
public void anno(string aQuotidiano){
this.annoQuotidiano = aQuotidiano;
}
// Funzioni di richiamo:
public string getNome() {
return this.nomeQuotidiano;
}
public string getDirettore() {
return this.dirQuotidiano;
}
public string getAnno() {
return this.annoQuotidiano;
}
}
I'm sorry if the name of the objects and stuff is in italian, I didn't think that I couldn't get stuck at this point.
Basically where I'm stuck is in the block of commented code: what I want to do is find in the hashtable one of its 3 object and get its elements using its key.
So, I want to put the key (with the readline method) and find the elements of the object associated to the key. How can I do that? Probably the solution is very simple, but I can't see it right now.
Hashtable quotidiani = new Hashtable();
var value = quotidiani["key"];
More about hashtables can be found here . Hashtables are kind of an old school type of collection in .NET, you might want to consider using Dictionaries .
I solved! The block of code that does what I meant is this:
Console.WriteLine("Di quale giornale vuoi le informazioni? (inserisci la chiave)");
var key = Console.ReadLine();
var value = quotidiani[key];
Giornale grl = (Giornale)value;
Console.WriteLine("Nome: {0}, Direttore: {1}, Anno di fondazione: {2}",grl.getNome(), grl.getDirettore(), grl.getAnno());
Thank you all for the tips :P
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.