问题描述
在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
推荐答案
正如您所注意到的,您无法扩展不可继承的类。不是你需要的
重载版本的int32.tostring提供了什么?如果没有,你必须
写一个单独的函数来执行转换。
-
Armin
如何引用及其原因:
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
如何引用及原因:
我理解你的意图并看到这种方法的优点。那么
将一个Integer属性添加到派生的Textbox类中呢?我认为这是更好的地方,因为在显示某个地方之前,整数的可读值是不相关的
,就像在文本框中一样。 Value
属性的setter会将其转换为字符串并将其分配给Text属性。
稍后在代码中你总是要编写percenttextbox1.value = 3只有。
-
Armin
如何报价以及原因:
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类似的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!