C
C#17mo ago
Soinagie

❔ can't '-' int

String age = "";
Convert.toInt32(age);
Console.WriteLine (age - 1);
Console.ReadKey();
String age = "";
Convert.toInt32(age);
Console.WriteLine (age - 1);
Console.ReadKey();
Why can't I minus 1 an int? Its a number
53 Replies
Angius
Angius17mo ago
This method returns a number It doesn't convert in place After all, age is a string
Soinagie
Soinagie17mo ago
But I converted it
Angius
Angius17mo ago
And it can only ever remain a string, it can only ever hold a string, it cannot, under any circumstance, hold an integer Convert.ToInt32() RETURNS a number It doesn't change the variable you passed
Soinagie
Soinagie17mo ago
Returns to where?
Angius
Angius17mo ago
Just... returns To the left of it, let's say Like any method that returns anything
Soinagie
Soinagie17mo ago
But I want my string to be an int
Angius
Angius17mo ago
age was declared as a string, so it can only ever be a string You need a new variable An int type variable
Soinagie
Soinagie17mo ago
Then whats convert even for?
Angius
Angius17mo ago
For... converting a string into an int, in this case? I mean, it does what you expect it to
Soinagie
Soinagie17mo ago
You said it doesnt convert Just returns
Angius
Angius17mo ago
It converts the string into an integer and returns that integer
Soinagie
Soinagie17mo ago
Yeah
Angius
Angius17mo ago
As I said, C# is statically and strictly typed
Soinagie
Soinagie17mo ago
But it doesnt work
Angius
Angius17mo ago
A variable cannot change its type It doesn't work because you're using it wrong
Soinagie
Soinagie17mo ago
. .
Angius
Angius17mo ago
You're not using what the method returns Simple as
Soinagie
Soinagie17mo ago
You said 2 opposite things
Angius
Angius17mo ago
I didn't? The method doesn't change the type of any variable?
Soinagie
Soinagie17mo ago
What method Im just using main
Angius
Angius17mo ago
Jesus $helloworld
Soinagie
Soinagie17mo ago
Nothing else
Angius
Angius17mo ago
Start with the basics
Soinagie
Soinagie17mo ago
I want my string to be converted to int
MODiX
MODiX17mo ago
Angius#1586
REPL Result: Success
string text = "420";
int number = Convert.ToInt32(text);
Console.Write(number * 2);
string text = "420";
int number = Convert.ToInt32(text);
Console.Write(number * 2);
Console Output
840
840
Compile: 607.266ms | Execution: 34.749ms | React with ❌ to remove this embed.
Angius
Angius17mo ago
And, really, go through the linked tutorials You need to know the type system You need to know what methods are
Soinagie
Soinagie17mo ago
What type system
MODiX
MODiX17mo ago
MODiX#0152
Quoted by
<@!85903769203642368> from #can't '-' int (click here)
React with ❌ to remove this embed.
Angius
Angius17mo ago
That types in C# are strict and static and what it means, for example
Soinagie
Soinagie17mo ago
I know what methods are And Im using only main I just want my string to be an int but it shows error Why is this int number empty?
Angius
Angius17mo ago
Why would it be? It's not empty
Soinagie
Soinagie17mo ago
int number =
int number =
Angius
Angius17mo ago
Angius
Angius17mo ago
It's... not empty?
Soinagie
Soinagie17mo ago
So you did convert that string after all
Angius
Angius17mo ago
Yes?
Soinagie
Soinagie17mo ago
But you said I can't
Angius
Angius17mo ago
You can't convert IN PLACE
Soinagie
Soinagie17mo ago
What place? Wdym?
Pobiega
Pobiega17mo ago
String age = "";
Convert.toInt32(age);
String age = "";
Convert.toInt32(age);
is NOT THE SAME as
String age = "";
int number = Convert.toInt32(age);
String age = "";
int number = Convert.toInt32(age);
x0rld
x0rld17mo ago
you can't change the type of age
Angius
Angius17mo ago
Variable text is STILL a string, and it STILL holds the value "420" Variable number is NEW and its type is int The method converted the value and assigned it to a new variable The old variable remains wholly and entirely unchanged
RO // Kronos
RO // Kronos17mo ago
"wholly and entirely unchanged" very descriptive
Soinagie
Soinagie17mo ago
They just said I cant I have to make new variable
x0rld
x0rld17mo ago
yes it's normal
Soinagie
Soinagie17mo ago
Yes what?
x0rld
x0rld17mo ago
age is define as a string you have to create a new variable to have an int
Soinagie
Soinagie17mo ago
So I cant change its type like you said
Pobiega
Pobiega17mo ago
you missed a t in cant
x0rld
x0rld17mo ago
notlikethis thx
Soinagie
Soinagie17mo ago
Ok so variable is always same type and I have to make a new one with the same value for it to work
Accord
Accord17mo ago
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.