Optional Parameters?

  • Thread starter Thread starter dkode
  • Start date Start date
D

dkode

Guest
I am creating a UML layout of one of my DALC, and i have a quick question

This DALC can accept many different parameters:

No parameters : all db rows
sbuid : specific sbuid
revid : specific revid
revtypeid : specific revtypeid
month : specific month
year : specific year
goalamount : greater than / less than

I originally started making a seperate method for each parameter but then i noticed that all of these paramters can be used in conjunction. IE: they want the revenue goals that are a specific SbuID and specific RevType for a specific year.

If you know what i mean, there can be alot of different combonations

Is there a way to fit all of this into one method? if so how can i do so?
 
Ok,

I think I might have figured out a way,

someone please let me know if I am going about this wrong. Thank you

Code:
		Public Shared Function GetRevenueGoals (Optional ByVal RevenueID As Integer = 0, _
								Optional ByVal SbuID As Integer = 0, _
								Optional ByVal RevenueTypeID As Integer = 0, _
								Optional ByVal Month As Integer = 0, _
								Optional ByVal Year As Integer = 0) As RevenueGoalCollection
			
		End Function
 
Optional parameters are generally looked down on in .Net and they can have nasty side effects if used in public functions within a DLL.

Function overloading is generally the recomended way.
 
Overloading would require you to overload all possible permutations.

How about declaring a class DALCParameters with a public property for each of your cases as described above.

You can then pass this class as the single parameter.
If, in the future, you'd also want to search for a specific day, you just change the DALCParameters class. All interfaces remain the same.

For your convenience, you can also add several constructors.
 
Last edited:
Could i create the parameters class as a structure?

i'm still trying to figure out the uses of a structure and an interface. I understand the basic concept of both, but I do not know when to use them/how they are useful?
 
Never used structures so far.

When I mentioned "Interfaces" I wasn't talking of "use defined interfaces" (IMySpecialInterface), perhaps I should have written
"signature" instead.

So :)

...

You can then pass this class as the single parameter.
If, in the future, you'd also want to search for a specific day, you just change the DALCParameters class. All *signature* remain the same.
 
Creating a struct RevenueGoals and then passing it into GetRevenueGoals would probably be much better for what you're doing.

C#:
public struct RevenueGoals {
   public int RevenueID = 0;
   public int SbuID = 0;
   // ...
}

public void GetRevenueGoals(RevenueGoals revenues) {
   if (revenues.RevenueID != 0) {
      // whatever..
   }
   // etc...
}
 
Visual Basic:
Public Structure RevenueGoals
   Public RevenueID as Integer = 0
   Public SbuID as Integer = 0
   ' ...
End Structure

Public Sub GetRevenueGoals(ByVal revenues as RevenueGoals)
   'Do Stuff
End Sub
You can change the structure to a class
Visual Basic:
Public Class MyParameters
     Protected _RevenueID as Integer
     Proteted _SbuID as Integer

     Public Property RevenueID() as Integer
         Get
              Return _RevenueID
         End Get
         Set(ByVal Value as Integer)
              _RevenueID = Value
         End Set
     End Property

     Public Property SbuID() as Integer
         Get
             Return _SbuID
         End Get
         Set(ByVal Value as Integer)
             _SbuID = Value
         End Set
     End Property
End Class

Public Class DoStuff
    Public Sub GetRevenueGoals(ByVal revenues as MyParameters)
       If revenues Is Nothing Then Exit Sub

       'Do Stuff
    End Sub
End Class
Or the overloaded functions means multiple declarations
Visual Basic:
Public Shared Function GetRevenueGoals (ByVal RevenueID As Integer) As RevenueGoalCollection
Public Shared Function GetRevenueGoals (ByVal RevenueID As Integer, ByVal SbuID As Integer) As RevenueGoalCollection
Public Shared Function GetRevenueGoals (ByVal RevenueID As Integer, ByVal Year As Short) As RevenueGoalCollection
'etc
 
Back
Top