[RESOLVED] [2008] Declare with an arbitrary number of parameters
Hi!!
Is it possible to have a class, sub or function declared with an arbitrary number of parameters? And those parameters be a type, like generics? For example
vb.net Code:
Public Class Test (Of T1, (Of T2))'The (Of T2) means that after the first parameter can be an arbitrary number of parameters
Public Sub New (Of T1, (Of T2))
...
End Sub
...
End Function
Dim a As Test (Of String, Of Integer, Of Short, Of Long)
Re: [2008] Declare with an arbitrary number of parameters
You can have a function with a variable number of arguments passed to it by using 'paramarray'. Here's the MSDN page on ParamArray:
http://msdn.microsoft.com/en-us/libr...9h(VS.71).aspx
Is that what you were looking for?
Re: [2008] Declare with an arbitrary number of parameters
Its not exactly that, but is very close. With that, all the elements in the array are of one type. For example:
vb Code:
Public Function CalcSum(ByVal Test As String, ByVal ParamArray Args() As Double) As Double
Every element in the args array has to be of the type double. My objective is to have the possibility that they are all of different types.
But is a very good thing to start work, cause I can make the array of type object and work from there. Thanks a lot!!
Re: [2008] Declare with an arbitrary number of parameters
Hmm, an arbitrary number of parameters with arbitrary types? I don't believe you can do that, because each argument or set of arguments usually represents 'something' relevant to what the function is. I suppose if you want it to be arbitrary, you could get close to it by accepting a paramarray of Object. That'll cause all the values to be boxed into the object.
Re: [2008] Declare with an arbitrary number of parameters
Well I gave a bad example. On a function it is ridiculous to do this. And I dont even want to do it on a function, I want to do it on a class. But i think I've encountered something impossible to do, its like trying to do the class List on vb 6.
Re: [2008] Declare with an arbitrary number of parameters
Just to see is this is closer to what you want:
VB Code:
Public Class Test(Of T1, T2, T3, T4)
Private m_op1 As T1
Private m_op2 As T2
Private m_op3 As T3
Private m_op4 As T4
Public Sub new (op1 as T1, op2 as T2, op3 as T3, op4 as T4)
m_op1 = op1
m_op2 = op2
m_op3 = op3
m_op4 = op4
End Sub
Public Property Property1 As T1
Get
Return m_op1
End Get
Set(ByVal value As T1)
m_op1 = value
End Set
End Property
Public Property Property2 As T2
Get
Return m_op2
End Get
Set(ByVal value As T2)
m_op2 = value
End Set
End Property
Public Property Property3 As T3
Get
Return m_op3
End Get
Set(ByVal value As T3)
m_op3 = value
End Set
End Property
Public Property Property4 As T4
Get
Return m_op4
End Get
Set(ByVal value As T4)
m_op4 = value
End Set
End Property
End Class
Re: [2008] Declare with an arbitrary number of parameters
It is that, but why infinite types. Like
Public Class Test(Of T1, ..., Tn)
Dim Hi As New Test(Of String, Integer, Boolean, Integer, Integer, String, String, Char, Long)
Re: [2008] Declare with an arbitrary number of parameters
Yep, you can't.
So, why would you want to do this though?
Re: [2008] Declare with an arbitrary number of parameters
I'm a fan of generics, especially the List class.
A List is (Generics are) like a table with two columns the indices's and the data. Or two columns of data, if you use a dictionary, but never more than two columns.
My idea was to create a class like a table, where you can have infinite data columns and always a indices's column. Like a multidimensional dynamic array.
If this existed I would use it A LOT of times. Of course I can always make a class that implements the table class for specific cases. But I would start to do very similar classes and just change the number of columns.
Re: [2008] Declare with an arbitrary number of parameters
You can also come somewhat close to it with .NET's anonymous types.
Anonymous Types lets you define object instances of classes without defining the classes. The definition occurs 'behind the scenes'.
MSDN has this example:
Dim product = New With {Key .Name = "paperclips", .Price = 1.29}
What your 'ultimate' goal seems to be is similar to dynamic types, which is being introduced as part of the Dynamic Language Runtime in .NET 4.0.
Having said all that, I understand what you're saying about declaring classes 'all the time', but that's what OOP is about. You're defining something that you're using which represents a logical entity in your application. Having something 'vague' will only lead to vaguer code.