Home > Net >  what does casting object type from 'System.String[]' to type 'System.IConvertible
what does casting object type from 'System.String[]' to type 'System.IConvertible

Time:06-16

I am quite new to C# and I was trying to write a code to practice about a calculator. It would ask if you would want to divide, multiply, subtract, or add. The calculator would ask how many numbers will be in the equation and, solve it. I know that the code could be a lot neater, but since this is just practice, I did not make it very neat or efficient. Here is the code and error message

Unable to cast object of type 'System.String[]' to type 'System.IConvertible'" Line 20 ("int number4 = Convert.ToInt32(number3);

using System;
                    
public class Program
{
    public static void Main()
    {
        Console.WriteLine("Welcome to the calculator! Enter 1 if you want to multiply. 2 if you want to divide. 3 if you want to add and 4 if you want to subtract");
        string input = Console.ReadLine();
        int number;
        number = Convert.ToInt32(input);
        int newnumber = 0;
        if (number == 1) {
            Console.WriteLine("How many numbers will there be in this multiplication equation?");
            string multiplication = Console.ReadLine();
            int number2;
            number2 = Convert.ToInt32(multiplication);
            string[] number3 = new string[number2];
            int number4 = Convert.ToInt32(number3);
            do {
                int newnumber2 = newnumber;
                newnumber2  ;
                newnumber = newnumber2;
                Console.WriteLine("Please enter in your"   newnumber2   "st number");
                int arrayNum = number4;
                int[] equationNumbers = new int[number4];
                string input2;
                input2 = Console.ReadLine();
                int input3;
                input3 = Convert.ToInt32(input2);
                equationNumbers[arrayNum] = input3;
                arrayNum--;
            } while (newnumber >= number4);
        } //if statement bracket
    }
}

Also I think the bottom part of the code isn't correct because I was slightly confused on how to take in the input number and put it into an array

CodePudding user response:

You are trying to convert a string[] to an int. There's no way to do that, so the compiler doesn't know how to handle that either.

IConvertible is an interface implemented on various data types. If you take a look at the methods provided by this IConvertible interface, you'll see that there's no method for converting a string[] to an int. In other words: the string[] is not convertible.

You might wanna read more about casting and type conversions.

CodePudding user response:

I believe this is because you cant convert an array of strings to an int. However you can convert a string to an int. Your code is full of bugs and wont run event if you were able fix this problem you have around 5 more down the line. I would recommend that you take a debugger and step though your code and look at what the values become. If you are using visual studio this video is really great (just ignore the part about unity)

  •  Tags:  
  • c#
  • Related