Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to provide method which have parameters in switch case:
C#
switch(input){
case"y":contact(no,value);
break;
}

when i am doing so its give me error. plz help

[Update with code from comment]
C#
Console.Write("You have cross the limit of shares.Do you want to continue?(Y/N)");
string input = Console.ReadLine();

switch (input)
{
    case "y":Calculateshare(share:, totalBalance:);

        break;

    case "n":

        break;
}

near Calculateshare the error is Argument missing in parameters.

What I have tried:

Trying to provide parameterized method in switch case.
Posted
Updated 27-Apr-16 1:42am
v2
Comments
phil.o 27-Apr-16 5:59am    
Please show us your actual code. "its give me error" does not indicate anything useful whatsoever.
RJ_Zed 27-Apr-16 6:03am    
Provide us the exact code snippet and the error which you get. Do you need a generic method or just to resolve the error.
Karthik_Mahalingam 27-Apr-16 6:06am    
what error?
@j@y123 27-Apr-16 6:15am    
Console.Write("You have cross the limit of shares.Do you want to continue?(Y/N)");
string input = Console.ReadLine();

switch (input)
{
case "y":Calculateshare(share:, totalBalance:);

break;

case "n":

break;
}
near Calculateshare the error is Argument missing in parameters.
Patrice T 27-Apr-16 7:41am    
I updated your question with this code.
Use Improve question to update your question.

 
Share this answer
 
try:

case "y":
contact(no,value);
break;

most likely one, or both of your parameters 'no and 'value are undefined.

if the method "contact" is defined in some other class, then you will be able to use it only if you have a valid reference to it ... which can happen in several different ways.

if 'contact is defined as a static method in a static class, then put the static class name in the method call: someStaticClassName.contact(no, value);

the compile error should give you information about the problem here.
 
Share this answer
 
Comments
@j@y123 27-Apr-16 6:41am    
Thank you it works.
BillWoodruff 27-Apr-16 9:58am    
Remember that every Class, Struct, Method has a "virtual address:" AssemblyName/NameSpace/ClassName/????? ... most of the time you may not have to specify those because the current context is one where whatever object is accessible by a simple direct reference. But, when you use multiple NameSpaces, etc., you may need to specify the full "address." Of course, 'using directives' at the top of your .cs files may include the names of other name-spaces, or libraries (dll's) required.

It's like when you are in your "home town" you can say to someone: "I live on 19th. Street," you don't have to state the name of your province, district, city, etc. But, go to another country, and you may need to give a full address.

NameSpaces in .NET are another tool for organizing programs to achive encapsulation and separation of concerns. They allow you to use the same names for different purposes in different contexts ... or, of course, unique names.
verify your syntax

C#
static void Main(string[] args)
        { 
            Console.Write("You have cross the limit of shares.Do you want to continue?(Y/N)");
            string input = Console.ReadLine();

            switch (input)
            {
                case "y": Calculateshare(share: 5, totalBalance: 10);

                    break;

                case "n":

                    break;
            }

        }

        static void Calculateshare(int share, int totalBalance)
        {

        }



or

C#
  int share = 5;
            int totalBalance = 10;

            Console.Write("You have cross the limit of shares.Do you want to continue?(Y/N)");
            string input = Console.ReadLine();

            switch (input)
            {
                case "y": Calculateshare(share, totalBalance);

                    break;

                case "n":

                    break;
            }
 
Share this answer
 
v2
Comments
@j@y123 27-Apr-16 7:07am    
Thanx its really working.
Karthik_Mahalingam 27-Apr-16 7:19am    
If it works pls close this post
BillWoodruff 27-Apr-16 23:04pm    
+5
Karthik_Mahalingam 27-Apr-16 23:06pm    
Thank you Bill :-)
C#
Calculateshare(share:, totalBalance:);

What is this syntax ? Can it be the problem ?
 
Share this answer
 
You should make the called method as static to make use of it in switch case.

C#
static void Main()
       {
           char input = Convert.ToChar(Console.ReadLine());
           switch (input)
           {
               case 'y':
                   contact();
                   break;
               case 'x':
                   contact1();
                   break;

           }
           Console.ReadLine();
       }

       public static void contact()
       {
           Console.WriteLine("Hi am method 1");
       }
       public static void contact1()
       {
           Console.WriteLine("Hi am method 2");
       }


is working. Just check
 
Share this answer
 
Comments
@j@y123 27-Apr-16 6:31am    
The method which i define is in different class.
Here is basic examples

Click Here

And your code is also correct only need to remove colon ( : ) symbol from parameters.
 
Share this answer
 
v2
Comments
Philippe Mori 27-Apr-16 22:06pm    
Fix the emoticon if you want some vote greater than 1.
Shambhoo kumar 2-May-16 1:14am    
i was not inserted emoticon symbol just i used colon (:) symbol but it's automatically shown there ( emotion symbol.

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