By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,183 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,183 IT Pros & Developers. It's quick & easy.

Percent TypeConverter

P: n/a
Is there a TypeConverter that converts Doubles to percent values in a PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the TypeConverter that I want.

Thank you,
Lance
Nov 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
View this thread from Jay Harlow and Charles Law. It might help.

-------------

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


"ljlevend" <lj******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com: Is there a TypeConverter that converts Doubles to percent values in a
PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the
TypeConverter that I want.

Thank you,
Lance


Nov 20 '05 #2

P: n/a
Thanks for the help, but that isn't quite what I'm looking for. I need a TypeConverter class rather than a technique that converts a value. I could create my own TypeConverter class, but I want to make sure that there isn't a standard percent TypeConverter before doing so.

Lance
"scorpion53061" wrote:
View this thread from Jay Harlow and Charles Law. It might help.

-------------

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



"ljlevend" <lj******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com:
Is there a TypeConverter that converts Doubles to percent values in a
PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the
TypeConverter that I want.

Thank you,
Lance


Nov 20 '05 #3

P: n/a
Thanks for the help, but that isn't quite what I'm looking for. I need a TypeConverter class rather than a technique for converting the value. I could create my own TypeConverter, but I want to make sure that there isn't a standard TypeConverter for converting percents before doing so.

Lance
"scorpion53061" wrote:
View this thread from Jay Harlow and Charles Law. It might help.

-------------

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



"ljlevend" <lj******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com:
Is there a TypeConverter that converts Doubles to percent values in a
PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the
TypeConverter that I want.

Thank you,
Lance


Nov 20 '05 #4

P: n/a
Check out the OpacityConverter class
That's what the opacity property uses

/claes

"ljlevend" <lj******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Is there a TypeConverter that converts Doubles to percent values in a PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the
TypeConverter that I want.
Thank you,
Lance

Nov 20 '05 #5

P: n/a
That's exactly what I was looking for. Thanks!

"Claes Bergefall" wrote:
Check out the OpacityConverter class
That's what the opacity property uses

/claes

"ljlevend" <lj******@discussions.microsoft.com> wrote in message
news:54**********************************@microsof t.com...
Is there a TypeConverter that converts Doubles to percent values in a

PropertyGrid? The Windows.Forms.Form.Opacity property seems to use the
TypeConverter that I want.

Thank you,
Lance


Nov 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.