Object data is being overwritten

Posted on 2006-04-11
Last Modified: 2010-04-23
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?

Public Class PTITemplate
 Private Shared strName As String
 Private Shared aFields As ArrayList
 Public Shared Property TemplateName() As String
     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

  Dim objElement As PTIField
  For Each obj As Object In aFields
    objElement = CType(obj, PTIField)
  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
     Name = sName
   End Get
   Set(ByVal Value As String)
     sName = Value
   End Set
 End Property
 Public Shared Property Type() As Int16
     Type = iType
   End Get
   Set(ByVal Value As Int16)
     iType = Value
   End Set
 End Property

End Class
Question by:Whah
    LVL 85

    Accepted Solution

    Remove the Shared keyword EVERYWHERE you see it...

    Author Comment

    Thank you very much for that quick and accurate response.  I didn't know that it couldn't be a shared class.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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.


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now