|
-
Feb 4th, 2009, 03:05 PM
#1
Thread Starter
Member
How to create a Property.subProperty?
Hi Guys,
My final goal is to have a property contain 2 values, the first value will be used the normal way you would in a property,
Eg.
Code:
myClass.myProperty = "myName"
dim str as String = myClass.myProperty
and the second value, will be metaData,
Eg.
Code:
myClass.myProperty.metaData = "mySurname"
dim str as string = myClass.myProperty.metaData
-----------------------------------------------------------
Here is where I'm up to + my problem...
Code:
Public Class clsMetaData
Private m_metaData As String
Public Property metaData() As String
Get
Return m_metaData
End Get
Set(ByVal value As String)
m_metaData = value
End Set
End Property
End Class
Public Class myTester
Private m_myProperty As New clsMetaData
Public Property myProperty() As clsMetaData
Get
Return m_myProperty
End Get
Set(ByVal value As clsMetaData)
m_myProperty = value
End Set
End Property
End Class
Public Class settings1
Dim myClass1 As myTester
Public Sub DoThis()
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty = "myName"
End Sub
End Class
When I use [myClass1.myProperty.metaData = "mySurname"] - Hurray, it seems I have my meta data, until I try [myClass1.myProperty = "myName"] it wont allow me to do this because I am not initializing the property with a string variable.
I cannot seem to overload this property right either because if I add in the next code section, only the return types differ, thus errors.
Code:
Private m_myPropertyValue As String
Public Property myProperty() As String
Get
Return m_myPropertyValue
End Get
Set(ByVal value As String)
m_myPropertyValue = value
End Set
End Property
Thanks in advance for any advice and ideas,
Smithy.
-
Feb 4th, 2009, 03:11 PM
#2
Re: How to create a Property.subProperty?
I would say that a better solution would be to make two members in the metadata class, one with the name, the other with the metadata. After all, for this to work in any way, it would need to have both of those members, so why not put them into the same class?
My usual boring signature: Nothing
 
-
Feb 4th, 2009, 03:17 PM
#3
Thread Starter
Member
Re: How to create a Property.subProperty?
The trouble I have then is the property grid control wont be able to automate the way it accesses my list of properties.
I need to be able to do:
Code:
myClass.myProperty1
myClass.myProperty2
it is the myClass.myProperty.metaData I am flexible with to change.
Last edited by Smithy963; Feb 4th, 2009 at 03:21 PM.
-
Feb 4th, 2009, 10:36 PM
#4
Re: How to create a Property.subProperty?
First up, there's really no such thing as a subproperty. The fact that you're thinking as though there is probably making it harder to achieve your aim. If I do this:
vb.net Code:
var1 = myObject.MyProperty.MySubproperty
then MySubproperty has no direct relationship to myObject or its type at all. myObject has a property named MyProperty and that property returns an object. That object has a property of its own named MySubproperty. MySubproperty is NOT a member of myObject. That code above is equivalent to this:
vb.net Code:
var2 = myObject.MyProperty var1 = var2.MySubproperty
Also, you cannot overload a property that doesn't have any parameters. What defines the difference between overloads is the number, type and order of its parameters. If a property has no parameters then it can't be overloaded. The only properties that generally get overloaded are those that index a collection, e.g. the DataColumnCollection.Item property is overloaded because you can specify a numeric index or a name string.
-
Feb 5th, 2009, 05:03 AM
#5
Thread Starter
Member
Re: How to create a Property.subProperty?
I already understand why its not working,
Thank you for your help, but is there no work around to achieve this some other way.
currently I have created a metaData class, and the meta data is accessible like so...
Code:
myClass.myProperty = "string"
myClass.MetaData.myproperty = "metaString"
This isnt really ideal as I'm now using 2 separate objects instead of an object in an object...
-
Feb 5th, 2009, 09:33 AM
#6
Re: How to create a Property.subProperty?
I don't believe there is any way to achieve what you want, as what I understand your needs to be is that you want myClass.myProperty to get/set a string, while myClass.myProperty.Metadata would require myProperty to get/set a metadata object. Since that would mean that myProperty would return different things in different cases, you have some pretty bad name collision.
Aside from the option you just showed, the closest I can come to would be to move the string down into the metadata class such that myClass.MyProperty would always return a metadata object and the two versions would be:
myClass.myProperty.myString
-and-
myClass.myProperty.MetaData
My usual boring signature: Nothing
 
