I believe that the answer depends on what you mean by "the User". If
you mean another developer who will be using your class, then all the
previous answers work well and you can ignore the rest of my post.
Inheritance or extension methods may be a good solution.
But if you mean an end user who is going to use your application, then
the best way would be to expose a collection of name-value pairs. For
example, you could have a NameValueCollection object exposed by a
public property called "AdditionalInfo".
---
Imports System.Collections.Specialized
Public Class Person
Private id As String
Private moreInfo As NameValueCollection
'Constructor
Public Sub New(ByVal uniqueID As String)
'Other class initialization.
id = uniqueID
moreInfo = New NameValueCollection()
End Sub
Default Public Property AdditionalInfo(ByVal key As String) As
String
Get
Return moreInfo.Get(key)
End Get
Set(ByVal value As String)
moreInfo.Add(key, value)
End Set
End Property
Public ReadOnly Property PersonID() As String
Get
Return id
End Get
End Property
End Class
---
Couple of things to note about the above code:
1. I have assumed that the type of data that needs to be saved in
every possible property is going to be a String. I would have used a
Generic collection, but I make an exception because - You are a
beginner and beginners may take some time to get familiar with
Generics.
2. Your "hobbies" property will probably have more than one entry so
I used a NameValueCollection as the base for my property. In case you
feel that all possible properties entered by the user should only be
of a single type (let's say, String), then you should probably use a
StringDictionary (or better still, lean towards a Generic dictionary).
Sample usage of this class:
---
Dim mp As New MyClasses.Person("1")
'One way of setting the properties
mp.AdditionalInfo("Name") = "Mr. Smith"
mp.AdditionalInfo("Address") = "1, Hollywood way, Bollywood"
mp.AdditionalInfo("TelephoneNumber") = "1-800-MR-SMITH"
mp.AdditionalInfo("SoftwareVersion") = "0.1"
Dim mp2 As New MyClasses.Person("2")
'Another way of setting the properties. Allowed because it's a Default
Property.
mp2("Name") = "Mr. Brown"
mp2("Address") = "1, Bollywood way, Hollywood"
mp2("TelephoneNumber") = "1-800-MR-BROWN"
mp2("Hobbies") = "80-ball pool"
mp2("Hobbies") = "Collecting dust"
mp2("Hobbies") = "Watching pr0n"
mp2("website") = "www.mrbrownisborn.com"
---
Since this is the end user, You could now build a UI with two
textboxes, one for the name of the property(eg, Hobbies) and the other
for the value to set.
On Feb 27, 3:04 pm, Belisario <[email protected]> wrote:
> Hello everybody!
>
> I'm a VB.Net beginner.
> I'm trying to understand if it's possible to create a class with a
> method in it to add new properties to this class.
>
> Let me put it better: I am building a customer class. This would have
> some base fixed properties like "Name" or "Address", but I would like
> to give the user the possibility to build his own free custom
> property, like "Telephone Number" and "Software version" for Mr. Smith
> and "Customer hobbies" and "Website" for Mr. Brown.
> So:
>
> Mr. Smith's Customer Object properties:
> "Name"
> "Address"
> "Telephone Number"
> "Software version"
>
> Mr. Brown Customer Object properties:
> "Name"
> "Address"
> "Customer hobbies"
> "Website"
>
> I searched a lot through the net but I still can't understand if this
> is possible and how. I heard about something called "reflection", but
> I am still not sure if I am searching the right way...
>
> Could you please help me on this point or show me other possible ways
> to overcome the issue?
>
> Thank you very much!