Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Trying to create generic object: SOLVED

Trying to create generic object: SOLVED

Scheduled Pinned Locked Moved Visual Basic
helpdatabasexmlquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mo1492
    wrote on last edited by
    #1

    As usual, as soon as I post, if figure it out; sorry. :( This Me._Column = Activator.CreateInstance(GetType(T))() Should be this Me._Column = Activator.CreateInstance(GetType(T)). However, now I have another problem; I'll work on this awhile. I hope I am in the right group. I have a Class that creates an xml file that currently works. However, I would like to change the definition to define 1 of 2 generic types (T) when creating the file; Column1 or Column2. The code below is what I have come up with through searches. Everything compiles but when I create an instance of MyXmlDef I get this error. 'No Default Member Found for type 'Column1' A search on the error says: A late-bound index Get or Set call has been attempted. Ensure that the object has a default property with a Get or Set, depending on which you are trying to make. I have tried to define a Property for Column below but it doesn't fix the error. I don't know what 'object' the error refers to. Can someone help? Thank you ' I am just showing the relevant code ' I have hardcoded this to Column1 to test public Class MyXmlDef _ Public _ColumnList As ColumnList(Of Column1) Public Sub New() Me._ColumnList = New ColumnList(Of Column1) end sub end Class Class ColumnList(Of T) _ Public _Column As T ' This is test code to try and fix the error; it doesn't fix. Public Property Column() As T Get Return Me._Column End Get Set(ByVal value As T) Me._Column = value End Set End Property Public Sub New() ' THis is the code I found on the internet to create an object from generic. ' However, it does not compile. ' Me._Column = Activator.CreateInstance(T)() ' This compiles but get the error described above for Column1. ' I do not know if this is valid or not. Me._Column = Activator.CreateInstance(GetType(T))() End Sub End Class Class Column1 _ Public _ProductName As XMLProductName ' ... other XmlElments Public Sub New() '... initialize elements end sub End Class Class Column2 _ Public _ProductName As XMLProductName ' ... o

    Richard DeemingR 1 Reply Last reply
    0
    • M mo1492

      As usual, as soon as I post, if figure it out; sorry. :( This Me._Column = Activator.CreateInstance(GetType(T))() Should be this Me._Column = Activator.CreateInstance(GetType(T)). However, now I have another problem; I'll work on this awhile. I hope I am in the right group. I have a Class that creates an xml file that currently works. However, I would like to change the definition to define 1 of 2 generic types (T) when creating the file; Column1 or Column2. The code below is what I have come up with through searches. Everything compiles but when I create an instance of MyXmlDef I get this error. 'No Default Member Found for type 'Column1' A search on the error says: A late-bound index Get or Set call has been attempted. Ensure that the object has a default property with a Get or Set, depending on which you are trying to make. I have tried to define a Property for Column below but it doesn't fix the error. I don't know what 'object' the error refers to. Can someone help? Thank you ' I am just showing the relevant code ' I have hardcoded this to Column1 to test public Class MyXmlDef _ Public _ColumnList As ColumnList(Of Column1) Public Sub New() Me._ColumnList = New ColumnList(Of Column1) end sub end Class Class ColumnList(Of T) _ Public _Column As T ' This is test code to try and fix the error; it doesn't fix. Public Property Column() As T Get Return Me._Column End Get Set(ByVal value As T) Me._Column = value End Set End Property Public Sub New() ' THis is the code I found on the internet to create an object from generic. ' However, it does not compile. ' Me._Column = Activator.CreateInstance(T)() ' This compiles but get the error described above for Column1. ' I do not know if this is valid or not. Me._Column = Activator.CreateInstance(GetType(T))() End Sub End Class Class Column1 _ Public _ProductName As XMLProductName ' ... other XmlElments Public Sub New() '... initialize elements end sub End Class Class Column2 _ Public _ProductName As XMLProductName ' ... o

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      You just need a constraint on your generic type parameter:

      Class ColumnList(Of T As New)
      ...
      Public Sub New()
      Me._Column = New T()
      End Sub
      ...

      Generic Types in Visual Basic (Visual Basic) | Microsoft Docs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups