There must be a better way to enter lots of strongly typed properties in a class
-
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
-
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
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? -
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?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
-
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
-
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
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
-
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
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 -
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
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
-
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, 2007Sorry 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
-
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
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
-
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
For the Person:
Public Class Person
Private _Name As String
Private _Title As StringPublic Property Name As String ... Public Property Title As String ...
End Class
For the Collection of Persons (simplified)
Public Class PersonsCollection
Implements IList, ICollection, IEnumerablePrivate 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 -
For the Person:
Public Class Person
Private _Name As String
Private _Title As StringPublic Property Name As String ... Public Property Title As String ...
End Class
For the Collection of Persons (simplified)
Public Class PersonsCollection
Implements IList, ICollection, IEnumerablePrivate 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, 2007Hi 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
-
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
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 -
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
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 namesA.E.K
-
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 namesA.E.K
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 -
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, 2007you 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
-
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
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 -
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, 2007wow 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