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. There must be a better way to enter lots of strongly typed properties in a class

There must be a better way to enter lots of strongly typed properties in a class

Scheduled Pinned Locked Moved Visual Basic
csharpdatabasevisual-studiodata-structureshelp
17 Posts 5 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
    MartyK2007
    wrote on last edited by
    #1

    Hello, I am trying to write a strongly typed object class (so I can bind the properties of the class object to form controls) along the lines of: Public Class foo Private _myCol As Collection Public Property Name() As String Get Return _myCol("Name") End Get Set(ByVal value As String) _myCol.Remove("Name") _myCol.Add(value, "Name") End Set End Property Public Property Title() As String Get Return _myCol("Title") End Get Set(ByVal value As String) _myCol.Remove("Title") _myCol.Add(value, "Title") End Set End Property End Class This is quite simply awful. If I use an array instead of a collection I can get rid of the collection remove and add rubbish. In effect I could replace Return _myCol("Title") with Return _myArray(1) This means that I have to relate each property to an index (Name to 1 and Title to 2 for example) and it means that when I create the class I have to manually type this relationship in. The real problem with this is that I anticpate having lots of classes like this each with lots of properties and frankly I am a bit lazy and dont want to physically key them in like this. One option was to bind the controls to an exposed dataset in my foo class instead but I dont want to do that. I want to expose Foo.Name and Foo.Title etc. There must be an easier way than that above - any ideas?? (either a template based way of entering them into visual studio or a completly new way of coding them) thanks in advance Martin -- modified at 5:25 Wednesday 27th June, 2007

    life is a bowl of cherries go on take a byte

    M S 2 Replies Last reply
    0
    • M MartyK2007

      Hello, I am trying to write a strongly typed object class (so I can bind the properties of the class object to form controls) along the lines of: Public Class foo Private _myCol As Collection Public Property Name() As String Get Return _myCol("Name") End Get Set(ByVal value As String) _myCol.Remove("Name") _myCol.Add(value, "Name") End Set End Property Public Property Title() As String Get Return _myCol("Title") End Get Set(ByVal value As String) _myCol.Remove("Title") _myCol.Add(value, "Title") End Set End Property End Class This is quite simply awful. If I use an array instead of a collection I can get rid of the collection remove and add rubbish. In effect I could replace Return _myCol("Title") with Return _myArray(1) This means that I have to relate each property to an index (Name to 1 and Title to 2 for example) and it means that when I create the class I have to manually type this relationship in. The real problem with this is that I anticpate having lots of classes like this each with lots of properties and frankly I am a bit lazy and dont want to physically key them in like this. One option was to bind the controls to an exposed dataset in my foo class instead but I dont want to do that. I want to expose Foo.Name and Foo.Title etc. There must be an easier way than that above - any ideas?? (either a template based way of entering them into visual studio or a completly new way of coding them) thanks in advance Martin -- modified at 5:25 Wednesday 27th June, 2007

      life is a bowl of cherries go on take a byte

      M Offline
      M Offline
      MohammadAmiry
      wrote on last edited by
      #2

      Why don't you create a structure with the properties you like, i.e.

      Class _MyProperies
       Public Title, Name, etc As String
      End Class
      

      And then in your foo class define an array of this type Private _MyArray As _MyProperties() Does this help you?

      M 1 Reply Last reply
      0
      • M MohammadAmiry

        Why don't you create a structure with the properties you like, i.e.

        Class _MyProperies
         Public Title, Name, etc As String
        End Class
        

        And then in your foo class define an array of this type Private _MyArray As _MyProperties() Does this help you?

        M Offline
        M Offline
        MartyK2007
        wrote on last edited by
        #3

        Hi, thanks for that but it doesnt really my foo properties woud be Public Property Name ... get .. return _Myarry(Index) .... so for each property I would need to key in name and the index in a couple of places I was hoping to avoid that for 2 reasons 1.. I dont like saying property Name is Index 1 in the _MyArray - Its just ugly 2.. I am lazy and dont like keying in Name and index for every property in every class any other ideas?? thanks Martin

        life is a bowl of cherries go on take a byte

        T 1 Reply Last reply
        0
        • M MartyK2007

          Hi, thanks for that but it doesnt really my foo properties woud be Public Property Name ... get .. return _Myarry(Index) .... so for each property I would need to key in name and the index in a couple of places I was hoping to avoid that for 2 reasons 1.. I dont like saying property Name is Index 1 in the _MyArray - Its just ugly 2.. I am lazy and dont like keying in Name and index for every property in every class any other ideas?? thanks Martin

          life is a bowl of cherries go on take a byte

          T Offline
          T Offline
          Taylor
          wrote on last edited by
          #4

          Private C As New Collection Property MyProperties(ByVal i As String) As String Get Return C(i) End Get Set(ByVal value As String) Try C.Remove(i) Catch ex As Exception Finally C.Add(value, i) End Try End Set End Property

          A.E.K

          M 1 Reply Last reply
          0
          • T Taylor

            Private C As New Collection Property MyProperties(ByVal i As String) As String Get Return C(i) End Get Set(ByVal value As String) Try C.Remove(i) Catch ex As Exception Finally C.Add(value, i) End Try End Set End Property

            A.E.K

            M Offline
            M Offline
            MartyK2007
            wrote on last edited by
            #5

            Hi, Thanks for that but any code using foo class would have to do foo.MyProperties("Name") = "John" I absolutley need the outside world to see it as strongly typed properties such as Foo.Name and Foo.Type. This is because the classes will be used as business objects and I may wish to add extra code to one or two of a classes properties later on . I dont want a big case statement in the properties code (or indeed a function called by the properties code). thanks anyway Martin

            life is a bowl of cherries go on take a byte

            D 1 Reply Last reply
            0
            • M MartyK2007

              Hi, Thanks for that but any code using foo class would have to do foo.MyProperties("Name") = "John" I absolutley need the outside world to see it as strongly typed properties such as Foo.Name and Foo.Type. This is because the classes will be used as business objects and I may wish to add extra code to one or two of a classes properties later on . I dont want a big case statement in the properties code (or indeed a function called by the properties code). thanks anyway Martin

              life is a bowl of cherries go on take a byte

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              In that case, you could do two classes. One for an instance of the object that holds all the properties and a second as a collection of those objects. The .NET Framework uses a setup like this extensively.

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007

              M 1 Reply Last reply
              0
              • M MartyK2007

                Hello, I am trying to write a strongly typed object class (so I can bind the properties of the class object to form controls) along the lines of: Public Class foo Private _myCol As Collection Public Property Name() As String Get Return _myCol("Name") End Get Set(ByVal value As String) _myCol.Remove("Name") _myCol.Add(value, "Name") End Set End Property Public Property Title() As String Get Return _myCol("Title") End Get Set(ByVal value As String) _myCol.Remove("Title") _myCol.Add(value, "Title") End Set End Property End Class This is quite simply awful. If I use an array instead of a collection I can get rid of the collection remove and add rubbish. In effect I could replace Return _myCol("Title") with Return _myArray(1) This means that I have to relate each property to an index (Name to 1 and Title to 2 for example) and it means that when I create the class I have to manually type this relationship in. The real problem with this is that I anticpate having lots of classes like this each with lots of properties and frankly I am a bit lazy and dont want to physically key them in like this. One option was to bind the controls to an exposed dataset in my foo class instead but I dont want to do that. I want to expose Foo.Name and Foo.Title etc. There must be an easier way than that above - any ideas?? (either a template based way of entering them into visual studio or a completly new way of coding them) thanks in advance Martin -- modified at 5:25 Wednesday 27th June, 2007

                life is a bowl of cherries go on take a byte

                S Offline
                S Offline
                SHatchard
                wrote on last edited by
                #7

                Are you using .net 1.1 or 2.0? If you are using 2.0 you can use generics Public Class MyProperties Public Property1 as string Public Property2 as string End Class Dim list as new list(of myproperties) You then then access the properies in a strongly-typed way Dim name as string = list(0).MyProperty1

                M 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  In that case, you could do two classes. One for an instance of the object that holds all the properties and a second as a collection of those objects. The .NET Framework uses a setup like this extensively.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007

                  M Offline
                  M Offline
                  MartyK2007
                  wrote on last edited by
                  #8

                  Sorry dave, I'm not quite following how that would work can you give me a snippet I dont see how it makes adifference if its one Foo object or a collection of foo objects. Foo.Name still needs to get to a varaible or internal collection I have just been reading up on intellisense code snippets that is in effect a template for my class and that may help thanks Martin

                  life is a bowl of cherries go on take a byte

                  D 1 Reply Last reply
                  0
                  • S SHatchard

                    Are you using .net 1.1 or 2.0? If you are using 2.0 you can use generics Public Class MyProperties Public Property1 as string Public Property2 as string End Class Dim list as new list(of myproperties) You then then access the properies in a strongly-typed way Dim name as string = list(0).MyProperty1

                    M Offline
                    M Offline
                    MartyK2007
                    wrote on last edited by
                    #9

                    hi, I am on version 2 I think this is the same as the array solution it would look something like public property Name as string get return list(0).Myproperty1 ..........etc I need to relate property Name to Index position 0 in this case. In my current development I will need about 30 of these clases averaging 20 of these properties per class. The worst class will have 80 or so properties. I am really looking for something like public property Name as string get return ("Name") 'where is some classed object ............... I could use collection but writing back changes is upgly (removing the item then adding it again - yuk) I could use arrayLists as in return ArrayList.BinarySearch("Name") but I have to keep the arraylist sorted and am wondering how fast the binarysearch will be compared to the collection name index. Although knowing microsoft its probably the same code. and then I am looking for some quick n painless way to actually type in these classes into visual studio. Thanks Martin

                    life is a bowl of cherries go on take a byte

                    1 Reply Last reply
                    0
                    • M MartyK2007

                      Sorry dave, I'm not quite following how that would work can you give me a snippet I dont see how it makes adifference if its one Foo object or a collection of foo objects. Foo.Name still needs to get to a varaible or internal collection I have just been reading up on intellisense code snippets that is in effect a template for my class and that may help thanks Martin

                      life is a bowl of cherries go on take a byte

                      D Offline
                      D Offline
                      Dave Kreskowiak
                      wrote on last edited by
                      #10

                      For the Person:

                      Public Class Person
                      Private _Name As String
                      Private _Title As String

                      Public Property Name As String
                      ...
                      Public Property Title As String
                      ...
                      

                      End Class

                      For the Collection of Persons (simplified)

                      Public Class PersonsCollection
                      Implements IList, ICollection, IEnumerable

                      Private Persons As Person()
                      Private PersonsCount As Integer
                      
                      Public Sub New()
                      End Sub
                      
                      Public Overridable Sub Add(ByVal person As Person)
                         ...
                      End Sub
                      
                      ...
                      

                      End Class

                      Of course, in 2005, you can skip the collection and just use a typed collection instead:

                      Dim Persons As New List(Of Person)
                      

                      A guide to posting questions on CodeProject[^]
                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                           2006, 2007

                      M 1 Reply Last reply
                      0
                      • D Dave Kreskowiak

                        For the Person:

                        Public Class Person
                        Private _Name As String
                        Private _Title As String

                        Public Property Name As String
                        ...
                        Public Property Title As String
                        ...
                        

                        End Class

                        For the Collection of Persons (simplified)

                        Public Class PersonsCollection
                        Implements IList, ICollection, IEnumerable

                        Private Persons As Person()
                        Private PersonsCount As Integer
                        
                        Public Sub New()
                        End Sub
                        
                        Public Overridable Sub Add(ByVal person As Person)
                           ...
                        End Sub
                        
                        ...
                        

                        End Class

                        Of course, in 2005, you can skip the collection and just use a typed collection instead:

                        Dim Persons As New List(Of Person)
                        

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        M Offline
                        M Offline
                        MartyK2007
                        wrote on last edited by
                        #11

                        Hi my problem is in here Public Class Person Private _Name As String Private _Title As String Public Property Name As String get return _name set (value..) _name = value In this property I have enterd Name once and _Name twice. now this is fine for one or two properties but when you have 80 of these properties in a class and you need to create many classes then typing it in becomes painfull. I think the code snippet will help but I was hoping for some kind of generator where I could key in a list and have a class created for me with those properties set from the list. End Class thanks Martin

                        life is a bowl of cherries go on take a byte

                        D T 2 Replies Last reply
                        0
                        • M MartyK2007

                          Hi my problem is in here Public Class Person Private _Name As String Private _Title As String Public Property Name As String get return _name set (value..) _name = value In this property I have enterd Name once and _Name twice. now this is fine for one or two properties but when you have 80 of these properties in a class and you need to create many classes then typing it in becomes painfull. I think the code snippet will help but I was hoping for some kind of generator where I could key in a list and have a class created for me with those properties set from the list. End Class thanks Martin

                          life is a bowl of cherries go on take a byte

                          D Offline
                          D Offline
                          Dave Kreskowiak
                          wrote on last edited by
                          #12

                          MartyK2007 wrote:

                          In this property I have enterd Name once and _Name twice. now this is fine for one or two properties but when you have 80 of these properties in a class and you need to create many classes then typing it in becomes painfull.

                          A generator or two might exist, but I don't know of any. Try Googling for "VB.NET Property code generation".

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          1 Reply Last reply
                          0
                          • M MartyK2007

                            Hi my problem is in here Public Class Person Private _Name As String Private _Title As String Public Property Name As String get return _name set (value..) _name = value In this property I have enterd Name once and _Name twice. now this is fine for one or two properties but when you have 80 of these properties in a class and you need to create many classes then typing it in becomes painfull. I think the code snippet will help but I was hoping for some kind of generator where I could key in a list and have a class created for me with those properties set from the list. End Class thanks Martin

                            life is a bowl of cherries go on take a byte

                            T Offline
                            T Offline
                            Taylor
                            wrote on last edited by
                            #13

                            Write another program that gets the name of the properties ,adds them to a list and finally makes the code for you to add to your class. The function below may help you Function Generate(ByVal mc As Collection) As String Dim str As String For i As Integer = 1 To mc.Count str &= "Public Property " & mc(i) & "() As String" * vbCrLf & _ "Get" & vbCrLf & "Return MyDataCol(""" & mc(i) & """)" & vbCrLf & _ "End Get" & vbCrLf & "Set(ByVal value As String)" & vbCrLf & _ "MyDataCol.Remove(""" & mc(i) & """)" & vbCrLf & "MyDataCol.Add(value, """ & mc(i) & """)" & _ vbCrLf & "End Set" & vbCrLf & "End Property" & vbCrLf Next Return str End Function where mc is a collection of property names

                            A.E.K

                            D 1 Reply Last reply
                            0
                            • T Taylor

                              Write another program that gets the name of the properties ,adds them to a list and finally makes the code for you to add to your class. The function below may help you Function Generate(ByVal mc As Collection) As String Dim str As String For i As Integer = 1 To mc.Count str &= "Public Property " & mc(i) & "() As String" * vbCrLf & _ "Get" & vbCrLf & "Return MyDataCol(""" & mc(i) & """)" & vbCrLf & _ "End Get" & vbCrLf & "Set(ByVal value As String)" & vbCrLf & _ "MyDataCol.Remove(""" & mc(i) & """)" & vbCrLf & "MyDataCol.Add(value, """ & mc(i) & """)" & _ vbCrLf & "End Set" & vbCrLf & "End Property" & vbCrLf Next Return str End Function where mc is a collection of property names

                              A.E.K

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              Nah. I hate copy and paste coding and refuse to use such a technique in an addin. I'd much rather have the addin write, and if need be, rewrite the property code for me, directly in the CodeDom, when I change the member variables.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007

                              M 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                Nah. I hate copy and paste coding and refuse to use such a technique in an addin. I'd much rather have the addin write, and if need be, rewrite the property code for me, directly in the CodeDom, when I change the member variables.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                     2006, 2007

                                M Offline
                                M Offline
                                MartyK2007
                                wrote on last edited by
                                #15

                                you can rewrite the property?? Is this a special VB way or "Just" writing a program to read in the .vb file and change it then save the new file overwriting the old one?? thanks Martin

                                life is a bowl of cherries go on take a byte

                                D 1 Reply Last reply
                                0
                                • M MartyK2007

                                  you can rewrite the property?? Is this a special VB way or "Just" writing a program to read in the .vb file and change it then save the new file overwriting the old one?? thanks Martin

                                  life is a bowl of cherries go on take a byte

                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #16

                                  No. This would be a VS AddIn (not for Express though!) that manipulates the document object model that is your code, otherwise know as CodeDom. Search for that term on MSDN and you'll come up with all kinds of information.

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                       2006, 2007

                                  M 1 Reply Last reply
                                  0
                                  • D Dave Kreskowiak

                                    No. This would be a VS AddIn (not for Express though!) that manipulates the document object model that is your code, otherwise know as CodeDom. Search for that term on MSDN and you'll come up with all kinds of information.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                         2006, 2007

                                    M Offline
                                    M Offline
                                    MartyK2007
                                    wrote on last edited by
                                    #17

                                    wow I never knew that existed NET is getting bigger and bigger - sigh - brain overload!! Thanks for that - very interesting Martin

                                    life is a bowl of cherries go on take a byte

                                    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