• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 170
  • Last Modified:

Object data is being overwritten

I am trying to create a object that is self contained with methods, properties and functions.  My scenario is simple.  I have created one object called a Template that contains a property called name.  I have created a second object called Field that has several properties.  I would like to be able to add multiple fields to the template object.  Then I can use the template object in other programs.  

My problem is that the properties of the Field object are getting overwritten each time I call it in the AddField function.  I am pasting the code below for you to review.   What am I doing wrong?
Thanks,

Public Class PTITemplate
 Private Shared strName As String
 Private Shared aFields As ArrayList
 Public Shared Property TemplateName() As String
   Get
     TemplateName = strName
   End Get
   Set(ByVal Value As String)
     If Value <> "" Then strName = Value
   End Set
 End Property

 Public Shared Function AddField(ByVal FieldName As String, ByVal FieldType As Int16) As Boolean
  Dim addPTIField2 As PTIField
  addPTIField2 = New PTIField
  addPTIField2.Name = FieldName
  addPTIField2.Type = FieldType
  aFields.Add(addPTIField2)

  Dim objElement As PTIField
  For Each obj As Object In aFields
    objElement = CType(obj, PTIField)
    Console.WriteLine(objElement.Name)
    Console.WriteLine(CStr(objElement.Type))
  Next
  addPTIField2 = Nothing
 End Function
End Class

Public Class PTIField
 Private Shared sName As String
 Private Shared iType As Int16
 Sub New()

 End Sub
 Sub New(ByVal fName As String)
   sName = fName
 End Sub
 Sub New(ByVal fName As String, ByVal fType As Int16)
   sName = fName
   iType = fType
 End Sub
 Public Shared Property Name() As String
   Get
     Name = sName
   End Get
   Set(ByVal Value As String)
     sName = Value
   End Set
 End Property
 Public Shared Property Type() As Int16
   Get
     Type = iType
   End Get
   Set(ByVal Value As Int16)
     iType = Value
   End Set
 End Property

End Class
0
Whah
Asked:
Whah
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Remove the Shared keyword EVERYWHERE you see it...
0
 
WhahAuthor Commented:
Thank you very much for that quick and accurate response.  I didn't know that it couldn't be a shared class.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
The Shared keyword indicates that something should be shared across ALL instances of a class.  So when you change the value in one instance, all instances also see that change.

When you remove the shared keyword then each instance keeps its OWN copy of the values.

=)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now