![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 225
Rep Power: 4
![]() |
A standards question, optional inputs into Methods
I'm creating a few methods that have a number of optional inputs
Quick example, say you have a method that prints Hello <name> where <name> is optional input parameter (I know, it's a ridiculously simple example, but bare with me) so you have two options I can do
public void PrintHi()
{
code
}
public void PrintHi(string name)
{
code
}which would work fine, but when you have say 10 optional variables and you can have any combinations of them it's quite a lot of different methods (plus you start running into issues if all your variables are of the same type). However the other way, just having one method with the variables all in, but allowing null/0/? inputs for the optional portions seems somewhat clumsy, anyone have other ideas? or other ways this "should" be done? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
You could always assign the variables a certain value (for instance an array of variable statuses)... each position in the array representing each variable with a slightly increasing numeric.
When you get into the function, you can see if the variables have values by checking their length, etc. Add up the elements of the status array to determine which values were set. Based on the value, you will know which variables were set and which execution path to take. This is kinda like the chmod command in linux... but chances are, you need just basic function overloading, may need to be more specific.
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Programmer
|
Arla:
I think a more specific example of what you are trying to code is needed for me to really understand what you are trying to do. I see no problem with having a bunch of methods that are named the same but have different input params. From what I gather from you though, perhaps you can use something like this: public void printGreeting( string Name, string Address, int Age, bool Married, int CustomerLevel )
{
switch( CustomerLevel )
{
case 1:
//do something
break;
case 2:
//do something
break;
case 3:
//do something
break;
case -1:
//perhaps this can be the case where this parameter is empty or
//should not have a value
break;
}
switch ( Name )
{
case "Jane Doe":
//do something
break;
case "John Smith":
//do something
break;
case "Englebert Humperdinck":
//with such a name he should always have his own case
//do something
break;
case "":
//again, perhaps this can be the case where this parameter is
//empty or should not have a value
break;
}
if ( Married )
{
//do something
}
else
{
//do something else, or not
}
}...again, I am not sure exactly if that will help, or particularily what you are trying to accomplish. If you can post some of the code you already have , that would be great!
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
} |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Mar 2005
Posts: 225
Rep Power: 4
![]() |
An example,
Well okay taking your example and using it (since I don't really have code I can post right now) A fairly simple database component that takes in a number of variables and stores them (name, address, phone number, etc etc), now I can have one method that takes in everything
public void StorePerson( string FirstName, string LastName, string Address1, string Address2, string City, string State, string Zip, string Country, int Age, bool Married, string areacode, string phone7 )
{
do stuff
}but then the requirement is that you fill each of those variables, and say someone doesn't have an address2, then you have to use null instead, which for numerics gets somewhat harder since you can't use null, so you have to use 0 and if the field means something for 0, you need to define the 'null' value. Alternative is to have many varients, a basic one that takes in the mandatory fields, and then others that each take in progressively more of the optional variables, the issue then becomes say address2 and city are variable, you could end up with
public void StorePerson( string FirstName, string LastName, string Address1, string Address2, string State, string Zip, string Country, int Age, bool Married, string areacode, string phone7 )
{
do stuff
}
public void StorePerson( string FirstName, string LastName, string Address1, string City, string State, string Zip, string Country, int Age, bool Married, string areacode, string phone7 )
{
do stuff
}Which on a number of variables and types selection are in fact, the same method. |
|
|
|
|
|
#5 |
|
Programmer
|
Arla:
I think there are a few ways do accomplish what you are trying to do. Personally I would not go with a different C# method/procedure for each different case. I would however consider some other points:
..as well, if you are getting the Person information from a form, you can take that information and insert it right into your SQL statement( provided you have checked that information for validity & security ). Please correct me if I am not getting what you need to do. I do like these kinds of problems though. They are the meat of programming - how can I make this or that happen in the most efficient and interesting way? Again, it would be nice, if its possible for you to post some code, so we get a better idea of what you are trying to accomplish. Right now, I am actually working on an ASP.NET application in C# that will help me organise inofmation, take notes, track grades, print reports, etc... for me in my collge program at NAIT here in Edmonton, Alberta, Canada. Its a lot of fun!
__________________
public class MySignature extends Post implements JavaSyntax throws NuttinHoney{
MySignature()
{
Salutation();
Name();
}
public string Salutation()
{
Screen.printLn( "Sincerely,\n" );
}
public string Name()
{
Screen.printLn( "Kevin Parkinson" );
}
}Last edited by new User(Kevin.Parkinson); Apr 27th, 2005 at 10:41 PM. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|