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 10 1698
"Charles Law" <bl***@nowhere.com> schrieb 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 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
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 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
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
"Charles Law" <bl***@nowhere.com> schrieb 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
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
Charles,
Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per se.
You could always:
Public Structure MyValueClass
Public Function ToPercent() As String
End Function
Public Function ToInteger() As Integer
End Function
End Structure i = mc.Value.ToInteger() Textbox1.Text = mc.Value.ToPercent()
With VB.NET 2005 (Whidbey) we will be able to override CType, the conversion
operator, so you will be able to do something like (based on the CTP release
of Whidbey):
Public Structure MyValueClass
Public Function ToPercent() As String
End Function
Public Function ToInteger() As Integer
End Function
Public Shared Widening Operator CType(value As MyValueClass) As
Integer
Return value.ToInteger()
End Operator
Public Shared Narrowing Operator CType(value As Integer) As
MyValueClass
Return New MyValueClass(value)
End Operator
End Structure
i = mc.Value
mc.Value = CType(i, MyValueClass)
A Widening CType operator does not require CType, while a Narrowing CType
operator does.
Hope this helps
Jay
"Charles Law" <bl***@nowhere.com> wrote in message
news:ey**************@TK2MSFTNGP11.phx.gbl... 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
Hi Armin
I think in creating my example I have omitted some of the key detail. In
using ToString as the example, it suggests that I want to format my
percentage as a string. This is actually just incidental. The real purpose
is to convert a value in one range to another range, which in this case is
the range 0 to 100.
A more representative example would be that I have a value x, such that
-Limit <= x <= +Limit
I need to express x as a percentage of this range, so that if Limit = 1000
and x = 500, then ToPercent evaluates to 75.
I will also have a corresponding method ToAbsolute, which will perform the
reverse conversion, solving for x.
I take your point about the textbox scenario, but I used a textbox only for
illustration.
Charles
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de... "Charles Law" <bl***@nowhere.com> schrieb 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
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
Hi Jay
Thanks for confirming what I was beginning to suspect.
Using the example that I just used to Armin, I would then have
<code>
Public Structure MyValueClass
Public Function ToPercent() As Double
End Function
Public Function ToAbsolute() As Integer
End Function
End Structure
Dim d As Double
Dim i As Integer
d = mc.Value.ToPercent()
i = mc.Value.ToAbsolute()
</code>
showing that I must explicitly call ToAbsolute rather than just write
i = mc.Value
Charles
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:eZ**************@TK2MSFTNGP11.phx.gbl... Charles, Neither VB.NET 2002 nor VB.NET 2003 support what you are attempting, per
se. You could always:
Public Structure MyValueClass
Public Function ToPercent() As String End Function
Public Function ToInteger() As Integer End Function
End Structure
i = mc.Value.ToInteger() Textbox1.Text = mc.Value.ToPercent()
With VB.NET 2005 (Whidbey) we will be able to override CType, the
conversion operator, so you will be able to do something like (based on the CTP
release of Whidbey):
Public Structure MyValueClass
Public Function ToPercent() As String End Function
Public Function ToInteger() As Integer End Function
Public Shared Widening Operator CType(value As MyValueClass) As Integer Return value.ToInteger() End Operator
Public Shared Narrowing Operator CType(value As Integer) As MyValueClass Return New MyValueClass(value) End Operator
End Structure
i = mc.Value mc.Value = CType(i, MyValueClass)
A Widening CType operator does not require CType, while a Narrowing CType operator does.
Hope this helps Jay
"Charles Law" <bl***@nowhere.com> wrote in message news:ey**************@TK2MSFTNGP11.phx.gbl... 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 Hi Armin
I think in creating my example I have omitted some of the key detail. In using ToString as the example, it suggests that I want to format my percentage as a string. This is actually just incidental. The real purpose is to convert a value in one range to another range, which in this case is the range 0 to 100.
A more representative example would be that I have a value x, such that
-Limit <= x <= +Limit
I need to express x as a percentage of this range, so that if Limit = 1000 and x = 500, then ToPercent evaluates to 75.
I will also have a corresponding method ToAbsolute, which will perform the reverse conversion, solving for x.
I take your point about the textbox scenario, but I used a textbox only for illustration.
I'm afraid, I don't have a simple solution for this problem. I would
probably use the class you mentioned (Value property representing x, Limit
property, ToPercent function...). You currently can not ommit explicitly
accessing the Value property, but... see Jay's answer (VB 2006)
--
Armin
How to quote and why: http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
Thanks again Armin.
Charles
"Armin Zingler" <az*******@freenet.de> wrote in message
news:40***********************@news.freenet.de... "Charles Law" <bl***@nowhere.com> schrieb Hi Armin
I think in creating my example I have omitted some of the key detail. In using ToString as the example, it suggests that I want to format my percentage as a string. This is actually just incidental. The real purpose is to convert a value in one range to another range, which in this case is the range 0 to 100.
A more representative example would be that I have a value x, such that
-Limit <= x <= +Limit
I need to express x as a percentage of this range, so that if Limit = 1000 and x = 500, then ToPercent evaluates to 75.
I will also have a corresponding method ToAbsolute, which will perform the reverse conversion, solving for x.
I take your point about the textbox scenario, but I used a textbox only for illustration.
I'm afraid, I don't have a simple solution for this problem. I would probably use the class you mentioned (Value property representing x, Limit property, ToPercent function...). You currently can not ommit explicitly accessing the Value property, but... see Jay's answer (VB 2006)
-- Armin
How to quote and why: http://www.plig.net/nnq/nquote.html http://www.netmeister.org/news/learn2quote.html
> > > Hi Armin I think in creating my example I have omitted some of the key detail. In using ToString as the example, it suggests that I want to format my percentage as a string. This is actually just incidental. The real purpose is to convert a value in one range to another range, which in this case is the range 0 to 100.
A more representative example would be that I have a value x, such that
-Limit <= x <= +Limit
I need to express x as a percentage of this range, so that if Limit = 1000 and x = 500, then ToPercent evaluates to 75.
I will also have a corresponding method ToAbsolute, which will perform the reverse conversion, solving for x.
I take your point about the textbox scenario, but I used a textbox only for illustration.
I'm afraid, I don't have a simple solution for this problem. I would probably use the class you mentioned (Value property representing x, Limit property, ToPercent function...). You currently can not ommit explicitly accessing the Value property, but... see Jay's answer (VB 2006)
-- Armin
Class MyValue
Private mValue As Object
Public Property Value() As Object
Get
Return mValue
End Get
Set (ByVal Value As Object)
mValue = Value
End Set
End Property
Public Function ToPercent() As Single
Return CSng(Value) / 100
End Function
Public Function ToString() As String
Return CStr(Value)
End Function
Public Function ToInteger() As Integer
Return CInt(Value)
End Function
End Class
Class MyClass
Private mValue As MyValue
Public Property Value() As MyValue
Get
Return mValue
End Property
Set (ByVal Value As MyValue)
mValue = Value
End Set
End Property
End Class
Above is not exactly what you want, but still, can work :)
Mythran
Thanks Mythran. I think this is going to be my best bet for now.
Cheers
Charles
"Mythran" <ki********@hotmail.com> wrote in message
news:Oz**************@TK2MSFTNGP12.phx.gbl... > Hi Armin > > I think in creating my example I have omitted some of the key
detail. > In using ToString as the example, it suggests that I want to format > my percentage as a string. This is actually just incidental. The
real > purpose is to convert a value in one range to another range, which
in > this case is the range 0 to 100. > > A more representative example would be that I have a value x, such > that > > -Limit <= x <= +Limit > > I need to express x as a percentage of this range, so that if Limit
= > 1000 and x = 500, then ToPercent evaluates to 75. > > I will also have a corresponding method ToAbsolute, which will > perform the reverse conversion, solving for x. > > I take your point about the textbox scenario, but I used a textbox > only for illustration.
I'm afraid, I don't have a simple solution for this problem. I would probably use the class you mentioned (Value property representing x,
Limit property, ToPercent function...). You currently can not ommit
explicitly accessing the Value property, but... see Jay's answer (VB 2006)
-- Armin
Class MyValue Private mValue As Object
Public Property Value() As Object Get Return mValue End Get Set (ByVal Value As Object) mValue = Value End Set End Property
Public Function ToPercent() As Single Return CSng(Value) / 100 End Function
Public Function ToString() As String Return CStr(Value) End Function
Public Function ToInteger() As Integer Return CInt(Value) End Function End Class
Class MyClass Private mValue As MyValue
Public Property Value() As MyValue Get Return mValue End Property Set (ByVal Value As MyValue) mValue = Value End Set End Property End Class Above is not exactly what you want, but still, can work :)
Mythran
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
|
by: Dan V. |
last post by:
How do I create a one line text file with these control codes? e.g.: 144 =
0x90 and 147 = 0x93?
I am trying to create a one line text file with these characters all one one
row with no spaces.
...
|
by: jez123456 |
last post by:
Hi. I have a web form with a datagrid that displays employees who are on holiday. One of the datagrid columns shows the date of their last day of holiday, but what I really require is the Start Back...
|
by: citytroop |
last post by:
Hi I am creating a form for vieweing and editing faxes. I read the fax
tiff file into the picturebox. And I have two problems:
1.I want to add a function for the users to be able to select an...
|
by: Tubs |
last post by:
Am i missing something or does the .Net Framework have a quirk in the
way methods work on an object. In C++ MFC, if i have a CString and i
use the format method, i format the string i am using. ...
|
by: Mikey |
last post by:
Sample VB .NET source code to create mailing labels or customized letters
using MS Word MailMerge
This VB .NET source code will start MS Word and call methods and set
properties in MS Word to...
|
by: moondaddy |
last post by:
How can I create a public variable similar to an Enum, but rather than
returning integer values it would return String values?
--
moondaddy@nospam.com
|
by: Surrealist |
last post by:
I need something likes as when I create an event procedure.
I can use top-left and top-right dropdown list of code editor
to select object and its exposed events respectively.
Then, the IDE,...
|
by: mirandacascade |
last post by:
O/S: Windows XP Home
Vsn of Python: 2.4
Copy/paste of interactive window is immediately below; the
text/questions toward the bottom of this post will refer to the content
of the copy/paste
...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |