C
C#8mo ago
Muhammad

❔ ✅ Return

How do I return a dictionary
56 Replies
Muhammad
Muhammad8mo 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 🌊💃8mo ago
Dictionary requires two type parameters. Dictionary<TKey, TValue>.
Muhammad
Muhammad8mo ago
public Dictionary<string, int> getNameAndAge() { return nameAndAge; } like that? the key is a string and the value is an int
Hazel 🌊💃
Hazel 🌊💃8mo ago
🙂
Muhammad
Muhammad8mo 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 🌊💃8mo ago
That looks like you tried something like this:
MODiX
MODiX8mo 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 🌊💃8mo ago
Console.WriteLine(bob.getNameAndAge)
Console.WriteLine(bob.getNameAndAge)
Muhammad
Muhammad8mo ago
nope
Hazel 🌊💃
Hazel 🌊💃8mo ago
Console.WriteLine(Human.getNameAndAge);
Muhammad
Muhammad8mo ago
Human.getNameAndAge yep
Hazel 🌊💃
Hazel 🌊💃8mo 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()
Muhammad
Muhammad8mo ago
Console.WriteLine(Human.getNameAndAge());
Console.WriteLine(Human.getNameAndAge());
? i tried same thing
MODiX
MODiX8mo 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 🌊💃8mo ago
It's not the same thing It's different
Muhammad
Muhammad8mo ago
oh ye i see
Hazel 🌊💃
Hazel 🌊💃8mo 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]
Muhammad
Muhammad8mo ago
i saw
Hazel 🌊💃
Hazel 🌊💃8mo ago
You're getting closer
Muhammad
Muhammad8mo ago
yeah
Hazel 🌊💃
Hazel 🌊💃8mo ago
So, I imagine what you want is to print all names and ages
Muhammad
Muhammad8mo ago
ye in dictionary form {key, value, ... }
Hazel 🌊💃
Hazel 🌊💃8mo 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}");
Muhammad
Muhammad8mo 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 🌊💃8mo 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 🌊💃8mo ago
C# is a zero-based language, yes. Your snippet doesn't make much sense though.
Muhammad
Muhammad8mo 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
Muhammad
Muhammad8mo ago
No description
Hazel 🌊💃
Hazel 🌊💃8mo ago
Try using foreach instead. for will work, but foreach is really all that's needed here.
Muhammad
Muhammad8mo ago
it's working with the for how would you do it?
Hazel 🌊💃
Hazel 🌊💃8mo 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
Angius8mo ago
The dictionary has string as the key, you can't use numbers from 0 up to get items from it this way
Muhammad
Muhammad8mo ago
it's for the index of the pair key = 0, value = 1 and it's iterating over the pairs
Hazel 🌊💃
Hazel 🌊💃8mo ago
It's not though When you access the dictionary like that it expects the key, not an index.
Muhammad
Muhammad8mo ago
not yet
Hazel 🌊💃
Hazel 🌊💃8mo ago
I highly recommend trying to use foreach instead.
MODiX
MODiX8mo 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
Angius8mo 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 🌊💃8mo ago
You can technically iterate over the keys using an index IIRC
Muhammad
Muhammad8mo 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 🌊💃8mo ago
Best way to learn 🙂
Muhammad
Muhammad8mo ago
how do i use foreach what arguments does it take
MODiX
MODiX8mo 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.
Muhammad
Muhammad8mo 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
Angius8mo ago
$tias
Muhammad
Muhammad8mo ago
it's only showing the key
Angius
Angius8mo ago
Trial and error, the basis of all science, including computer science lol
Muhammad
Muhammad8mo 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
Angius8mo ago
$"{{{pair.Key}, {pair.Value}}}" should do the trick
Muhammad
Muhammad8mo ago
you gotta do it by hand? 💀
Angius
Angius8mo ago
Well, you could serialize the dictionary to JSON and print it all if you wanted to Otherwise, yes
Muhammad
Muhammad8mo ago
nvm ye thank you okay i got the hang of it thank you :)))
Hazel 🌊💃
Hazel 🌊💃8mo ago
I supplied a link to documentation that goes over it.
Muhammad
Muhammad8mo ago
@The Dark Realm's No. 2 - Hazel u 2 thank you 🙂 i understand now thank you again !close
Accord
Accord8mo 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
More Posts