C#
C#

help

Root Question Message

shawski
shawski12/6/2022
✅ need help with a problem

public Person(int initialAge)
    {
        // Add some more code to run some checks on initialAge
        if (initialAge < 0)
        {
            Console.WriteLine("Age is not valid, setting age to 0.");
            age = 0;
        }
        initialAge = age;

    }
    public void amIOld()
    {
        // Do some computations in here and print out the correct statement to the console 
        List<int> ages = new List<int>();
        ages.Add(age);

        foreach (int i in ages)
        {
            if (i < 13)
            {
                Console.WriteLine("You are young.");
            }
            else if (i >= 13 && i < 18)
            {
                Console.WriteLine("You are a teenager.");
            }
            else 
            {
                Console.WriteLine("You are old.");
            }
        }
    }

why does it print "you are young" no matter what the input is?
AntonC
AntonC12/6/2022
ages.Add(age)
what's the age here?
AntonC
AntonC12/6/2022
age seems to never be assigned, so it's always zero
shawski
shawski12/6/2022
thats supposed to add the user input to a list
shawski
shawski12/6/2022
unless i did it wrong
AntonC
AntonC12/6/2022
what's the point of doing the list? just check the age
shawski
shawski12/6/2022
yeah you're right
shawski
shawski12/6/2022
im not sure why i did that
shawski
shawski12/6/2022
gimme a sec
shawski
shawski12/6/2022
i believe i know what i did wrong
shawski
shawski12/6/2022
ill lyk if it works
shawski
shawski12/6/2022
alright i got it working
shawski
shawski12/6/2022
i assigned age wrong and messed up the initial check
shawski
shawski12/6/2022
all's good now though
AntonC
AntonC12/6/2022
don't name your member variables like your locals btw
AntonC
AntonC12/6/2022
prefixing with an underscore is best practice
shawski
shawski12/6/2022
i didnt name any of those. it was a practice problem i was trying to solve.
shawski
shawski12/6/2022
wdym?
AntonC
AntonC12/6/2022
_age
AntonC
AntonC12/6/2022
if it's a private field
shawski
shawski12/6/2022
alright
AntonC
AntonC12/6/2022
public fields aren't usually used at all
shawski
shawski12/6/2022
good to know.
shawski
shawski12/6/2022
my job was just to write the methods
AntonC
AntonC12/6/2022
I personally name them like properties when I do use them
shawski
shawski12/6/2022
so i should do this instead?
shawski
shawski12/6/2022
public int _age;
shawski
shawski12/6/2022
if i were to write the whole thing myself
AntonC
AntonC12/6/2022
private
AntonC
AntonC12/6/2022
i just told you you don't typically use public fields
shawski
shawski12/7/2022
i said before, i didnt write this. i just wrote inside the method, not around it.
AntonC
AntonC12/7/2022
bruh you just typed above "if i were to write the whole thing myself"
shawski
shawski12/7/2022
yeah
shawski
shawski12/7/2022
i forgot i said that
shawski
shawski12/7/2022
so why should i add _ to age
shawski
shawski12/7/2022
im curious
shawski
shawski12/7/2022
is it just conventionally how private fields are named?
AntonC
AntonC12/7/2022
mostly to visually distinguish them from local variables
shawski
shawski12/7/2022
alright thanks for that tip
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy