Home > Back-end >  How to pass in a int array as a value for a void method parameter?
How to pass in a int array as a value for a void method parameter?

Time:10-11

I am trying to make a small console application for an ATM machine, and I am struggling with the Insert method of this Program, so it goes like this:

public void Insert(int[] cash) //I cannot change the type or parameter of the method
{

    for (int i = 0; i < cash.Length; i  ) // using .Length input into the array
    {
        cash[i] = Convert.ToInt32(Console.In.ReadLine());
    }
    bool IsntAcceptable = cash.Intersect(AcceptableBanknotes).Any(); // Acepttable Banknotes variable is a array like this  private int []AcceptableBanknotes = { 5, 10, 20, 50, 100 };
    
    if (IsntAcceptable)
    {
        throw new ArgumentException("Only acceptable banknotes are 5, 10, 20, 50 and 100");
    }
    this.Balance  = cash.Sum(); // add to balance

    Console.Write(this.Balance);
}

In my main it looks like this

    static void Main(string[] args)
    {
// some other code here
        Console.WriteLine("\nEnter the amount to deposit: ");
        atm.Balance = atm.Insert(Array.ConvertAll(Console.ReadLine().Trim().Split(' '), Convert.ToInt32));
    }

but it just gives me an error of Error CS0029 Cannot implicitly convert type 'void' to 'int'

I would be very grateful for any help or guidance you could give me, thanks in advance!

CodePudding user response:

You are not returning anything from Insert that is the cause an issue. Just change it to normal call and it will work for you like below:

 atm.Insert(Array.ConvertAll(Console.ReadLine().Trim().Split(' '), Convert.ToInt32));

or if you want to return something from that method then make that method with specific type as per your requirements.

CodePudding user response:

This is an error caused by putting an void type in the member variable atm.balance.

public int Insert(int[] cash) //I cannot change the type or parameter of the method
{

    for (int i = 0; i < cash.Length; i  ) // using .Length input into the array
    {
        cash[i] = Convert.ToInt32(Console.In.ReadLine());
    }
    bool IsntAcceptable = cash.Intersect(AcceptableBanknotes).Any(); // Acepttable Banknotes variable is a array like this  private int []AcceptableBanknotes = { 5, 10, 20, 50, 100 };
    
    if (IsntAcceptable)
    {
        throw new ArgumentException("Only acceptable banknotes are 5, 10, 20, 50 and 100");
    }
    this.Balance  = cash.Sum(); // add to balance
    //If you return Balance, you don't need this sentence.

    Console.Write(this.Balance);
    return Balance;
}

OR

  static void Main(string[] args)
    {
// some other code here
        Console.WriteLine("\nEnter the amount to deposit: ");
        atm.Insert(Array.ConvertAll(Console.ReadLine().Trim().Split(' '), Convert.ToInt32));**
    }

CodePudding user response:

Looks like you're trying to make Insert an extension method to atm. If that's the case your Insert method needs to be static and take atm as a parameter. If you're going down the route of extension methods I'd create a separate class for your extensions like this:

public static class AtmExtensions
{
    public static void Insert(this Atm atm, int[] cash)
    {
        // your logic.
    }
}

Where Atm is a separate class composing of the various properties like Balance.

Next thing is that your passing in the values read from the console only to ignore that later and read from the console again. Read the values from the console and then use that where needed. So in this case you can remove the for loop as you've already converted the values to ints.

Finally, you're expecting Insert to set atm.Balance in the Main method, but it's void so doesn't return anything, and is setting it anyway as part of the Insert method. So, I think what you're trying to achieve should look something like this:

Your Atm class:

public class Atm
{
    public int Id { get; set; }
    public decimal Balance { get; set; }
    // other properties...
}

A new class for holding all your extension methods for Atm:

public static class AtmExtensions
{
    public static void Insert(this Atm atm, int[] cash)
    {
        var acceptableBanknotes = new int[] { 5, 10, 20, 50, 100 };

        if (cash.All(note => acceptableBanknotes.Contains(note)))
        {
            atm.Balance  = cash.Sum();
            Console.Write(atm.Balance);
        }
        else
        {
            throw new ArgumentException("Only acceptable banknotes are 5, 10, 20, 50 and 100");
        }
    }
}

Called like this:

static void Main(string[] args)
{
    var atm = new Atm();
    Console.WriteLine("\nEnter the amount to deposit: ");
    atm.Insert(Array.ConvertAll(Console.ReadLine().Trim().Split(' '), Convert.ToInt32));
}
  •  Tags:  
  • c#
  • Related