How to Create a Function that accepts different number ofparameters based on a number

sureshcd10

Regular
Joined
Dec 25, 2003
Messages
77
I ve got two buttons on my WebForm with the following code behind them
Private sub Button1_Click(......)...
cmdsql = New SqlClient.SqlCommand("ProcedureName", con)
cmdsql.CommandType = CommandType.StoredProcedure
cmdsql.Parameters.Add("@Id1", Id1)
cmdsql.Parameters.Add("@Id2", Id2)
end sub

Private sub Button2_Click(......)...
cmdsql = New SqlClient.SqlCommand("ProcedureName", con)
cmdsql.CommandType = CommandType.StoredProcedure
cmdsql.Parameters.Add("@Id1", Id1)
cmdsql.Parameters.Add("@Id2", Id2)
cmdsql.Parameters.Add("@Id3", Id3)
cmdsql.Parameters.Add("@Id4", Id4)
end sub

Now I want to replace the code between the two ButtonClik events with a common function
Is it possible to write such a function ? How ?

ie Like the following
so that I can write like this

Private sub Button1_Click(......)...
MyFunction("ProcedureName",2,"@Id1","@Id2",Id1,Id2)
end sub

Private sub Button2_Click(......)...
MyFunction("ProcedureName",3,"@Id1","@Id2","@Id3",Id1,Id2,Id3)
end sub


Public Function MyFunction(Procdurename as String,n as Integer,y as String,z as String)
dim i as integer
for i=1 to n
......
next
end function

suppose if I am giving 2 as the value on n,then myFunction should allow me to pass 2 parameters(ie 2 values for y and 2 values for z)
and if I am giving 3 as the value on n,then myFunction should allow me to pass 3 parameters(ie 2 values for y and 2 values for z)

Any one having an idea to create such a function ....

Thanks in advance :confused: :confused: :confused:
 
Back
Top