0

I'm trying to write a console app, that does dicerolls with multiple types of dice, and after having done that, asks the user to confirm a new roll, or quit.

Here is minimal part that shows the problem:

string rolltype = "bob";
if (rolltype = "d4") // <-- error on this line
{
    Console.WriteLine("d4 roll is {0}", d4);
}

Code produces following error at compile time:

cannot implicitly convert type string to bool

Complete source:

namespace diceroll
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            int d4 = rand.Next(1, 5);
            int d6 = rand.Next(1, 7);
            int d8 = rand.Next(1, 9);
            int d10 = rand.Next(1, 11);
            int d12 = rand.Next(1, 13);
            int d20 = rand.Next(1, 21);
            string rolltype;
            Console.WriteLine("what do you want to roll?");
            rolltype = (Console.ReadLine());
            Console.WriteLine("your choice is {0}", rolltype);
            if (rolltype = "d4")
            {
                Console.WriteLine("d4 roll is {0}", d4);
            }
            else { }
            Console.ReadKey();
        }
    }
}

What I wish to achieve here, is the console asking for the type of roll, and upon being given that, it returns you a random number. The (rolltype = "d4" ) returns an error "cannot implicitly convert type string to bool".

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
HankStar
  • 11
  • 4
  • 1
    Please note that your post does not show any research (i.e. use some search engine to look for error string - Google, Bing, Yahoo - https://www.bing.com/search?q=c%23%20if%20cannot%20implicitly%20convert%20type%20string%20to%20bool) as well does not show [MCVE] (there is large amount of code unrelated to the question - should be just two lines...) – Alexei Levenkov Apr 02 '17 at 23:39
  • Also please avoid asking multiple questions in single post (I've edited it out along with major rewrite)... But https://www.bing.com/search?q=C%23+refactor+if+multiple may give you answer on how to change multiple ifs. – Alexei Levenkov Apr 02 '17 at 23:45
  • I'm sorry about that, but since i'm new to this, i wasn't sure how to ask this question properly, or search for a solution, because all my tries came back as unrelated to my problem. Will try to do it better in the future. – HankStar Apr 02 '17 at 23:50

2 Answers2

2

"cannot implicitly convert type string to bool", and i have no idea how to fix it

this is because the code below doesn't result to a boolean value, below you're trying to assign rolltype to the string "d4" hence the error.

if (rolltype = "d4")

what you want is this:

if (rolltype == "d4")

Also is there a more elegant way of going at this than writing 6 separate if statements for the rolltypes?

Sure, we can use an array to store the possible options and then just loop over it.

Step 1 - create the array:

string[] myArray = {"d4","d6","d8","d10","d12","d20"};

now your code becomes like this:

Random rand = new Random();
int d4 = rand.Next(1, 5);
int d6 = rand.Next(1, 7);
int d8 = rand.Next(1, 9);
int d10 = rand.Next(1, 11);
int d12 = rand.Next(1, 13);
int d20 = rand.Next(1, 21);
string[] myArray = {"d4","d6","d8","d10","d12","d20"};

Step 2 - loop over it to find if the entered value is equal to any of those within the array.

foreach(string str in myArray){
   if (rolltype == str){
      // do something
      break; // if we get here then we don't need to loop any further
   }
}

now your code becomes like this:

Random rand = new Random();
int d4 = rand.Next(1, 5);
int d6 = rand.Next(1, 7);
int d8 = rand.Next(1, 9);
int d10 = rand.Next(1, 11);
int d12 = rand.Next(1, 13);
int d20 = rand.Next(1, 21);
string[] myArray = {"d4","d6","d8","d10","d12","d20"};
string rolltype;
Console.WriteLine("what do you want to roll?");
rolltype = (Console.ReadLine());
Console.WriteLine("your choice is {0}", rolltype);

foreach(string str in myArray){
    if (rolltype == str){
          // do something
        break; // if we get here then we don't need to loop any further
    }
}

Another good solution suggested by Chris within the comments is to simply take the number out of the string. This will obviously reduce the amount of rand.Next() you currently have.

Example:

int dieType = int.Parse(rollType.Substring(1)); 
int result = rand.Next(1,dieType+1);
Community
  • 1
  • 1
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • In C# there's no (practical) difference (for strings) using `==` or `Equals` as long as they are typed as string. If they are typed as object or T then you definitely want to use the Equals method. – Jeremy Thompson Apr 02 '17 at 23:44
  • @JeremyThompson alright I see, it's just that in java when using "==" with strings you're checking whether they refer to same location in memory rather than "are the contents equal". but thanks anyway. good to learn something new. – Ousmane D. Apr 02 '17 at 23:46
  • "don't use == to compare strings, use Equals() method." is at best pointless suggestion for C#. I personally find it flat out wrong and confusing - so voting to express the fact I find this answer *not useful* (C# *is not* Java or C++ or JavaScript - recommend appropriately). – Alexei Levenkov Apr 02 '17 at 23:47
  • 1
    @AlexeiLevenkov thanks for the suggestion , will update. – Ousmane D. Apr 02 '17 at 23:47
  • @Ousmane Mahy Diaw Thank you, I haven't encountered arrays yet, so i'm learning a lot from this. I was hoping the 6 if statements wouldn't be necessary. – HankStar Apr 02 '17 at 23:55
  • @HankStar you're welcome, arrays are really easy so you should be able to have a grasp of it soon, also good luck. :) – Ousmane D. Apr 02 '17 at 23:56
  • 1
    Better than using arrays would be to just take the number out of the string. `int dieType = int.Parse(rollType.Substring(1)); int result = rand.Next(1,dieType+1);` – Chris Apr 02 '17 at 23:59
  • @Chris sure that definitely would be a good solution. – Ousmane D. Apr 03 '17 at 00:01
  • It has the benefit that if you want to support new dice then you don't need to change the code. Want to roll a d100? It works already? Want to roll a d143? It works already! Though no idea why you would want to roll a d143. :) – Chris Apr 03 '17 at 00:03
  • @Chris definitely :), and considering what you've just stated my solution wouldn't be good for that case as you'd have to type d4,d6,d8...... haha but yeah I get your point. I would have probably included your suggestion within the post but since this post will most likely be deleted soon I don't see the point. Thanks for the suggestion once again! :) – Ousmane D. Apr 03 '17 at 00:07
  • @OusmaneMahyDiaw: I doubt it will be deleted. Its a reasonable question with a reasonable answer. Our discussion isn't really the point of the question which would be the main reason not to bother editing it into the question. You are welcome to do so if you want to though. – Chris Apr 03 '17 at 00:11
0
 if (rolltype == "d4")

You need to use the equality operator not the assignment one.

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321