C
C#2y 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
Angius2y ago
This method returns a number It doesn't convert in place After all, age is a string
Soinagie
SoinagieOP2y ago
But I converted it
Angius
Angius2y 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
SoinagieOP2y ago
Returns to where?
Angius
Angius2y ago
Just... returns To the left of it, let's say Like any method that returns anything
Soinagie
SoinagieOP2y ago
But I want my string to be an int
Angius
Angius2y 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
SoinagieOP2y ago
Then whats convert even for?
Angius
Angius2y ago
For... converting a string into an int, in this case? I mean, it does what you expect it to
Soinagie
SoinagieOP2y ago
You said it doesnt convert Just returns
Angius
Angius2y ago
It converts the string into an integer and returns that integer
Soinagie
SoinagieOP2y ago
Yeah
Angius
Angius2y ago
As I said, C# is statically and strictly typed
Soinagie
SoinagieOP2y ago
But it doesnt work
Angius
Angius2y ago
A variable cannot change its type It doesn't work because you're using it wrong
Soinagie
SoinagieOP2y ago
. .
Angius
Angius2y ago
You're not using what the method returns Simple as
Soinagie
SoinagieOP2y ago
You said 2 opposite things
Angius
Angius2y ago
I didn't? The method doesn't change the type of any variable?
Soinagie
SoinagieOP2y ago
What method Im just using main
Angius
Angius2y ago
Jesus $helloworld
Soinagie
SoinagieOP2y ago
Nothing else
Angius
Angius2y ago
Start with the basics
Soinagie
SoinagieOP2y ago
I want my string to be converted to int
MODiX
MODiX2y 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
Angius2y ago
And, really, go through the linked tutorials You need to know the type system You need to know what methods are
Soinagie
SoinagieOP2y ago
What type system
MODiX
MODiX2y ago
MODiX#0152
Quoted by
<@!85903769203642368> from #can't '-' int (click here)
React with ❌ to remove this embed.
Angius
Angius2y ago
That types in C# are strict and static and what it means, for example
Soinagie
SoinagieOP2y 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
Angius2y ago
Why would it be? It's not empty
Soinagie
SoinagieOP2y ago
int number =
int number =
Angius
Angius2y ago
Angius
Angius2y ago
It's... not empty?
Soinagie
SoinagieOP2y ago
So you did convert that string after all
Angius
Angius2y ago
Yes?
Soinagie
SoinagieOP2y ago
But you said I can't
Angius
Angius2y ago
You can't convert IN PLACE
Soinagie
SoinagieOP2y ago
What place? Wdym?
Pobiega
Pobiega2y 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 👻 🎃
you can't change the type of age
Angius
Angius2y 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 // Kronos2y ago
"wholly and entirely unchanged" very descriptive
Soinagie
SoinagieOP2y ago
They just said I cant I have to make new variable
x0rld 👻 🎃
yes it's normal
Soinagie
SoinagieOP2y ago
Yes what?
x0rld 👻 🎃
age is define as a string you have to create a new variable to have an int
Soinagie
SoinagieOP2y ago
So I cant change its type like you said
Pobiega
Pobiega2y ago
you missed a t in cant
x0rld 👻 🎃
notlikethis thx
Soinagie
SoinagieOP2y 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
Accord2y 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.
Want results from more Discord servers?
Add your server