Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 11:14 AM   #1
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
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?
Arla is offline   Reply With Quote
Old Apr 26th, 2005, 1:20 PM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
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."
Infinite Recursion is offline   Reply With Quote
Old Apr 26th, 2005, 10:10 PM   #3
new User(Kevin.Parkinson)
Programmer
 
new User(Kevin.Parkinson)'s Avatar
 
Join Date: Mar 2005
Location: Edmonton, Alberta, Canada
Posts: 37
Rep Power: 0 new User(Kevin.Parkinson) is on a distinguished road
Send a message via MSN to new User(Kevin.Parkinson)
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" );
     }
}
new User(Kevin.Parkinson) is offline   Reply With Quote
Old Apr 27th, 2005, 6:20 PM   #4
Arla
Hobbyist Programmer
 
Arla's Avatar
 
Join Date: Mar 2005
Posts: 227
Rep Power: 4 Arla is on a distinguished road
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.
Arla is offline   Reply With Quote
Old Apr 27th, 2005, 10:38 PM   #5
new User(Kevin.Parkinson)
Programmer
 
new User(Kevin.Parkinson)'s Avatar
 
Join Date: Mar 2005
Location: Edmonton, Alberta, Canada
Posts: 37
Rep Power: 0 new User(Kevin.Parkinson) is on a distinguished road
Send a message via MSN to new User(Kevin.Parkinson)
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:
  • create a person class with all those fields, so that your method will look like this: public void StorePerson( Person p ); instead of multiple methods with many params.
  • have optional( NULL ) fields within your database.
  • have one generic stored procedure that can take care of everything
  • perhaps use text/non-stored procedure based SQL statements( assuming you apply the notion of optional fields in the database ) and then you can insert fields as you wish.


..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.
new User(Kevin.Parkinson) is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:54 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC