Results 1 to 4 of 4

Thread: [2005] Generic & ParamArray

  1. #1

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Lightbulb [2005] Generic & ParamArray

    Hi all,

    I m working on project i which i have to make one method which accept multiple parameter of the Generic class. But the problem is while declaring the function.

    Let me clear the vision.

    Code:
    Class SqlParam(Of T)
    
            Public obj As T
    
            Sub New(ByVal parameterName As String, ByVal type As System.Data.SqlDbType, ByVal size As Integer, ByVal value As T, Optional ByVal isNullable As Boolean = False)
                'Action
            End Sub
    
        End Class
    Now I want to make one method in another class that Insert the record in DB using function having ParamArray of Generic Class object.

    Like

    Code:
    Class SqlHelper
    
    Public Sub Insert(ByVal ParamArray parameters() As SqlParam(Of Object))
    'Action Here
    End Sub
    End Class
    What should i defined there inplace of Object maked red. Bec'z I want to Achieve this



    Code:
    Dim objSql As New SqlHelper()
    objSql.Insert(New SqlParam(Of String)("@ID", SqlDbType.VarChar, 1000, "Coool"), New SqlParam(Of Integer)("@Total", SqlDbType.Int, 50, 23))
    I know there are other methods but I am using generic class bec'z i want type safety. Hope you can focus on this.

    Thanks in Advance.
    I am using .NET 2010 with Windows 7

  2. #2

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: [2005] Generic & ParamArray

    Anybody plz.. Is there any alternative way for this but with type safety?
    I am using .NET 2010 with Windows 7

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Generic & ParamArray

    What you're asking for is not possible. I'd suggest that you declare a common base class or common interface, then have your generic class inherit or implement that. You can then use that type as the type of your paramarray.

    When you define a generic class you're actually defining a whole family of classes. You cannot then declare a parameter whose tpye is a family of classes. It must be one single class out of that family.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Hyperactive Member Coool's Avatar
    Join Date
    Feb 2006
    Location
    System.Coool
    Posts
    333

    Re: [2005] Generic & ParamArray

    Thanks for reply. Is there any other method in ur eye which provide the same functonality with type safety?
    I am using .NET 2010 with Windows 7

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