What if my properties have methods?

  • Thread starter Thread starter Mark Jones
  • Start date Start date
M

Mark Jones

This rather cryptic title is actually a .net oop noob question.

Example:
My customer object has a public property called .name which is a string.
I would like to be able to reference mycust.name.length which would return
the string length of the value of name.

I can't nest property/subs so how do I accomplish this?

Another example would be adding something like .tostring to my own
object properties. What if I wanted to add a .tostring to every
numeric property of my objects? I would like to write just one .tostring
function and then have every property inherit that method. So I could
reference my objct props as follows:

mycust.ordernum.tostring

Little help? Shove in the right direction? Are these considered
"attributes?"
 
Mark Jones said:
This rather cryptic title is actually a .net oop noob question.

Example:
My customer object has a public property called .name which is a
string. I would like to be able to reference mycust.name.length which
would return the string length of the value of name.

I can't nest property/subs so how do I accomplish this?

Another example would be adding something like .tostring to my own
object properties. What if I wanted to add a .tostring to every
numeric property of my objects? I would like to write just one
.tostring function and then have every property inherit that method.
So I could reference my objct props as follows:

mycust.ordernum.tostring

Little help? Shove in the right direction? Are these considered
"attributes?"

You can add your own ToString method to your classes. You can not inherit
from the basic datatypes (Integer, String...) and override their ToString
function.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
Armin Zingler said:
You can add your own ToString method to your classes. You can not inherit
from the basic datatypes (Integer, String...) and override their ToString
function.

I figured I could make a .tostring method on an entire class, but can I add it to
properties?
How would I implement something like:

myorder.number.tostring ?

How do I add the .tostring fucntion to the property below?

Public Class Customer
Dim _number as integer

Public Red Only Property number
Get
Return _number
End Get
End Property
End Class


Thanks in advance!
 
Mark Jones said:
I figured I could make a .tostring method on an entire class, but can
I add it to properties?
How would I implement something like:

myorder.number.tostring ?

How do I add the .tostring fucntion to the property below?

Public Class Customer
Dim _number as integer

Public Red Only Property number
Get
Return _number
End Get
End Property
End Class


The type of the number property is Integer. You can not inherit from the
Integer data type and override the ToString function.
 
Armin Zingler said:
The type of the number property is Integer. You can not inherit from the
Integer data type and override the ToString function.


--

Thanks Armin. Let me try another approach here.

Public Class Customer
Dim _number as integer

Public Read Only Property number
Get
Return _number
End Get
End Property
End Class

How would I add code so that I could use:

Mycustomer.number.mystring ?


What I mean is, once I store this class in a class library,
I want to be able to

Dim myCust as new Customer

and then be able to type mycustomer.number.mystring and have a
string returned.

Is this possible? I can't figure out where to add the code to get this
behavior to occur. I have tried creating a class called "number" that
has a "mystring" method in it but when I start to type the refernce out
in the IDE intellisense shows me mycustomer.|number
|mystring
It doesn't show the "mystring" method after the number property.


That sound very complicated for something very simple. I just need the
construct so I can use

mycust.number.numbertoastring


Thanks again!
 
Mark Jones said:
Thanks Armin. Let me try another approach here.

Public Class Customer
Dim _number as integer

Public Read Only Property number
Get
Return _number
End Get
End Property
End Class

How would I add code so that I could use:

Mycustomer.number.mystring ?


What I mean is, once I store this class in a class library,
I want to be able to

Dim myCust as new Customer

and then be able to type mycustomer.number.mystring and have a
string returned.

Is this possible? I can't figure out where to add the code to get
this behavior to occur. I have tried creating a class called
"number" that has a "mystring" method in it but when I start to type
the refernce out in the IDE intellisense shows me
mycustomer.|number
|mystring
It doesn't show the "mystring" method after the number property.


That sound very complicated for something very simple. I just need
the construct so I can use

mycust.number.numbertoastring


Thanks again!


If the type of the number property is one of your own classes, you can add
any method to your class, and the method can return any String you want. If
the type of the number property is Integer, you can only use the methods
that the Integer data type offers. As Integer is not inheritable, you also
can not override the default behavior.
 
Armin Zingler said:
If the type of the number property is one of your own classes, you can add
any method to your class, and the method can return any String you want. If
the type of the number property is Integer, you can only use the methods
that the Integer data type offers. As Integer is not inheritable, you also
can not override the default behavior.

Yes, the number property is a class that I created it looks like this:

