|
-
Jan 19th, 2009, 09:30 AM
#1
Thread Starter
Fanatic Member
[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)
-
Jan 19th, 2009, 09:46 AM
#2
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?
-
Jan 19th, 2009, 10:41 AM
#3
Thread Starter
Fanatic Member
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!!
-
Jan 19th, 2009, 10:45 AM
#4
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.
-
Jan 19th, 2009, 02:46 PM
#5
Thread Starter
Fanatic Member
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.
-
Jan 19th, 2009, 03:58 PM
#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
-
Jan 20th, 2009, 01:10 PM
#7
Thread Starter
Fanatic Member
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)
-
Jan 20th, 2009, 02:37 PM
#8
Re: [2008] Declare with an arbitrary number of parameters
Yep, you can't.
So, why would you want to do this though?
-
Jan 20th, 2009, 04:43 PM
#9
Thread Starter
Fanatic Member
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.
-
Jan 20th, 2009, 05:29 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|