Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am a beginner in C# & .NET.
In the following code CreateCountry function declared as int and its return type is also int (this part I got it)

But I am confused about the overloaded string variable in int function, also nowhere in this piece of code the given string is converted to int value.

Please guide me. Thank you (Reference:CRUD project)
C#
public int CreateCountry(string countryName)
{
   if (string.IsNullOrEmpty(countryName))
   {
       return -1;
   }
   else
   {
        DALCountry dalCountry = new DALCountry();
        return dalCountry.CreateCountry(countryName);
   }            
}
Posted
Updated 20-Nov-15 3:37am
v2

It would be better design to check the country argument and throw an exception if it's not right. The calling function would have a try/catch block to handle the error.

C#
public DALCountry CreateCountry(string countryName)
{
  if (String.IsNullOrEmpty(countryName))
    throw new ArgumentException("countryName");

  DALCountry dalCountry = new DALCountry();
  return dalCountry.CreateCountry(countryName);
}
 
Share this answer
 
v2
Your question doesn't make a lot of sense.

Are you asking how to overload a method with a copy that takes the same parameter type, a string in your case, and differs only by the return type? Are you asking how a method can return either an integer OR a DALCountry type?

It's easy. You can't. You must plan your code better. A method can only ever return a single type. It cannot return multiple types. Solution #1 is the best solution as a DALCountry class can hold much more meaningful information than a single integer can ever hold.
 
Share this answer
 
Comments
Member 11633058 20-Nov-15 16:29pm    
Yes, I want to ask that, how can you overload a method with a copy that takes the same parameter type, (a string in this case), and differs by the return type(int in this case)?
Dave Kreskowiak 20-Nov-15 18:16pm    
Go back and reread my post. YOU CAN'T DO THAT! It's impossible for the runtime to figure out which overload to call because the parameter lists are identical. The runtime also cannot call every overload to see which one should be the valid return value. What you want cannot be done.
Member 11633058 21-Nov-15 6:57am    
Thank you Dave! thanks for the guidance
'CreateCountry is not "declared as int." It returns an 'int, and it takes a 'string parameter.

The 'CreateCountry method in the 'DALCountry class must be different from the 'CreateCountry method you show here, or: the method would call itself recursively until there was a stack overflow error.

There is no "overloaded string variable" here.
 
Share this answer
 
C#
Hello.
you can not see the conversion from String to integer, because it is a class method : DALCountry .

DAL , stands for: Data Access Layer .

The conversion of " countryname " to integer data type , can be given in different ways, but the most is roughly a query to the database : "SELECT ID FROM WHERE CountryName Country = 'USA'"

With the above query, an identifier is obtained for countryName
And that outcome , which will return the function CreateCountry

And I hope it helped you on your question 

Best regards From Mexico.
 
Share this answer
 

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