-
Feb 5th, 2009, 02:59 PM
#7
Thread Starter
Member
Re: How to create a Property.subProperty?
As I said before, I need to be able to access string properties directly so they are automated into my PropertyGrid control.
Perhaps I will understand what I really want and how to do it someday...
but for today I will have to deal with:
myClass.myProperty
-and-
myClass.metaData.myProperty (to retrieve my properties attached metadata)
Thanks a lot for your advice,
Muchos gracias,
Smithy.
-
Feb 5th, 2009, 03:04 PM
#8
Re: How to create a Property.subProperty?
You would usually do:
MyClass.MySubClass.MySubClassProperty
So instead of a property what you need is a sub-class and instead of sub-property you need a property in the sub-class.
-
Feb 5th, 2009, 03:15 PM
#9
Re: How to create a Property.subProperty?
Do something like this for what you are trying to achieve:
vb.net Code:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim MyClassX As New Class2 MyClassX.MyProperty.MySubProperty = "hello" MsgBox(MyClassX.MyProperty.MySubProperty) End Sub End Class Public Class Class2 Private _MyProperty As New SubClass2 Public Property MyProperty() As SubClass2 Get Return _MyProperty End Get Set(ByVal value As SubClass2) _MyProperty = value End Set End Property Public Class SubClass2 Private _MySubProperty As String Public Property MySubProperty() As String Get Return _MySubProperty End Get Set(ByVal value As String) _MySubProperty = value End Set End Property End Class End Class
Pradeep
-
Feb 5th, 2009, 03:34 PM
#10
Thread Starter
Member
Re: How to create a Property.subProperty?
Theres always somebody to jump in and raise spirits when that last shred of hope dwindles....
I take my hat off to you, It's a little more work than I envisioned since I have around 100-200 properties, but this is so stupidly obvious I cannot believe I missed it.
/bow
Thank you.
Smithy
-
Feb 5th, 2009, 03:50 PM
#11
Re: How to create a Property.subProperty?
I don't think what you have showed us is posible to do. Even the above example can not give you this (what you wanted in the first place).
vb Code:
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty = "myName"
In the Pradeep's example myProperty is a type of subClass and it is not a string type. Is this what you wanted? but your example above is not showing that.
If we go with the logic of your example, it tells us that the string data type has a property called metadata which is not true. Isn’t myProperty a string type that takes “myName”?
Last edited by VBDT; Feb 5th, 2009 at 03:58 PM.
-
Feb 5th, 2009, 04:03 PM
#12
Re: How to create a Property.subProperty?
 Originally Posted by VBDT
I don't think what you have showed us is posible to do. Even the above example can not give you this (what you wanted in the first place).
vb Code:
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty = "myName"
In the Pradeep's example myProperty is a type of subClass and it is not a string type. Is this what you wanted? but your example above is not showing that.
If we go with the logic of your example, it tells us that the string data type has a property called metadata which is not true. Isn’t myProperty a string type that takes “myName”?
Code:
Public Property myProperty() As clsMetaData
Isn't myProperty declared as a class?
-
Feb 5th, 2009, 04:10 PM
#13
Re: How to create a Property.subProperty?
 Originally Posted by Pradeep1210
Code:
Public Property myProperty() As clsMetaData
Isn't myProperty declared as a class?
Pradeep, yes it is but from his example the “MyProperty” takes a string “myName” which means it spouse to be a string type. here there is a logic error. So if we let myProperty be of a type clsMetaData than how it is possible to assign to it “myName”?
Initially as I understand he wanted to overload the property and that is not possible with properties as Jmc told it already.
-
Feb 5th, 2009, 04:53 PM
#14
Thread Starter
Member
Re: How to create a Property.subProperty?
I hereby decree VBDT the bringer of bad news...
aw, VBDT your point inspired me to try out a few variations of Pradeep's solution hence my opening line 
Do you think I could just overload the property and give it parameter:
Code:
Public Property ServerName() As String
Get
Return m_HostName
End Get
Set(ByVal value As String)
m_HostName = value
End Set
End Property
Public Property ServerName(ByVal metaData As Boolean) As String
Get
Return m_HostName_metaData
End Get
Set(ByVal value As String)
m_HostName_metaData = value
End Set
End Property
I could then give a special attribute to the Property to hide it from the PropertyGrid control...
Thanks for bouncing some great ideas off me guys.
Smithy
-
Feb 5th, 2009, 04:59 PM
#15
Re: How to create a Property.subProperty?
yes, if your properties differe by the parameter they take. the first property would not conflict with the second one because the second one has a parameter and the first one doesn't. If this is good for you than it can work. Just make a use of the parmeter, let say metaData has a property and you get or set the value of that property through the second property.
-
Feb 5th, 2009, 05:08 PM
#16
Re: How to create a Property.subProperty?
 Originally Posted by VBDT
Pradeep, yes it is but from his example the “MyProperty” takes a string “myName” which means it spouse to be a string type. here there is a logic error. So if we let myProperty be of a type clsMetaData than how it is possible to assign to it “myName”?
Initially as I understand he wanted to overload the property and that is not possible with properties as Jmc told it already.
Ohhh.. I see now.
vb Code:
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty = "myName"
This was something possible in VB6 using parameterless default properties. But in .NET, default properties can't be parameterless. You must provide atleast one parameter.
So the nearest you would come to is:
vb.net Code:
'Add this property to clsMetaData
Default Public Property Prop1(ByVal n As Integer) As String
Get
'get value
End Get
Set(ByVal value As String)
'set value
End Set
End Property
'So then you can access it like this:
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty(0) = "myName"
The other and better way is to add a function which accepts your string and returns a clsMetaData object:
vb.net Code:
Public Class clsMetaData
Private m_metaData As String
Public Property metaData() As String
Get
Return m_metaData
End Get
Set(ByVal value As String)
m_metaData = value
End Set
End Property
Public Shared Function Parse(ByVal s As String) As clsMetaData
'resolve your string to object of type clsMetaData class here
Dim obj As New clsMetaData
Return obj
End Function
End Class
Public Class myTester
Private m_myProperty As New clsMetaData
Public Property myProperty() As clsMetaData
Get
Return m_myProperty
End Get
Set(ByVal value As clsMetaData)
m_myProperty = value
End Set
End Property
End Class
Public Class settings1
Dim myClass1 As myTester
Public Sub DoThis()
myClass1.myProperty.metaData = "mySurname"
myClass1.myProperty = clsMetaData.Parse("myName")
End Sub
End Class
Pradeep
-
Feb 5th, 2009, 06:12 PM
#17
Re: How to create a Property.subProperty?
That's true, as long as you are willing to have one version take a parameter and the other take no parameter, then you can overload as much as you like. It makes the code a bit obscure, though, because the parameter is meaningless.
My usual boring signature: Nothing
 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|