简体   繁体   中英

How do I pass an int to an IEnumerable?

My code is giving me the following error: Compilation error (line 23, col 5): The best overloaded method match for 'Program.PrintValues(System.Collections.IEnumerable)' has some invalid arguments Compilation error (line 23, col 17): Argument 1: cannot convert from 'int' to 'System.Collections.IEnumerable'"

using System;
using System.Collections;

public class Program
{
    public static void Main()
    {
        Stack opStack = new Stack();
        Stack valStack = new Stack();
        Stack postStack = new Stack();
        string infix = "A*B+C/D";
        Console.WriteLine(infix);
        char[] infixray = infix.ToCharArray();
        for (int i = 0; i < infixray.Length; i++)
        {
            //Console.WriteLine(infixray[i]);
            if (!infixray[i].Equals("*") || !infixray[i].Equals("+") || !infixray[i].Equals("/") || !infixray[i].Equals("-"))
            {
                postStack.Push(infixray[i]);
            }
            else if (infixray[i].Equals("*") || infixray[i].Equals("+") || infixray[i].Equals("/") || infixray[i].Equals("-"))
            {
                **PrintValues(i);**
                opStack.Push(infixray[i]);
                //int multpres=1, divpres=2, addpres=3, subpres=4;
                postStack.Push(infixray[i]);
                opStack.Push(infixray[i]);
                valStack.Pop();
            //valStack.Pop();
            }

            PrintValues(postStack);
        }
    }

    public static void PrintValues(IEnumerable myCollection)
    {
        foreach (Object obj in myCollection)
            Console.Write("    {0}", obj);
        Console.WriteLine();
    }
}

*"How do I pass an int to an IEnumerable?"

You can create a single-item List (which implements IEnumerable ) that contains the int and pass that to the method:

PrintValues(new List<int> { i });

Or, let's read the message the compiler is sending us:

"The best overloaded method match for 'Program.PrintValues(System.Collections.IEnumerable)' has some invalid arguments"

Since the compiler is complaining about there being no suitable overload, we can provide one for it by writing an overload of the method that takes an int argument:

public static void PrintValues(int item)
{
    Console.WriteLine("    {0}", item);
}

(of course now the plural method name doesn't make much sense, so perhaps a new method name is also in order, like PrintValue )

Seems you just want to output the index of the current branch ( if )

Just call the Console.Write or Console.WriteLine directly

...
else if (infixray[i].Equals("*") || infixray[i].Equals("+") || infixray[i].Equals("/") || infixray[i].Equals("-"))
{
     Console.WriteLine("    {i}");
     opStack.Push(infixray[i]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM