Results 1 to 10 of 10

Thread: [RESOLVED] [2008] Declare with an arbitrary number of parameters

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [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:
    1. Public Class Test (Of T1, (Of T2))'The (Of T2) means that after the first parameter can be an arbitrary number of parameters
    2.     Public Sub New (Of T1, (Of T2))
    3.         ...
    4.     End Sub
    5.     ...
    6. End Function
    7.  
    8. Dim a As Test (Of String, Of Integer, Of Short, Of Long)
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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?

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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:
    1. 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!!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  6. #6
    Hyperactive Member syntaxeater's Avatar
    Join Date
    Dec 2006
    Location
    Des Moines, IA
    Posts
    460

    Re: [2008] Declare with an arbitrary number of parameters

    Just to see is this is closer to what you want:

    VB Code:
    1. Public Class Test(Of T1, T2, T3, T4)
    2.     Private m_op1 As T1
    3.     Private m_op2 As T2
    4.     Private m_op3 As T3
    5.     Private m_op4 As T4
    6.     Public Sub new (op1 as T1, op2 as T2, op3 as T3, op4 as T4)
    7.         m_op1 = op1
    8.         m_op2 = op2
    9.         m_op3 = op3
    10.         m_op4 = op4
    11.     End Sub
    12.     Public Property Property1 As T1
    13.         Get
    14.             Return m_op1
    15.         End Get
    16.         Set(ByVal value As T1)
    17.             m_op1 = value
    18.         End Set
    19.     End Property
    20.     Public Property Property2 As T2
    21.         Get
    22.             Return m_op2
    23.         End Get
    24.         Set(ByVal value As T2)
    25.             m_op2 = value
    26.         End Set
    27.     End Property
    28.     Public Property Property3 As T3
    29.         Get
    30.             Return m_op3
    31.         End Get
    32.         Set(ByVal value As T3)
    33.             m_op3 = value
    34.         End Set
    35.     End Property
    36.     Public Property Property4 As T4
    37.         Get
    38.             Return m_op4
    39.         End Get
    40.         Set(ByVal value As T4)
    41.             m_op4 = value
    42.         End Set
    43.     End Property
    44. End Class

  7. #7

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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)
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2008] Declare with an arbitrary number of parameters

    Yep, you can't.

    So, why would you want to do this though?

  9. #9

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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
  •  



Click Here to Expand Forum to Full Width