C
C#2y ago
احمد

❔ ✅ 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
احمد
احمدOP2y ago
im tryna add names and ages from all the objects
Tvde1
Tvde12y ago
Can you tell us what the error at the red line says? :)
احمد
احمدOP2y ago
into a dictionary
احمد
احمدOP2y ago
No description
Tvde1
Tvde12y ago
can you try guess what this error means?
Jimmacle
Jimmacle2y ago
not gonna lie, this code makes 0 sense
احمد
احمدOP2y ago
i don't even know
Tvde1
Tvde12y ago
you start somewhere, Jimmacle 😄
احمد
احمدOP2y ago
💀 word
Jimmacle
Jimmacle2y 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
Tvde12y ago
Human.name would try to access a field or property name which is static on Human
Hazel 🌊💃
If you're trying to use a dictionary, why can't you guess at what the error means? 🤔
Tvde1
Tvde12y ago
but name is a non-static instance field
احمد
احمدOP2y ago
name isn't static
Hazel 🌊💃
That's their point.
احمد
احمدOP2y ago
it's public string
Tvde1
Tvde12y ago
so you have to either use this.name or, take it from an instance of Human
cypherpotato
cypherpotato2y ago
Add(name, age) just remove "Human" when u specify Human.age you're referencing the static field age
Hazel 🌊💃
Can't do that eithr. Need an instance.
Tvde1
Tvde12y ago
we are in the ctor :)
cypherpotato
cypherpotato2y ago
but hes running that from the constructor
Hazel 🌊💃
I'll see myself into my corner of shame.
cypherpotato
cypherpotato2y ago
he is in the instance.
احمد
احمدOP2y ago
No description
احمد
احمدOP2y ago
ye so do i make them static
Tvde1
Tvde12y ago
you don't want them static
cypherpotato
cypherpotato2y ago
nameAndAge should be Dictionary<string, int>
Tvde1
Tvde12y ago
you might want them public
cypherpotato
cypherpotato2y ago
no
احمد
احمدOP2y ago
they r huh
cypherpotato
cypherpotato2y ago
where are nameAndAge defined? ah i see now
Tvde1
Tvde12y 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
احمد
احمدOP2y ago
i did that
cypherpotato
cypherpotato2y 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?
احمد
احمدOP2y ago
0 cause of my first line
Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.objectCount);
im meant to get 3 tho huh'
cypherpotato
cypherpotato2y ago
why not? you called new Human() three times
احمد
احمدOP2y ago
idek apparently its cause im overpopulating my constructor
Tvde1
Tvde12y ago
do you understand what the constructor is for?
cypherpotato
cypherpotato2y ago
whats the purpose of nameAndAge dictionary?
احمد
احمدOP2y ago
not really nothing, just testing things
cypherpotato
cypherpotato2y ago
constructor is an method that is called on the object initialization
Tvde1
Tvde12y ago
the constructor is run whenever a new object Human is created
احمد
احمدOP2y ago
yeah ik
cypherpotato
cypherpotato2y ago
for each new Human(), you call your constructor once on it
احمد
احمدOP2y ago
oh wait wait wait
Tvde1
Tvde12y ago
😄
احمد
احمدOP2y 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
cypherpotato2y ago
because you're calling it before you define each human you're calling objectCount++ on constructor, which is called on new Human
MODiX
MODiX2y 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
cypherpotato2y ago
what you mean? are u talking about the dictionary or the first writeline?
Tvde1
Tvde12y ago
you first write the count and then add 3 humans :)
Jimmacle
Jimmacle2y 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
cypherpotato2y ago
how to run this code snippet?
Hazel 🌊💃
Console.WriteLine(Human.objectCount);
Console.WriteLine(Human.objectCount);
This is still wrong. $eval
MODiX
MODiX2y 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
Tvde12y ago
is it?
Hazel 🌊💃
Oh chiz, it was made static; okay 😄 I stand corected.
Tvde1
Tvde12y ago
Hazel are you sleepy?
Hazel 🌊💃
Yeah It always was 😭
Tvde1
Tvde12y ago
😄
Hazel 🌊💃
Back to my corner of shame. Byeeeee lol
Jimmacle
Jimmacle2y 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
MODiX2y 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
cypherpotato2y ago
@Ahmed see the 3 in the end? is that what you're expecting?
احمد
احمدOP2y 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
Accord2y 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.

Did you find this page helpful?