Public Class custid
Dim _custid As String
Dim _len As Integer
Public Property custid()
Get
Return _custid
End Get
Set(ByVal Value)
_custid = Value
End Set
End Property
Public ReadOnly Property strlength()
Get
Return Len(_custid)
End Get
End Property
End Class



Then, my customer object Inherits custID.
However, when I -

Dim MyCust as new Customer

and I try to reference mycust.custid.strnlength I cant.
I can reference mycust.strnlength which isn't what I want.

Thanks again.
 
Mark Jones said:
Yes, the number property is a class that I created it looks like this:

Public Class custid
Dim _custid As String
Dim _len As Integer
Public Property custid()
Get
Return _custid
End Get
Set(ByVal Value)
_custid = Value
End Set
End Property
Public ReadOnly Property strlength()
Get
Return Len(_custid)
End Get
End Property
End Class



Then, my customer object Inherits custID.
However, when I -

Dim MyCust as new Customer

and I try to reference mycust.custid.strnlength I cant.
I can reference mycust.strnlength which isn't what I want.
Are you simply asking how to get the length of the mycust.custid or are you
just using this as an example of your need to override a property's methods?
If the former, then simply use "mycust.custid.Length". Since custid is
(presumably) a String and Strings already have a Length property, this will
work. You don't have to create a new class. If the latter, then what you
have should work as long as the your Customer.custid property is definined
as a custid class. (You might want to turn on Option Strict to make these
variable definitions a little more clear.)

HTH

- Mitchell S. Honnert
 
Mark Jones said:
Yes, the number property is a class that I created it looks like
this:

Public Class custid
Dim _custid As String
Dim _len As Integer
Public Property custid()
Get
Return _custid
End Get
Set(ByVal Value)
_custid = Value
End Set
End Property
Public ReadOnly Property strlength()
Get
Return Len(_custid)
End Get
End Property
End Class


First, you should enable Option Strict, or at least Option Explicit because
you forgot to declare the data type of the properties custid and strlength.
After that, your class looks like this:

Public Class custid
Dim _custid As String
Public Property custid() As String
Get
Return _custid
End Get
Set(ByVal Value As String)
_custid = Value
End Set
End Property
Public ReadOnly Property strlength() As Integer
Get
Return Len(_custid)
End Get
End Property
End Class

Then, my customer object Inherits custID.

Why inherit? Don't you want your customer to have a number property, and
it's data type is CustID?
However, when I -

Dim MyCust as new Customer

and I try to reference mycust.custid.strnlength I cant.
I can reference mycust.strnlength which isn't what I want.

Class Customer
private m_Number as new custid
public readonly property Number as custid
get
return m_Number
end get
end property
end class

Now you can write

Dim MyCust as new Customer

msgbox MyCust.Number.strLength


This is only an example, of course, because usually the strlength property
is superfluous. You could get the same and easier by writing

msgbox MyCust.Number.custid.length
 
Mark,
In addition to Armin's comments, lets start simplier.

My customer object has a public property called .name which is a string.
I would like to be able to reference mycust.name.length which would return
the string length of the value of name.
Because .Name returns a String object, you can use any method of the String
class on the Name property.
I can't nest property/subs
Correct you cannot nest properties or subs.
so how do I accomplish this?
By having the properties of the first class return types (classes,
structures, enums, interfaces, delegates) that have properties & subs that
you want.
Another example would be adding something like .tostring to my own
object properties.
Here is the problem! you do not add ".tostring" to your properties, you add
".tostring" to your types. Your properties return types that may or may not
have ToString overriden!
object properties. What if I wanted to add a .tostring to every
numeric property of my objects?
You would need to define a Numeric Type (Class or Structure) that overrode
the ToString method to do what you want.

Something like:
Public Class Customer

Dim _number as Numeric

Public Read Only Property Number As Numeric
Get
Return _number
End Get
End Property

End Class

Public Class Numeric

' other stuff that made Numeric special!

Public Overrides Function ToString() As String
' add your specific ToString logic here!
End Function

End Class

As Armin stated, you cannot have the Number property be an Integer &
override Integer's ToString method, you need to have the Number property
return your own Type that has its own behavior.

Robin A. Reynolds-Haertle's book "OOP with Microsoft Visual Basic.NET and
Microsoft Visual C# .NET - Step by Step" from MS Press does a good intro to
the "how" of OOP in VB.NET, however it does not cover the "why" of OOP.


Hope this helps
Jay
 
Back
Top