Ramraj
Ramraj

Reputation: 1

Access same method with different parameter

I have a problem with passing number of parameters to a method:

Fruits(string Name1, string Name2, String Name3)

This method is working fine:

Fruits("Apple", "Orange","Pineapple");

I got this error

Fruits("Apple", "Orange");

"No overload for method 'Fruits' takes 2 arguments."

Upvotes: 0

Views: 220

Answers (3)

Jibbow
Jibbow

Reputation: 757

To add a variable number of parameters:

 Fruits(params string[] fruits)
    {
          string firstparameter = fruits[0];
    }

And you can call this method with any number of parameters:

Fruits("Banana");
Fruits("Apple","Orange");
Fruits("Pineapple", "Whatever", "Idontknow");

Upvotes: 0

zulqarnain
zulqarnain

Reputation: 1735

The answer is as @fubo recommended. However I think you should read a proper C# tutorial and understand the basics. If you calling a method you must call it with number of parameters it requires and the correct types as well. You can either pas empty string to the parameter you want to ignore or anything else. But know well if you doing any further processing dependent on that parameter it will be affected.

Upvotes: 0

fubo
fubo

Reputation: 45947

as the error says you have to add another constructor with 2 parameters

Fruits(string Name1, string Name2)

or you have to pass another value when you create your Fruits-object

Fruits("Apple", "Orange", "whatever")

Upvotes: 2

Related Questions