Shared Collection

H

hnolan

I want to create a collection of objects that is shared in an
assembly. What is the best way to do this? My initial thought was to
have a public class that has a private shared collection and then a
public shared property that loads the collection if needed and then
returns the needed object. Is this the correct way to do this?

Ex:

Public Class StandardImportDefinitions

Private Shared _definitions as System.Generics.List(of
ImportDefinition)

Private Sub New()
End Sub

Public Shared Function Definition(definitionName as string) as
ImportDefintion
if _definitions is nothing then
_definitions = New System.Generics.List(of ImportDefinition)
Dim xmldef as New XMLDocument
xmldef.Load(My.Resources.IMPORTSPEC)
'read XML, create ImportDefinition and add to collection
end if
for each def as ImportDefinition in _definitions
if def.name = definitionName then
Return def
end if
next
Return Nothing
End Function
End Class

Public Class ImportDefintion
private _name
private _fields etc...
Public Readonly Property Name as string
Get
Return _name
End Get
End Property
End Class

Public Class ImportSpecWithMappings
Private _thisspec
Private _sourcedata as datatable
Private _mappings as Collection

Public Sub New(standardSpecName as string)
_thisspec = StandardImportDefinitions.Definition(standardSpecName)
End Sub

End Class
 
L

Lloyd Sheen

hnolan said:
I want to create a collection of objects that is shared in an
assembly. What is the best way to do this? My initial thought was to
have a public class that has a private shared collection and then a
public shared property that loads the collection if needed and then
returns the needed object. Is this the correct way to do this?

Ex:

Public Class StandardImportDefinitions

Private Shared _definitions as System.Generics.List(of
ImportDefinition)

Private Sub New()
End Sub

Public Shared Function Definition(definitionName as string) as
ImportDefintion
if _definitions is nothing then
_definitions = New System.Generics.List(of ImportDefinition)
Dim xmldef as New XMLDocument
xmldef.Load(My.Resources.IMPORTSPEC)
'read XML, create ImportDefinition and add to collection
end if
for each def as ImportDefinition in _definitions
if def.name = definitionName then
Return def
end if
next
Return Nothing
End Function
End Class

Public Class ImportDefintion
private _name
private _fields etc...
Public Readonly Property Name as string
Get
Return _name
End Get
End Property
End Class

Public Class ImportSpecWithMappings
Private _thisspec
Private _sourcedata as datatable
Private _mappings as Collection

Public Sub New(standardSpecName as string)
_thisspec = StandardImportDefinitions.Definition(standardSpecName)
End Sub

End Class

Create a base class that contains the shared collection. Then derive all
your classes from this base class. They will then "share" the one instance
of the collection.

Hope this helps
Lloyd Sheen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top