✅ How can i fix this?

i was wondering if someone could help me write my asking for age method error is Local variable 'ageInput' might not be initialized before accessing error in LOC 49 https://paste.mod.gg/kihrmrjohlvd/0 what ive tried and looked at https://stackoverflow.com/questions/57947128/use-of-unassigned-local-variable-in-an-if-statement and tried changing
cs
int ageInput;
to something like intageinput = ConvertInt32(Console.Readline())
cs
int ageInput;
to something like intageinput = ConvertInt32(Console.Readline())
but that didnt work either e.o
BlazeBin - kihrmrjohlvd
A tool for sharing your source code with the world!
Stack Overflow
Use of unassigned local variable in an if statement
So I'm trying to start coding again(done a year in college before and thinking of going back but need to get back into swing of things. Doing this simple console application and getting this error....
18 Replies
Jexs(ping me)
Jexs(ping me)OP2mo ago
sorry for the double post!
Zarez
Zarez2mo ago
You are not assigning any value to the ageInput, neither to age_1. If you wanna take any value from user's input, you need to call the Console.ReadLine() with assignment to the variable. For example as you did in the Main() function: inputText = Console.ReadLine(); Basically the errors occurs cuz you are trying to parse a variable that wasn't initialized with any value yet. Example solution:
string ageInput;

Console.WriteLine("You must be 18 or older to continue:");
Thread.Sleep(1000);
Console.WriteLine("How old are you?: ");
ageInput = Console.ReadLine();
bool isAgeParsed = int.TryParse(ageInput, out int age);

if(!isAgeParsed){
Console.WriteLine("The age must be a number!");
return; // or continue, depends on the method logic
}

Console.WriteLine($"You are {age} years old.");
...
string ageInput;

Console.WriteLine("You must be 18 or older to continue:");
Thread.Sleep(1000);
Console.WriteLine("How old are you?: ");
ageInput = Console.ReadLine();
bool isAgeParsed = int.TryParse(ageInput, out int age);

if(!isAgeParsed){
Console.WriteLine("The age must be a number!");
return; // or continue, depends on the method logic
}

Console.WriteLine($"You are {age} years old.");
...
Jexs(ping me)
Jexs(ping me)OP2mo ago
Thank you for the reply i will read into this. im trying to make a class then a method with the functionality of asking if the user is he/she is and exiting the program if the user underage and continues when the person is older then 18 my brain tells me thats doable like can i do this outside of the main method?
Zarez
Zarez2mo ago
You can make up another class and initialize it to use its properties, methods, etc
Jexs(ping me)
Jexs(ping me)OP2mo ago
yeah okay
Zarez
Zarez2mo ago
public MyClass {
//region variables
...
//endregion

// example method
public bool checkAge(){
// logic for checking age, return true or false
...
}
}
...

Main(...){
MyClass instanceOfMyClass = new MyClass(); // or just = new();
bool isAgeOkay = instanceOfMyClass.checkAge();
if(!isAgeOkay){
// quit program
}
}
public MyClass {
//region variables
...
//endregion

// example method
public bool checkAge(){
// logic for checking age, return true or false
...
}
}
...

Main(...){
MyClass instanceOfMyClass = new MyClass(); // or just = new();
bool isAgeOkay = instanceOfMyClass.checkAge();
if(!isAgeOkay){
// quit program
}
}
Jexs(ping me)
Jexs(ping me)OP2mo ago
oh classes go on top!
Zarez
Zarez2mo ago
it's not really important, in general each class should be placed in other file for cleanlines
Jexs(ping me)
Jexs(ping me)OP2mo ago
okay!
Zarez
Zarez2mo ago
some other programming languages needs it, but for C# it's not
Jexs(ping me)
Jexs(ping me)OP2mo ago
this is whati have so far https://paste.mod.gg/aewkbrqlmrvf/0 please ignore the comments i use them to help me learn
BlazeBin - aewkbrqlmrvf
A tool for sharing your source code with the world!
Jexs(ping me)
Jexs(ping me)OP2mo ago
theses are th errors i have
No description
Jexs(ping me)
Jexs(ping me)OP2mo ago
i think i corrected the first one @Zarez Non-invocable member 'askingForAge' cannot be used like a method. is that because i set the return type to void?
Zarez
Zarez2mo ago
You cannot use class as a method: askingForAge(age);, that's wrong, you have to initialize the class (call constructor) and then use the class' public methods. Follow that what I've written here. Also, the name of classes should start with capital letter. The void declaration means that the method doesn't return any data. It doesn't mean that you can't invoke that method.
Zarez
Zarez2mo ago
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Jexs(ping me)
Jexs(ping me)OP2mo ago
Okay
Zarez
Zarez2mo ago
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Jexs(ping me)
Jexs(ping me)OP2mo ago
I’ll look into this tomrrow it’s late af for me rn thanks for the help bud !!

Did you find this page helpful?