Overloaded Property Set Statement

ErikL

New member
Joined
Jan 22, 2007
Messages
4
Programming Experience
3-5
Hey,

I am trying to overload a property set statement. I think its possible, but do not know how to do.

Here is my code...

'AMOUNT (PROPERTY)
'Amount of transaction
Public Overloads Property Amount(ByVal M) As Double
Get
Return mAmount
End Get
Set(ByVal aAmount As Double)
mAmount = aAmount
End Set
End Property

I want another set to accept a string and then convert it to a double and then store it to mAmount.

Any ideas or help would be great. Thanks!


Erik
 
you can only do that if the datatypes are the same.

in this case they are not the same, a double and a string are two different datatypes

what you can do is overload the property so that both are doubles, but the string one is accepted as a parameter
 
You cannot overload anything if the only difference is the return type

You have written:

Amount(ByVal M)


But what type is that?!

You can only overload something if the argument differs:

Consider a collection:

VB.NET:
Default Public Overloads ReadOnly Property Item(ByVal Index As Integer) As CollectionItem
Get 
End Get 
End Property 

Default Public Overloads ReadOnly Property Item(ByVal Key As String) As CollectionItem
Get 

End Get 
End Property

If you cannot supply this difference, you cannot overload the property

I recommend that you forget trying to allow string assignment, and leave the developers to Double.Parse it themselves..
Or if String is more likely to be used, make the property a string type.
 
you can only do that if the datatypes are the same.

Properties can have differing return types, no problem, but they HAVE to have some way of distincting them by calling argument:

VB.NET:
    Private newPropertyValue As Integer
    Public Overloads Property NewProperty(ByVal i As Integer) As Integer
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As Integer)
            newPropertyValue = value
        End Set
    End Property
    Public Overloads Property NewProperty() As String
        Get
            Return newPropertyValue.ToString()
        End Get
        Set(ByVal value As String)
            newPropertyValue = CInt(value)
        End Set
    End Property

Here the props have a differing return, but I have to supply an int to ensure vb will know which I'm meaning to use:


myClass.NewProperty(1) = 123
myClass.NewProperty = "123"


To be honest, doing so is quite lame. You woulf be better doing:

VB.NET:
Private newPropertyValue As Integer
    Public Overloads Property NewProperty(ByVal i As Integer) As Integer
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As Integer)
            newPropertyValue = value
        End Set
    End Property
    [B]Public Sub ParseNewProperty(strToParse as String)
        newPropertyValue = CInt(strToParse)
    End Sub[/B]

If you want NewProperty as a string.. well that would be what myClass.NewProperty.ToString() is for
 
you can only do that if the datatypes are the same.

in this case they are not the same, a double and a string are two different datatypes

what you can do is overload the property so that both are doubles, but the string one is accepted as a parameter

Properties can have differing return types, no problem, but they HAVE to have some way of distincting them by calling argument:

that's what I already said

this code below fails because the datatypes are different on an overloaded property:
VB.NET:
    Private mSomeProperty As Double

    Public Overloads Property SomeProperty() As Integer
        Get

        End Get
        Set(ByVal Value As Integer)

        End Set
    End Property

    Public Overloads Property SomeProperty() As Dobule
        Get

        End Get
        Set(ByVal Value As Double)

        End Set
    End Property
 
that's what I already said

this code below fails because the datatypes are different on an overloaded property:

No, that code fails becuase they differ only by return type. It is hence not possible for VB.NET to know what is meant by the following statement:

Dim o as Object = yourClass.SomeProperty

Which gets called?

Properties themselves are not objects and hence dont have a datatype.. They have return types ie they return an object. In English language terms, a property would be a verb (action) and what they return would be a noun(object).
The return types of a property CAN differ, just like any overloaded function, but there has to exist some way for the compiler to tell them apart to avoid ambiguous statements. One bad way of doing this is to provide an argument that will never be used (like my example)

A better way is to forget about overloading so as to allow the user to set different types directly. If they cant learn how to convert type_x into a type suitable for your property, they shouldnt be programming.. :D

Edit: Actually, i just thought it ought to be possible to create a custom class that overrides the equals operator for assignment, so as to allow both double and string assignment.. and then make the property of THAT type. But again I would still consider this to be a bit lame, compared to the logical type safety that OO confers.. Stick with it, is my advice
 
what you can do is overload the property so that both are doubles, but the string one is accepted as a parameter

Yes, but what you would end up with there is the crazy situation of:

VB.NET:
    Private mSomeProperty As Integer

    Public Overloads Property SomeProperty() As Integer
        Get

        End Get
        Set
          [B]mSomeProperty = value[/B]
        End Set
    End Property

    Public Overloads Property SomeProperty(s as String) As Integer
        Get

        End Get
        Set
          [B]mSomeProperty = CInt(s)[/B]
        End Set
    End Property

And we would use it like:

myClass.SomeProperty = 123
myClass.SomeProperty("123") = 456

The 456 can be anything we like.. it will be thrown away, and the 123 taken as the value. Ugly ugly ugly. Bad OO too. Leave this well alone, if it would even work, because it is nasty!


As noted before, if you take an int property.. say, Age then have it as an int:

person.Age = 27


If you want to provide suppport for strings, say as roman numerals:

person.ParseAgeRomanNumeral("XXVII")


Dont try and do that with a hacked property:

person.Age("XXVII") = 456


or a property that has a different return type and relies on a dummy argument:

person.Age(456) = "XXVII"


All ugly. Avoid for sure, and use the ParseXXX route..
 
Back
Top