Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating a method that can take multiple integer values as arguments . I am trying to do this by using params. An error shows up on line 5 ,MinVal declaration, when I am trying to execute this program

using System;
namespace LearnigToProgram
{
	class Min {
		public void MinVal(params []int numbers)
		{
			for (int i = 0; i < numbers.Length; i++)
			{
				Console.WriteLine(numbers[i]);
				Console.WriteLine();
			}
		}
		
		}
		class ParamsDemo {
			static void Main() {
			Min obj1 = new Min();
			obj1.MinVal(1, 2, 3, 4, 5);

			
			}
		}
	
	}
Posted
Updated 13-Sep-16 14:59pm
v3
Comments
[no name] 13-Sep-16 16:02pm    
Your code would have to compile first before you could try and execute it
public void MinVal(params int[] numbers)

try this, check in line comments.
C#
using System;
using System.Windows;  // Add this namespace
namespace LearnigToProgram
{
	class Min {
		public void MinVal(params int[] numbers) // only one variable name is a valid syntax
		{
			for (int i = 0; i < numbers.Length; i++)
			{
				Console.WriteLine(numbers[i]);
				Console.WriteLine();
			}
            Console.ReadLine(); //waits for input,  to view the output being displayed on the console window, else the window will be closed immeditely
		}
		
		}

		class ParamsDemo {
			static void Main() {
			Min obj1 = new Min();
			obj1.MinVal(1, 2, 3, 4, 5); 
			}
		}
	
	}
 
Share this answer
 
The only issue i see with your code is you have the array declaration wrong in your MinVal method.

The following code, only thing corrected being the array, compiles and runs as expected.

C#
static void Main(string[] args)
        {

            Min obj1 = new Min();
            obj1.MinVal(1, 2, 3, 4, 5);

            Console.ReadLine();
        }


        class Min
        {
            public void MinVal(params int[] numbers)
            {
                for (int i = 0; i < numbers.Length; i++)
                {
                    Console.WriteLine(numbers[i]);
                    Console.WriteLine();
                }
            }

        }
 
Share this answer
 
Comments
Member 12731696 13-Sep-16 21:47pm    
Thanks!!! Can't believe that I was struggling on a silly mistake of array declaration !!!! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900