如何创建与ToString类似的方法 [英] How to Create Methods Similar to ToString

查看:61
本文介绍了如何创建与ToString类似的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在VB.NET应用程序中,我可以写


Dim s As String = 3.ToString


这是因为3是类型Int32,这是一个结构,它有一个方法

ToString。


我也希望能够写出


Dim s As String = 3.ToPercent


通过提供我自己的ToPercent实现。如果我能做到这一点,那么我可以做我最终希望做的事情,那就是写下

如下:


Dim mc As New MyClass

Dim i As Integer


i = mc.Value

Textbox1。 Text = mc.Value.ToPercent


我试图制作MyValueClass类型的值,例如,从Int32继承

,但Int32不可继承,这是一个非首发。在任何

的情况下,第一个赋值给出了一个编译时错误,因为MyValueClass没有

的默认属性,除非它需要,否则我不能设置一个/>
参数(它没有)。


有谁能建议如何做到这一点?


TIA


Charles

In a VB.NET app, I can write

Dim s As String = 3.ToString

This is because 3 is of type Int32, which is a structure, which has a method
ToString.

I would also like to be able to write

Dim s As String = 3.ToPercent

by supplying my own implementation of ToPercent. If I could do this, then I
would be able to do what I ultimately wish to do, and that is write
something like the following:

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

I have tried to make Value of type MyValueClass, for example, which inherits
from Int32, but Int32 is not inheritable, so that is a non-starter. In any
case, the first assignment gives a compile time error because there is no
default property of MyValueClass, and I can''t set one unless it takes
parameters (which it doesn''t).

Can anyone suggest how this might be done?

TIA

Charles

推荐答案

" Charles Law" < BL *** @ nowhere.com> schrieb
"Charles Law" <bl***@nowhere.com> schrieb
在一个VB.NET应用程序中,我可以编写

Dim s As String = 3.ToString

这是因为3是类型Int32,这是一个结构,它有一个ToString的方法。

我也希望能够写出来这个Dim s As String = 3.ToPercent

提供我自己的ToPercent实现。如果我能做到这一点,那么我就可以做我最终想做的事情,那就是写下面的内容:

Dim mc As New MyClass
我整数D

我= mc.Value
Textbox1.Text = mc.Value.ToPercent

我试过制作类型的价值例如,MyValueClass继承自Int32,但Int32不可继承,所以这是一个非首发。在任何情况下,第一个赋值都会产生编译时间错误,因为MyValueClass没有默认属性,并且我不能设置一个,除非它需要参数(它不是)。

任何人都可以建议如何做到这一点?
In a VB.NET app, I can write

Dim s As String = 3.ToString

This is because 3 is of type Int32, which is a structure, which has a
method ToString.

I would also like to be able to write

Dim s As String = 3.ToPercent

by supplying my own implementation of ToPercent. If I could do this,
then I would be able to do what I ultimately wish to do, and that is
write something like the following:

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

I have tried to make Value of type MyValueClass, for example, which
inherits from Int32, but Int32 is not inheritable, so that is a
non-starter. In any case, the first assignment gives a compile time
error because there is no default property of MyValueClass, and I
can''t set one unless it takes parameters (which it doesn''t).

Can anyone suggest how this might be done?



正如您所注意到的,您无法扩展不可继承的类。不是你需要的

重载版本的int32.tostring提供了什么?如果没有,你必须

写一个单独的函数来执行转换。

-

Armin


如何引用及其原因:
http:// www .plig.net / nnq / nquote.html
http://www.netmeister.org/news/learn2quote.html


As you''ve noticed, you can not extend a not inheritable class. Don''t the
overloaded versions of int32.tostring offer what you need? If not, you must
write a separate function to perform the conversion.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


嗨Armin


ToPercent方法的吸引力在于它完全符合它所说的

锡。如果我使用ToString,那么每次调用它时我都必须提供格式或格式化程序

。关于编写我自己的函数,我为此做好了准备,但问题是如何将它连接起来以便它被称为

,就像并且具有与ToString相同的语法。


答案是拥有一个MyValueClass类并制作一个ToPercent方法,并且

如果是这样,我该如何创建默认属性?正如我所说,我的真正目标是

能够写出像


Dim mc As New MyClass

Dim i作为整数


i = mc.Value

Textbox1.Text = mc.Value.ToPercent


谢谢


Charles

" Armin Zingler" < AZ ******* @ freenet.de>在消息中写道

news:40 *********************** @ news.freenet.de ...
Hi Armin

