How do i get this to work?

//int x;
Console.WriteLine("Please Enter a number");
int Entry = Console.ReadLine();
//string Entry = int.TryParse(Console.ReadLine(), out x);(what ive tired)

for (int i = x; i < 10; i++)
{
Console.WriteLine($"Go up by {Entry}");
}}
//int x;
Console.WriteLine("Please Enter a number");
int Entry = Console.ReadLine();
//string Entry = int.TryParse(Console.ReadLine(), out x);(what ive tired)

for (int i = x; i < 10; i++)
{
Console.WriteLine($"Go up by {Entry}");
}}
error: Cannot implicitly convert type 'bool' to 'string' i clicked on the error and it told me to go here https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0029?f1url=%3FappId%3Droslyn%26k%3Dk(CS0029) here i see are only ints being converted to long is what im doing even possible?
57 Replies
leowest
leowest4mo ago
Let's try to make this as simple as possible. A method can return something or nothing. ReadLine is a method from the Console class. As you can see in this image, this method in particular will return something.
No description
leowest
leowest4mo ago
This is from the docs. So we know ReadLine will return a string. So obviously u cannot do.
int entry = Console.ReadLine();
int entry = Console.ReadLine();
because int is not a string. Now let's go a bit more complex... Methods can have parameters that you pass into it.
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
as you can see the TryParse method have 2 parameters a string and an int and it also returns a bool so we know that ReadLine gives us a string right? so we can give ReadLine to TryParse because it will provide it with a string and we know that the second parameter will output a result of type int we can later use. and we also know that it will return a bool. i.e.:
bool result = int.TryParse(Console.ReadLine(), out int value);
bool result = int.TryParse(Console.ReadLine(), out int value);
so when this line executes, it will go into the Console.ReadLine() and ask the user for an input and pass it to the method. which will then evaluate if the inpu is true or false and store it to result if it was successful value will have the actual value, if it failed value will have a default value give nto it, in the case of an int that value is 0. that is why its important to use a condition before you use the value so you dont mistakenly use it when its invalid
Jexs(ping me)
Jexs(ping me)OP4mo ago
give me one sec to read this thanks for the reply! Q1. When they say 'Returns the next line of character stream' by stream would that be my input? like what i put into the method via the cmd pop up? q2.i notice that it returns (by returns i mean converts a string into an int) a int value how would it q4 to test wether bool result = int.TryParse(Console.ReadLine(), out int value); returned true or false i just added console.writeline(result) to the code and i got it to print true or false is this a way to check the LOC it self to see if thats what it does is this a standard pratice of some type of checking system? sorry for late reply had to re read a few times
leowest
leowest4mo ago
q1 anything typed until u hit enter q2 can you be more specific here as to which method and location are u referring q4 no that is not checking anything, that is just outputting the value it holds checking would be using a condition to properly evaluate whether u should go to path A or B i.e.:
if (result)
{
// do something when result is true
}
else
{
// do something when result is false
}
if (result)
{
// do something when result is true
}
else
{
// do something when result is false
}
Here we are checking if result goes to path A or B and acting as needed.
Jexs(ping me)
Jexs(ping me)OP4mo ago
q1 clear! ty q2 i just read my question and idk even know why i typed that? q4 when you ouptting the value it holds i notice that if i enter the letter e its false if i enter the number 4 its true is what i mean
leowest
leowest4mo ago
yes the idea of the int.TryParse method is that it wants to verify all the characters typed are in fact digits that can be used to compose a whole number
Jexs(ping me)
Jexs(ping me)OP4mo ago
okay ty bud im gonna be re reading this chat today and tomrrow! ty!
leowest
leowest4mo ago
just practice types and if ur using visual studio $typehints
MODiX
MODiX4mo ago
Visual Studio 2019 and above can now give you inline hints of the type in lambda, var, etc, see the animated GIF on how to enable it: https://cdn.discordapp.com/attachments/169726586931773440/869498806343962694/2021-07-27_05-35-23.gif See the below image for an example of what it looks like the white line was made by me to illustrate what the above does: https://media.discordapp.net/attachments/569261465463160900/894398422365372416/unknown.png NOTE: You can also use it by pressing ALT + F1! You might need to go on the settings and disable/reenable it for it to work!
leowest
leowest4mo ago
it helps displaying what the type is so it might make things clearer for u
Jexs(ping me)
Jexs(ping me)OP4mo ago
i think i turned them off but i'lll re enable
Jexs(ping me)
Jexs(ping me)OP4mo ago
mines a bit different?
No description
leowest
leowest4mo ago
scroll down?
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
im off to bed but its there anyway just search the name at the top
Jexs(ping me)
Jexs(ping me)OP4mo ago
. the way to tell if something returns something is if it contains parenthesis ?
leowest
leowest4mo ago
no, its by looking at the type before the method name usually the parenthesis indicates whether the method has or not parameters if its empty () then it has no parameters in the case of ReadLine for example TryParse for example has multiple parameters so what comes before the method name is what define what type its returning or if its returning nothing aka void
leowest
leowest4mo ago
Methods - C#
A method in C# is a code block that contains a series of statements. A program runs the statements by calling the method and specifying arguments.
leowest
leowest4mo ago
No description
Jexs(ping me)
Jexs(ping me)OP4mo ago
are all data types return types? @leowest
Angius
Angius4mo ago
"return types" are not sume super special subset of "types" It's just a type that a method returns int Foo() has int as the return type Person Bar() has Person as the return type (int, string)[] Baz() has an array of (int, string) tuples as the return type And so on
Jexs(ping me)
Jexs(ping me)OP4mo ago
so public static string? ReadLine(); has a return type of string
Angius
Angius4mo ago
Of string? Which is "string or null"
Jexs(ping me)
Jexs(ping me)OP4mo ago
the return types would be string or null ?
Angius
Angius4mo ago
The return type would be string?
Jexs(ping me)
Jexs(ping me)OP4mo ago
would it also be null?
Angius
Angius4mo ago
What do you mean?
Jexs(ping me)
Jexs(ping me)OP4mo ago
uh
Angius
Angius4mo ago
Yes, string? can be a string, and can also be null
Jexs(ping me)
Jexs(ping me)OP4mo ago
and string and null both be returned types?
Angius
Angius4mo ago
The return type of that method is string? Which means the method can return a string or a null So, in that way, I guess, yeah
Jexs(ping me)
Jexs(ping me)OP4mo ago
so the return type is string? but the VALUES it can return are either a string or a null value?
Angius
Angius4mo ago
string? Foo()
{
if (Random.Shared.Next(0, 10) > 5)
{
return "blah";
}
return null;
}
string? Foo()
{
if (Random.Shared.Next(0, 10) > 5)
{
return "blah";
}
return null;
}
Yes
Jexs(ping me)
Jexs(ping me)OP4mo ago
okay! 😮
Angius
Angius4mo ago
It would be clearer if C# had union types, the return would be string|null then
Jexs(ping me)
Jexs(ping me)OP4mo ago
so in
cs
bool IsEven(int number)
{
return number % 2 == 0;
}
cs
bool IsEven(int number)
{
return number % 2 == 0;
}
the return type is bool ?
Angius
Angius4mo ago
Makes it much more obvious than a random ? But it is what it is Yes
Jexs(ping me)
Jexs(ping me)OP4mo ago
gonna put this in my notes! im getting return confused with something else when i run this code:
int Add(int a, int b)
{
return a + b;
}
int Add(int a, int b)
{
return a + b;
}
and i typed something like Add(1,4); would the value of 1,4 be a 'Return' is that the word we use?
Angius
Angius4mo ago
Not sure what you mean? Add(1, 4) would return 5
Jexs(ping me)
Jexs(ping me)OP4mo ago
uh when passing 1,4 and having it SPIT OUT 5 be called a return type or a return ? are they the same thing ?
Angius
Angius4mo ago
5 would be the return value, I guess? Return type is int
Jexs(ping me)
Jexs(ping me)OP4mo ago
okay!
leowest
leowest4mo ago
No description
leowest
leowest4mo ago
its a combination they have to match to work what u put in return must match with the signature u have in your method so if u have int u can't return double or string
Jexs(ping me)
Jexs(ping me)OP4mo ago
'return type' is the type of value from the list of types lets say (int,string,float,bool,char) ...? ff ive lost my self so return type = the value that gets spitted out right?
leowest
leowest4mo ago
return type is the type that you must output when returning a value if anything
MODiX
MODiX4mo ago
leowest
REPL Result: Failure
int Add(double a, int b) { return a + b; }
int Add(double a, int b) { return a + b; }
Exception: CompilationErrorException
- Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
- Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)
Quoted by
<@1102729783969861782> from #bot-spam (click here)
Compile: 275.068ms | Execution: 0.000ms | React with ❌ to remove this embed.
leowest
leowest4mo ago
as you can see the addition there would result in a double so it cannot return the double, because it wants u to provide it with an int
MODiX
MODiX4mo ago
leowest
REPL Result: Failure
int Add(double a, int b) { return "text"; }
int Add(double a, int b) { return "text"; }
Exception: CompilationErrorException
- Cannot implicitly convert type 'string' to 'int'
- Cannot implicitly convert type 'string' to 'int'
Compile: 228.849ms | Execution: 0.000ms | React with ❌ to remove this embed.
leowest
leowest4mo ago
the type u define in the method signature is the rule for what it will return to whoever call that method
Jexs(ping me)
Jexs(ping me)OP4mo ago
this is a side question buy both public and static are access modifiers? `cs public static
Angius
Angius4mo ago
public yes, static no
Jexs(ping me)
Jexs(ping me)OP4mo ago
ty ! @Angius is there a difference between an access modifer anda modifer ?
Angius
Angius4mo ago
Uh, like squares and rectangles, I guess? An access modifier is a modifier A modifier is not necessarily an access modifier
Jexs(ping me)
Jexs(ping me)OP4mo ago
oh

Did you find this page helpful?