C
C#14mo ago
Ahmed

❔ ✅ Return

How do I return a dictionary
56 Replies
Ahmed
AhmedOP14mo ago
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine();
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public Dictionary getNameAndAge() // the error is here, how do i return a dictionary via a function?
{ //
return nameAndAge; //
} //



}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine();
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public Dictionary getNameAndAge() // the error is here, how do i return a dictionary via a function?
{ //
return nameAndAge; //
} //



}
Hazel 🌊💃
Hazel 🌊💃14mo ago
Dictionary requires two type parameters. Dictionary<TKey, TValue>.
Ahmed
AhmedOP14mo ago
public Dictionary<string, int> getNameAndAge() { return nameAndAge; } like that? the key is a string and the value is an int
Hazel 🌊💃
Hazel 🌊💃14mo ago
🙂
Ahmed
AhmedOP14mo ago
but how do i get the whole list because i can only call it via the object not the class if i make it static? but i got this
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Hazel 🌊💃
Hazel 🌊💃14mo ago
That looks like you tried something like this:
MODiX
MODiX14mo ago
Ahmed
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge);
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge);
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Console Output
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
Compile: 765.760ms | Execution: 122.947ms | React with ❌ to remove this embed.
Hazel 🌊💃
Hazel 🌊💃14mo ago
Console.WriteLine(bob.getNameAndAge)
Console.WriteLine(bob.getNameAndAge)
Ahmed
AhmedOP14mo ago
nope
Hazel 🌊💃
Hazel 🌊💃14mo ago
Console.WriteLine(Human.getNameAndAge);
Ahmed
AhmedOP14mo ago
Human.getNameAndAge yep
Hazel 🌊💃
Hazel 🌊💃14mo ago
That's practically exactly what you did. Human.getNameAndAge is a method, so you need to invoke it like one:
Human.getNameAndAge()
Human.getNameAndAge()
Ahmed
AhmedOP14mo ago
Console.WriteLine(Human.getNameAndAge());
Console.WriteLine(Human.getNameAndAge());
? i tried same thing
MODiX
MODiX14mo ago
Ahmed
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge());
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.getNameAndAge());
Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Console Output
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Bob's age is 18 and he has connected.
Bobby's age is 16 and he has connected.
Bibby's age is 11 and he has connected.
3
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Compile: 770.151ms | Execution: 121.414ms | React with ❌ to remove this embed.
Hazel 🌊💃
Hazel 🌊💃14mo ago
It's not the same thing It's different
Ahmed
AhmedOP14mo ago
oh ye i see
Hazel 🌊💃
Hazel 🌊💃14mo ago
Look closely:
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
System.Func`1[System.Collections.Generic.Dictionary`2[System.String,System.Int32]]
System.Collections.Generic.Dictionary`2[System.String,System.Int32]
Ahmed
AhmedOP14mo ago
i saw
Hazel 🌊💃
Hazel 🌊💃14mo ago
You're getting closer
Ahmed
AhmedOP14mo ago
yeah
Hazel 🌊💃
Hazel 🌊💃14mo ago
So, I imagine what you want is to print all names and ages
Ahmed
AhmedOP14mo ago
ye in dictionary form {key, value, ... }
Hazel 🌊💃
Hazel 🌊💃14mo ago
You need to iterate over the elements and print each one like:
Console.WriteLine($"{item.Key}: {item.Value}");
Console.WriteLine($"{item.Key}: {item.Value}");
Ahmed
AhmedOP14mo ago
hmm i'd need to iterate a certain number of times too so i got a question dict = {key, value [0] key,value [1] } does the first pair count as 0
Hazel 🌊💃
Hazel 🌊💃14mo ago
Iteration statements -for, foreach, do, and while - C#
C# iteration statements (for, foreach, do, and while) repeatedly execute a block of code. You use those statements to create loops or iterate through a collection.
Hazel 🌊💃
Hazel 🌊💃14mo ago
C# is a zero-based language, yes. Your snippet doesn't make much sense though.
Ahmed
AhmedOP14mo ago
look
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);


int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}


Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

Console.WriteLine(Human.objectCount);


int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}


Console.ReadLine();

class Human
{
private string name { get; set; }
private int age { get; set; }
public static int objectCount;

static Dictionary<string, int> nameAndAge = new Dictionary<string, int>();

public Human(string name, int age)
{

this.name = name;
this.age = age;
objectCount++;

Console.WriteLine($"{this.name}'s age is {this.age} and he has connected.");

nameAndAge.Add(name, age);

}

public static Dictionary<string, int> getNameAndAge()
{
return nameAndAge;
}

}
this is the snippet im referring to
int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}
int length_of_objectCount = Human.objectCount;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
Console.WriteLine(Human.nameAndAge);
}
@The Dark Realm's No. 2 - Hazel
Ahmed
AhmedOP14mo ago
No description
Hazel 🌊💃
Hazel 🌊💃14mo ago
Try using foreach instead. for will work, but foreach is really all that's needed here.
Ahmed
AhmedOP14mo ago
it's working with the for how would you do it?
Hazel 🌊💃
Hazel 🌊💃14mo ago
I'll tell you once you have a working version in the thread 🙂 Giving you what I would do won't help you learn 😄
Angius
Angius14mo ago
The dictionary has string as the key, you can't use numbers from 0 up to get items from it this way
Ahmed
AhmedOP14mo ago
it's for the index of the pair key = 0, value = 1 and it's iterating over the pairs
Hazel 🌊💃
Hazel 🌊💃14mo ago
It's not though When you access the dictionary like that it expects the key, not an index.
Ahmed
AhmedOP14mo ago
not yet
Hazel 🌊💃
Hazel 🌊💃14mo ago
I highly recommend trying to use foreach instead.
MODiX
MODiX14mo ago
Angius
REPL Result: Success
var dictr = new Dictionary<string, int>(){
["twelve"] = 12,
["eleven"] = 11,
};

dictr["twelve"]
var dictr = new Dictionary<string, int>(){
["twelve"] = 12,
["eleven"] = 11,
};

dictr["twelve"]
Result: int
12
12
Compile: 515.941ms | Execution: 40.232ms | React with ❌ to remove this embed.
Angius
Angius14mo ago
This is how you retrieve values from a dictionary But, yes, if you wanted both keys and values, you would use a foreach
Hazel 🌊💃
Hazel 🌊💃14mo ago
You can technically iterate over the keys using an index IIRC
Ahmed
AhmedOP14mo ago
ima leave this code here n switch to foreach
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
string key = list_for_nameAndAge[KeyValuePair];
int value =
}
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
for (int iterator = 0; iterator > length_of_objectCount; iterator++)
{
string key = list_for_nameAndAge[KeyValuePair];
int value =
}
Hazel 🌊💃
Hazel 🌊💃14mo ago
Best way to learn 🙂
Ahmed
AhmedOP14mo ago
how do i use foreach what arguments does it take
MODiX
MODiX14mo ago
Angius
REPL Result: Success
foreach (char letter in "word")
{
Console.WriteLine($"The letter is '{letter}'");
}
foreach (char letter in "word")
{
Console.WriteLine($"The letter is '{letter}'");
}
Console Output
The letter is 'w'
The letter is 'o'
The letter is 'r'
The letter is 'd'
The letter is 'w'
The letter is 'o'
The letter is 'r'
The letter is 'd'
Compile: 612.394ms | Execution: 91.784ms | React with ❌ to remove this embed.
Ahmed
AhmedOP14mo ago
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
foreach (var pair in list_for_nameAndAge)
{
Console.WriteLine(pair.Key, pair.Value);
}
int length_of_objectCount = Human.objectCount;
Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge;
foreach (var pair in list_for_nameAndAge)
{
Console.WriteLine(pair.Key, pair.Value);
}
?
Angius
Angius14mo ago
$tias
Ahmed
AhmedOP14mo ago
it's only showing the key
Angius
Angius14mo ago
Trial and error, the basis of all science, including computer science lol
Ahmed
AhmedOP14mo ago
not the value int length_of_objectCount = Human.objectCount; Dictionary<string, int> list_for_nameAndAge = Human.nameAndAge; foreach (var pair in list_for_nameAndAge) { Console.WriteLine(pair.Key + pair.Value); } i fixed it how do you get it to appear like {key, value ... } in that format
Angius
Angius14mo ago
$"{{{pair.Key}, {pair.Value}}}" should do the trick
Ahmed
AhmedOP14mo ago
you gotta do it by hand? 💀
Angius
Angius14mo ago
Well, you could serialize the dictionary to JSON and print it all if you wanted to Otherwise, yes
Ahmed
AhmedOP14mo ago
nvm ye thank you okay i got the hang of it thank you :)))
Hazel 🌊💃
Hazel 🌊💃14mo ago
I supplied a link to documentation that goes over it.
Ahmed
AhmedOP14mo ago
@The Dark Realm's No. 2 - Hazel u 2 thank you 🙂 i understand now thank you again !close
Accord
Accord13mo ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server