的ToString() [英] ToString()
问题描述
我有一个属性,我想将tostring与
关联起来。所以这是有效的:
-----
Dim x as string
x = myobject .myproperty.tostring
-----
我知道我必须覆盖tostring()但是我该怎么做
得到我想要的结果?
谢谢!
< an ******* @ discussion.microsoft.com>在消息中写道
news:0b **************************** @ phx.gbl ... < blockquote class =post_quotes>我有一个属性,我想将tostring与
联系起来。所以这是有效的:
-----
Dim x as string
x = myobject.myproperty.tostring
--- -
我知道我必须覆盖tostring()但是我该怎么做才能得到我想要的结果?
谢谢!
通常,覆盖将出现在实例化的类中。在你的
示例中,用于创建myobject的类。
-
Peter [MVP Visual Developer]
所有行业的杰克,无人掌握。
在此页面中直接展示了一个样本。
> http://support.microsoft.com /default...b;en-us;309357
我希望这有帮助吗?
Cor
<一个******* @ discussions.microsoft.com> schrieb:我有一个属性,我想将tostring与
联系起来。所以这是有效的:
-----
Dim x as string
x = myobject.myproperty.tostring
--- -
我知道我必须覆盖tostring()但是我该怎么做才能得到我想要的结果?
''ToString''必须在属性类型或其中一个
基类中实现。示例:
\\\
公共类人员
私人m_Age为整数
私有m_Name为字符串
公共财产年龄()为整数
获取
返回m_Age
结束获取
设置(ByVal值为整数)
m_Age =价值
结束集
结束物业
公共属性名称()为字符串
获取
返回m_Name
结束获取
套装(ByVal Value As String)
m_Name =价值
套装
结束物业
>
Public Overrides Function ToString()As String
返回名称& " (& CStr(Age)&")"
结束功能
结束课程
..
..
..
Dim p As New Person()
p.Age = 44
p.Name =" Pink Panther"
MsgBox(p.ToString())''" Pink Panther(44)"。
///
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/ >
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
I have a property where I want to associate tostring with
it. So that this would be valid:
-----
Dim x as string
x = myobject.myproperty.tostring
-----
I know I have to overide tostring() but where do I do
that to get my desired result?
Thanks!
<an*******@discussions.microsoft.com> wrote in message
news:0b****************************@phx.gbl...I have a property where I want to associate tostring with
it. So that this would be valid:
-----
Dim x as string
x = myobject.myproperty.tostring
-----
I know I have to overide tostring() but where do I do
that to get my desired result?
Thanks!
Normally, the override would appear in the instantiated class. In your
example, the class that was used to create myobject.
--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
In this page is direct a sample that shows it you.
http://support.microsoft.com/default...b;en-us;309357
I hope this helps?
Cor
<an*******@discussions.microsoft.com> schrieb:I have a property where I want to associate tostring with
it. So that this would be valid:
-----
Dim x as string
x = myobject.myproperty.tostring
-----
I know I have to overide tostring() but where do I do
that to get my desired result?
''ToString'' must be implemented in the type of the property or one of its
base classes. Example:
\\\
Public Class Person
Private m_Age As Integer
Private m_Name As String
Public Property Age() As Integer
Get
Return m_Age
End Get
Set(ByVal Value As Integer)
m_Age = Value
End Set
End Property
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal Value As String)
m_Name = Value
End Set
End Property
Public Overrides Function ToString() As String
Return Name & " (" & CStr(Age) & ")"
End Function
End Class
..
..
..
Dim p As New Person()
p.Age = 44
p.Name = "Pink Panther"
MsgBox(p.ToString()) '' "Pink Panther (44)".
///
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
这篇关于的ToString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!