473,320 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

What if my properties have methods?

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?"
Nov 20 '05 #1
9 1050
"Mark Jones" <no**************@devnullbucket.org> schrieb
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

Nov 20 '05 #2
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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!
Nov 20 '05 #3
"Mark Jones" <no**************@devnullbucket.org> schrieb
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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

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

Nov 20 '05 #4
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
"Mark Jones" <no**************@devnullbucket.org> schrieb
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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

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!

Nov 20 '05 #5
"Mark Jones" <no**************@devnullbucket.org> schrieb

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

Nov 20 '05 #6
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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


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.



Nov 20 '05 #7

"Mark Jones" <no**************@devnullbucket.org> wrote in message
news:40***********************@news.twtelecom.net. ..
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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

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

Thanks again.




Nov 20 '05 #8
"Mark Jones" <no**************@devnullbucket.org> schrieb
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
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

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

--
Armin

Nov 20 '05 #9
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

"Mark Jones" <no**************@devnullbucket.org> wrote in message
news:40***********************@news.twtelecom.net. .. 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?"

Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements -...
23
by: Marcin Grzêbski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
21
by: Helge Jensen | last post by:
I've got some data that has Set structure, that is membership, insert and delete is fast (O(1), hashing). I can't find a System.Collections interface that matches the operations naturally offered...
13
by: Peter Kirk | last post by:
Hi there, can someone tell me what exactly a "property" is in a C# class? As far as I can see it is "two methods" - ie a getter and a setter for an instance variable. What is the difference...
1
by: Mitan | last post by:
Hello, I'm a beginner with what appears to be a simple question for which I haven't been able to get an answer. Can someone explain what "implementation code" is in relation to VB.NET? I want to be...
8
by: kevin | last post by:
I have a form and in the form I have a sub that uses a class I instantiate using visual basic code: Public oCP As New Rs232 'instantiate the comm port I need to share this sub with...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
9
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif",...
4
by: N.RATNAKAR | last post by:
hai, what is abstract class and abstract method
22
by: Peter | last post by:
Hi I was wondering, why use properties instead of get and set methods? I have a colleague who dislikes properties - and much prefers to write "string GetName()" and "void SetName(string name)"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.