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.
  • 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