Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public int abc( )

how to pass parameter

What I have tried:

public int abc( int )
      {
          try
          {
           using (MySqlCommand cmd = new MySqlCommand("select del_nok_data_days from communication", con))
            {
             cmd.CommandType = CommandType.Text;
              using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
              {
                using (DataTable dt = new DataTable())
                {
                 da.Fill(dt);

                con.Open();
                 cmd.ExecuteNonQuery();
                 con.Close();
                 //return 12  ;
                 return int.Parse(dt.Rows[0][0].ToString());
                   }
                  }
              }
          }
          catch (Exception ex)
          {
           //   return 0;
              LogFileWrite("getdata,frmAuto : " + ex.Message);
          }
      }
Posted
Updated 17-Mar-21 20:58pm

public int abc( int )

will never build/compile: you do not give the parameter a name.

you need to study the basic "facts" of what a method is in c#, and the syntax of its return Type and Parameters: [^]
 
Share this answer
 
Comments
Maciej Los 18-Mar-21 3:18am    
5ed!
To add to what Bill says, even if you declaration was correct:
C#
public int abc(int def)
   {
   ...
   }
You need to return a value.
There are two types of method: void methods which return no value at all, and methods with a return type.
The former are simple to deal with: you can optionally use a return statement at any point in the method body at which point execution of the method will end and control will pass back to the instruction after the method was called. If you don't, then this will happen when execution of the method reaches the close curly bracket which ends the body definition.

But when you specify a return type then you must provide a return value for every possible path through the method: if there is a single path the compiler can see (even if the logic means it can't happen) your code will not compile.
Normally, that means putting a return statement at the end of the method immediately above the closing curly bracket for the method body.

That return statement must have a value which is compatible with the return type you specified when you declared the method.
So this will work:
C#
public int abc(int def)
   {
   ...
   return 666;
   }
But this won't:
C#
public int abc(int def)
   {
   ...
   return "Hello world!";
   }
Because an integer and a string are not compatible.
C#
public int abc(int def)
   {
   if (def > 666)
      {
      return 666;
      }
   }
Will not work either, because there is a path where a return is not encountered.
 
Share this answer
 
Comments
Maciej Los 18-Mar-21 3:16am    
return 666; i like it!
As to me, the name of method should be replaced with DevilNumber
:laugh:

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