C
C#14mo ago
Ahmed

❔ ✅ c#

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

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

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


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.");

Dictionary<Human, Human> nameAndAge = new Dictionary<Human, Human>();



for (int counter = 0; counter < objectCount; counter++)
{
nameAndAge.Add(Human.name, Human.age);
}
}
}
Console.WriteLine(Human.objectCount);
Console.ReadLine();

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

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


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.");

Dictionary<Human, Human> nameAndAge = new Dictionary<Human, Human>();



for (int counter = 0; counter < objectCount; counter++)
{
nameAndAge.Add(Human.name, Human.age);
}
}
}
No description
67 Replies
Ahmed
AhmedOP14mo ago
im tryna add names and ages from all the objects
Tvde1
Tvde114mo ago
Can you tell us what the error at the red line says? :)
Ahmed
AhmedOP14mo ago
into a dictionary
Ahmed
AhmedOP14mo ago
No description
Tvde1
Tvde114mo ago
can you try guess what this error means?
Jimmacle
Jimmacle14mo ago
not gonna lie, this code makes 0 sense
Ahmed
AhmedOP14mo ago
i don't even know
Tvde1
Tvde114mo ago
you start somewhere, Jimmacle 😄
Ahmed
AhmedOP14mo ago
💀 word
Jimmacle
Jimmacle14mo ago
right but some extra info is needed to figure out what your end goal is, because it's not clear from the code itself
Tvde1
Tvde114mo ago
Human.name would try to access a field or property name which is static on Human
Hazel 🌊💃
Hazel 🌊💃14mo ago
If you're trying to use a dictionary, why can't you guess at what the error means? 🤔
Tvde1
Tvde114mo ago
but name is a non-static instance field
Ahmed
AhmedOP14mo ago
name isn't static
Hazel 🌊💃
Hazel 🌊💃14mo ago
That's their point.
Ahmed
AhmedOP14mo ago
it's public string
Tvde1
Tvde114mo ago
so you have to either use this.name or, take it from an instance of Human
cypherpotato
cypherpotato14mo ago
Add(name, age) just remove "Human" when u specify Human.age you're referencing the static field age
Hazel 🌊💃
Hazel 🌊💃14mo ago
Can't do that eithr. Need an instance.
Tvde1
Tvde114mo ago
we are in the ctor :)
Hazel 🌊💃
Hazel 🌊💃14mo ago
tacolost
cypherpotato
cypherpotato14mo ago
but hes running that from the constructor
Hazel 🌊💃
Hazel 🌊💃14mo ago
I'll see myself into my corner of shame.
cypherpotato
cypherpotato14mo ago
he is in the instance.
Ahmed
AhmedOP14mo ago
No description
Ahmed
AhmedOP14mo ago
ye so do i make them static
Tvde1
Tvde114mo ago
you don't want them static
cypherpotato
cypherpotato14mo ago
nameAndAge should be Dictionary<string, int>
Tvde1
Tvde114mo ago
you might want them public
cypherpotato
cypherpotato14mo ago
no
Ahmed
AhmedOP14mo ago
they r huh
cypherpotato
cypherpotato14mo ago
where are nameAndAge defined? ah i see now
Tvde1
Tvde114mo ago
yeah the next point is that the dictionary expects a Human as a key and a Human as a value. You probably want Name as a key and age as a value
Ahmed
AhmedOP14mo ago
i did that
cypherpotato
cypherpotato14mo ago
Dictionary<Human, Human> nameAndAge there you should add an keyvalue pair of humans and humans and im pretty sure you dont want it and what u got?
Ahmed
AhmedOP14mo ago
0 cause of my first line
Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.objectCount);
im meant to get 3 tho huh'
cypherpotato
cypherpotato14mo ago
why not? you called new Human() three times
Ahmed
AhmedOP14mo ago
idek apparently its cause im overpopulating my constructor
Tvde1
Tvde114mo ago
do you understand what the constructor is for?
cypherpotato
cypherpotato14mo ago
whats the purpose of nameAndAge dictionary?
Ahmed
AhmedOP14mo ago
not really nothing, just testing things
cypherpotato
cypherpotato14mo ago
constructor is an method that is called on the object initialization
Tvde1
Tvde114mo ago
the constructor is run whenever a new object Human is created
Ahmed
AhmedOP14mo ago
yeah ik
cypherpotato
cypherpotato14mo ago
for each new Human(), you call your constructor once on it
Ahmed
AhmedOP14mo ago
oh wait wait wait
Tvde1
Tvde114mo ago
😄
Ahmed
AhmedOP14mo ago
still didn't work i removed the loop you're right
Console.WriteLine(Human.objectCount);
Console.ReadLine();

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

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

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);

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

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

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

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);

}
}
but why is it still writing 0
cypherpotato
cypherpotato14mo ago
because you're calling it before you define each human you're calling objectCount++ on constructor, which is called on new Human
MODiX
MODiX14mo ago
The Dark Realm's No. 2 - Hazel
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

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

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);

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

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

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);

}
}
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.
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.
Compile: 769.700ms | Execution: 67.870ms | React with ❌ to remove this embed.
cypherpotato
cypherpotato14mo ago
what you mean? are u talking about the dictionary or the first writeline?
Tvde1
Tvde114mo ago
you first write the count and then add 3 humans :)
Jimmacle
Jimmacle14mo ago
code in a method runs from the top down, you have to do the stuff you want to run first at the top
cypherpotato
cypherpotato14mo ago
how to run this code snippet?
Hazel 🌊💃
Hazel 🌊💃14mo ago
Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.objectCount);
This is still wrong. $eval
MODiX
MODiX14mo ago
To compile C# code in Discord, use !eval Example:
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
!eval for(int i = 0; i < 4; i++) Console.WriteLine(i);
Please don't try breaking the REPL service. Also, remember to do so in #bot-spam!
Tvde1
Tvde114mo ago
is it?
Hazel 🌊💃
Hazel 🌊💃14mo ago
Oh chiz, it was made static; okay 😄 I stand corected.
Tvde1
Tvde114mo ago
Hazel are you sleepy?
Hazel 🌊💃
Hazel 🌊💃14mo ago
Yeah It always was 😭
Tvde1
Tvde114mo ago
😄
Hazel 🌊💃
Hazel 🌊💃14mo ago
Back to my corner of shame. Byeeeee lol
Jimmacle
Jimmacle14mo ago
maybe we should direct this person to writing the code in a more logical way instead of whatever this is turning into peepoE
MODiX
MODiX14mo ago
cypherpotato
REPL Result: Success
Human bob = new Human("Bob", 18);
Human bobby = new Human("Bobby", 16);
Human bibby = new Human("Bibby", 11);

// after declarations
Console.WriteLine(Human.objectCount);

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

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);

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

// after declarations
Console.WriteLine(Human.objectCount);

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

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);

}
}
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
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
Compile: 763.247ms | Execution: 113.162ms | React with ❌ to remove this embed.
cypherpotato
cypherpotato14mo ago
@Ahmed see the 3 in the end? is that what you're expecting?
Ahmed
AhmedOP14mo ago
i know why it wasn't loading in it's cause i had console.readline(); 💀 yes ye im an idiot i fixed it thank you everyone !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