The attraction of a ToPercent method is that it does exactly what it says on
the tin. If I use ToString then I have to provide the format or formatter
each time I call it. With regard to writing my own function, I am quite
prepared for this, but the problem then is how to wire it in so that it is
called like, behaves like and has the same syntax as ToString.

Is the answer to have a MyValueClass class and make a ToPercent method, and
if so, how do I create a default property? As I say, my real target is to be
able to write something like

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

Thanks

Charles
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de...
Charles Law < BL *** @ nowhere.com> schrieb
"Charles Law" <bl***@nowhere.com> schrieb
在一个VB.NET应用程序中,我可以编写

Dim s As String = 3.ToString

这是因为3是类型Int32,这是一个结构,它有一个ToString的方法。

我也希望能够写出来这个Dim s As String = 3.ToPercent

提供我自己的ToPercent实现。如果我能做到这一点,那么我就可以做我最终想做的事情,那就是写下面的内容:

Dim mc As New MyClass
我整数D

我= mc.Value
Textbox1.Text = mc.Value.ToPercent

我试过制作类型的价值例如,MyValueClass继承自Int32,但Int32不可继承,所以这是一个非首发。在任何情况下,第一个赋值都会产生编译时间错误,因为MyValueClass没有默认属性,并且我不能设置一个,除非它需要参数(它不是)。

任何人都可以建议如何做到这一点?

正如你所注意到的,你不能扩展一个不可继承的类。不要重载版本的int32.tostring提供你需要的东西吗?如果没有,你
In a VB.NET app, I can write

Dim s As String = 3.ToString

This is because 3 is of type Int32, which is a structure, which has a
method ToString.

I would also like to be able to write

Dim s As String = 3.ToPercent

by supplying my own implementation of ToPercent. If I could do this,
then I would be able to do what I ultimately wish to do, and that is
write something like the following:

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

I have tried to make Value of type MyValueClass, for example, which
inherits from Int32, but Int32 is not inheritable, so that is a
non-starter. In any case, the first assignment gives a compile time
error because there is no default property of MyValueClass, and I
can''t set one unless it takes parameters (which it doesn''t).

Can anyone suggest how this might be done?

As you''ve noticed, you can not extend a not inheritable class. Don''t the
overloaded versions of int32.tostring offer what you need? If not, you



必须写一个单独的函数来执行转换。

-
Armin

如何引用及原因:
http://www.plig.net /nnq/nquote.html
http:// www.netmeister.org/news/learn2quote.html



" Charles Law" < BL *** @ nowhere.com> schrieb
"Charles Law" <bl***@nowhere.com> schrieb
嗨Armin

ToPercent方法的吸引力在于它完全符合它在锡上所说的内容。如果我使用ToString,那么每次调用它时我都必须提供格式
或格式化程序。关于编写我自己的
函数,我已经为此做好了准备,但问题是如何将它连接起来以便它被称为类似,行为类似且具有相同的
语法为ToString。

是否拥有MyValueClass类并制作ToPercent
方法,如果是,我该如何创建默认属性?正如我所说,我的真正目标是能够写出像

Dim mc As New MyClass
Dim i as Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

谢谢
Hi Armin

The attraction of a ToPercent method is that it does exactly what it
says on the tin. If I use ToString then I have to provide the format
or formatter each time I call it. With regard to writing my own
function, I am quite prepared for this, but the problem then is how
to wire it in so that it is called like, behaves like and has the
same syntax as ToString.

Is the answer to have a MyValueClass class and make a ToPercent
method, and if so, how do I create a default property? As I say, my
real target is to be able to write something like

Dim mc As New MyClass
Dim i As Integer

i = mc.Value
Textbox1.Text = mc.Value.ToPercent

Thanks



我理解你的意图并看到这种方法的优点。那么

将一个Integer属性添加到派生的Textbox类中呢?我认为这是更好的地方,因为在显示某个地方之前,整数的可读值是不相关的

,就像在文本框中一样。 Value

属性的setter会将其转换为字符串并将其分配给Text属性。

稍后在代码中你总是要编写percenttextbox1.value = 3只有。

-

Armin


如何报价以及原因:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


I understand your intention and see the advantage of this method. What about
adding an Integer property to a derived Textbox class? I think this is the
better place, because the readable value of an Integer is not relevant
before it is displayed somewhere, like in a textbox. The setter of the Value
property would convert it to a string and assign it to the Text property.
Later in the code you always have to write "percenttextbox1.value = 3" only.
--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html


这篇关于如何创建与ToString类似的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