C
C#7mo ago
LESZEK

✅ help

No description
20 Replies
LESZEK
LESZEK7mo ago
why its dont work
SinFluxx
SinFluxx7mo ago
Hover over the highlighted lines to get an idea of what's wrong
SinFluxx
SinFluxx7mo ago
for example:
No description
Angius
Angius7mo ago
Console.ReadLine() returns string? a is of type int Square peg does not fit round hole
SinFluxx
SinFluxx7mo ago
Also if you're assigning them to string variables straight after anyway, may as well cut out the middleman and get rid of int a
Angius
Angius7mo ago
And of Convert.ToString() Since it basically does "turn this string into a string"
LESZEK
LESZEK7mo ago
string str1 , str2 ; int a ; Console.WriteLine("napisz 1 liczbe"); a = Console.ReadLine(); str1 = Convert.ToString(a); Console.WriteLine("napisz 2 liczbe"); a = Console.ReadLine(); str2 = Convert.ToString(a); string result = str1 + str2; Console.WriteLine( "wynik " + result); and now its have two mistake
Angius
Angius7mo ago
a is still of type int
LESZEK
LESZEK7mo ago
in 4 line and in 7 line
Angius
Angius7mo ago
Did you read anything we said?
LESZEK
LESZEK7mo ago
so the int type cannot be converted to the string type?
Angius
Angius7mo ago
Console.ReadLine() returns a string? Not an int Not a bool Not a DateTime A string? If you want to assign the value of Console.ReadLine() to a variable, that variable must be of type string? Not int Not bool Not DateTime
LESZEK
LESZEK7mo ago
if i want it to work i must write this? int str1 , str2 ; string a ; Console.WriteLine("napisz 1 liczbe");
a = Console.ReadLine(); str1 = Convert.ToInt32(a); Console.WriteLine("napisz 2 liczbe"); a = Console.ReadLine(); str2 = Convert.ToInt32(a); int result = str1 + str2; Console.WriteLine( "wynik " + result);
Angius
Angius7mo ago
Yes, this would be more correct
LESZEK
LESZEK7mo ago
ok because I didn't know that Ican't convert from string to int
ZacharyPatten
ZacharyPatten7mo ago
you can, just not implicitly
Angius
Angius7mo ago
You just did convert from a string to an int though? Convert.ToInt32() does just that Takes a string Returns an int
ZacharyPatten
ZacharyPatten7mo ago
but you should use int.Parse(...) instead of Convert.ToInt32(...) if you need to convert a string to an int
MODiX
MODiX7mo ago
When you don't know if a string is actually a number when handling user input, use int.TryParse (or variants, e.g. double.TryParse)
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
if(int.TryParse("123", out int number))
{
var total = number + 1;
Console.WriteLine(total); // output: 124
}
TryParse returns a bool, where true indicates successful parsing. Remarks: - Avoid int.Parse if you do not know if the value parsed is definitely a number. - Avoid Convert.ToInt32 entirely, this is an older method and Parse should be preferred where you know the string can be parsed. Read more here
Want results from more Discord servers?
Add your server
More Posts
Custom cmdlet giving me a missing assembly errorPowershell gives me this error when I try run my cmdlet: ```Get-UserFromId: Could not load file or aEF navigation property is unattached from dbContextHello! There are 2 Entities in relation with each other: `User` and `Domain`. When I want to insert Incorrect syntax near '@address_id'Hello, I'm currently working on a School project and I want to insert something into the Database buNo authenticationScheme was specified, and there was no DefaultChallengeScheme found.Hello, there! I added JWT Authentication to my ASP.NET Core Web API project by this code snipper: `MVVM WPF - Change Color in ColorAnimation in Style ControlTemplate on RuntimeI have various styles with color animations, and I need to change the "To" color of a color animatioSmart enums, inheritance and "siblings" behaving weirdly and I don't understand why.So, I'm using `Ardalis.SmartEnum` to create some smart enums with data members. I found myself haviHey guys I need some troubleshooting with a C# task using BFSI have a task that I's mostly working but sometimes I have undesirable outputs and not behaving righMVVM - View model with collection of view models?Within MVVM, can a view model have a collection of view models? Is it okay to do: ``` class BasketV2 QuestionsHi, I need to ask 2 questions as i am new to C#. So im missing 2 tasks to deploy my first app. And iRaw SQL Query QuestionI have a raw SQL query that I need to execute against a database that's external to my application.