12 Replies - 507 Views - Last Post: 22 August 2018 - 01:20 PM Rate Topic: **--- 2 Votes

#1 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:26 AM

Hi there,

I am having a hard time with solving this one, whatis the solution for this?

The error message:

Cannot implicit convert type 'bool' to 'int'

I am running this under while state, the array is string form
Is This A Good Question/Topic? 0
  • +

Replies To: array[0] = int.TryParse(Console.ReadLine(), out int write);

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14498
  • View blog
  • Posts: 58,118
  • Joined: 12-June 08

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:29 AM

Head to the MSDN docs and see what 'int.tryparse' does. What it returns. Etc. Should be pretty clear.

https://docs.microso..._System_Int32__
Was This Post Helpful? 1
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,258
  • Joined: 05-May 12

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:31 AM

If you look at the documentation, you'll see that int.TryParse() returns a boolean. You can't assign a boolean to a string.

Out of curiosity, what book or tutorial are you following to learn C#? From out point of view just watching your questions come in, it looks like you are taking a shotgun approach to learning the language and the .NET Framework.

modi123_1: Did it again! Jinx!
Was This Post Helpful? 3
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,736
  • Joined: 03-December 12

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:33 AM

Did you happen to go to the link I posted?
Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • Posts: 849
  • Joined: 05-December 13

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:41 AM

You cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. If you try to convert a bool to int with an implicit cast, you receive an errors. You could manipulate memory data, but Its also unsafe to do, and I won't be showing how to do that on a public forum as it could be used maliciously.
Was This Post Helpful? 0
  • +
  • -

#6 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:50 AM

View PostSkydiver, on 22 August 2018 - 09:31 AM, said:

If you look at the documentation, you'll see that int.TryParse() returns a boolean. You can't assign a boolean to a string.

Out of curiosity, what book or tutorial are you following to learn C#? From out point of view just watching your questions come in, it looks like you are taking a shotgun approach to learning the language and the .NET Framework.

modi123_1: Did it again! Jinx!


i dont know man i just go hard-core and try forward and google things up.

Mainly i use a swedish forum and there i get results quick, but been banned for 24 hours because I deleted my posts after getting answers....

Are you experienced enough to show me a solution to this problem?
Was This Post Helpful? 0
  • +
  • -

#7 ndc85430   User is online

  • I think you'll find it's "Dr"
  • member icon

Reputation: 895
  • View blog
  • Posts: 3,605
  • Joined: 13-June 14

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 09:55 AM

What didn't you understand about Sheepings' post? This shouldn't be too complicated a problem to fix if you understand what they wrote.

Also, there's really no need to quote posts in their entirety. Just quote the small part you want to address, or address the (honourable) member by name.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14498
  • View blog
  • Posts: 58,118
  • Joined: 12-June 08

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 10:01 AM

View Postdaygamer, on 22 August 2018 - 11:50 AM, said:

but been banned for 24 hours because I deleted my posts after getting answers....

I can see that being a problem. Heads up - don't do that here either.

View Postdaygamer, on 22 August 2018 - 11:50 AM, said:

Are you experienced enough to show me a solution to this problem?

Please be cognizant of the rules about asking people to do the work for you.
Was This Post Helpful? 0
  • +
  • -

#9 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • Posts: 849
  • Joined: 05-December 13

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 10:23 AM

If you are insisting to find a solution, then you will need to rewrite your own .net framework, **Cynicism implied** because you cannot do that.

Perhaps you missed what I and the other contributors had advised to you @daygamer? You can't do it, (not properly, anyway and any possible way wouldn't be advised and would be considered unsafe). Going against standard practices is not something I do when writing code.

I suggest you change the method you're using and adapt your code according to what you are allowed to convert.

View PostSheepings, on 22 August 2018 - 05:41 PM, said:

You cannot implicitly convert from bool to int. The C# compiler uses this rule to enforce program correctness. If you try to convert a bool to int with an implicit cast, you receive an errors. You could manipulate memory data, but Its also unsafe to do, and I won't be showing how to do that on a public forum as it could be used maliciously.

This post has been edited by Sheepings: 22 August 2018 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#10 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • Posts: 10,736
  • Joined: 03-December 12

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 10:28 AM

If I am interpreting what you actually want, you want the number in the array, not the boolean value. Since TryParse returns a boolean, it is just there to let you know if it converted it successfully. You want the out value, not the return from the TryParse call.


if (int.TryParse(Console.ReadLine(), out int write))
{
    array[0] = write; 
}


So this is more inline with what you want, but the docs would tell you this if you read thru how to use the method.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,258
  • Joined: 05-May 12

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 11:28 AM

Or if your willing to have exceptions:
array[0] = int.Parse(Console.ReadLine());



In general, though, TryParse() is the production quality code that you should be using. Parse() and Convert.ToInt32() should only be used in toy/prototype code, or where you have strict control over your inputs.
Was This Post Helpful? 1
  • +
  • -

#12 daygamer   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 110
  • Joined: 21-August 18

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 01:02 PM

View Postastonecipher, on 22 August 2018 - 10:28 AM, said:

If I am interpreting what you actually want, you want the number in the array, not the boolean value. Since TryParse returns a boolean, it is just there to let you know if it converted it successfully. You want the out value, not the return from the TryParse call.


if (int.TryParse(Console.ReadLine(), out int write))
{
    array[0] = write; 
}


So this is more inline with what you want, but the docs would tell you this if you read thru how to use the method.



of course..... I just have to rethink this one.

Thanks dude! Titally missed it before

View PostSkydiver, on 22 August 2018 - 11:28 AM, said:

Or if your willing to have exceptions:
array[0] = int.Parse(Console.ReadLine());



In general, though, TryParse() is the production quality code that you should be using. Parse() and Convert.ToInt32() should only be used in toy/prototype code, or where you have strict control over your inputs.


that is wrong bro
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6502
  • View blog
  • Posts: 22,258
  • Joined: 05-May 12

Re: array[0] = int.TryParse(Console.ReadLine(), out int write);

Posted 22 August 2018 - 01:20 PM

In what way is it wrong?

See MSDN: How to: Convert a String to a Number (C# Programming Guide)

Quote

You can convert a string to a number by using methods in the Convert class or by using the TryParse method found on the various numeric types (int, long, float, etc.).

If you have a string, it is slightly more efficient and straightforward to call a TryParse method (for example, int.TryParse("11", out number)). Using a Convert method is more useful for general objects that implement IConvertible.

You can use Parse or TryParse methods on the numeric type you expect the string contains, such as the System.Int32 type. The Convert.ToUInt32 method uses Parse internally. If the string is not in a valid format, Parse throws an exception whereas TryParse returns false.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1