Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2005

Tip: Looking for answers? Try searching our database.

ToString.....or not ToString (that is the question)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Scott M. - 27 Aug 2005 18:21 GMT
Although the object class has a ToString method, not all classes show this
method in their IntelliSense list of properties and methods.  Why?

Also, in VB .NET, with Option Strict turned on, you can sometimes get away
with assigning a non-string type to a string, without having to perform a
cast or ToString on the value being used.  Why?
Peter van der Goes - 28 Aug 2005 00:24 GMT
> Although the object class has a ToString method, not all classes show this
> method in their IntelliSense list of properties and methods.  Why?
>
> Also, in VB .NET, with Option Strict turned on, you can sometimes get away
> with assigning a non-string type to a string, without having to perform a
> cast or ToString on the value being used.  Why?

Please provide a specific example where implicit conversion succeeded with
Option Strict on.

Signature

Peter [MVP Visual Developer]
Jack of all trades, master of none.

David Anton - 28 Aug 2005 07:24 GMT
One implicit conversion that compiles with Option Strict On is Char to String.
e.g.,
Dim thisString As String = "a"c
(the same isn't true in C# - char's never implicitly convert to strings)

I was sure there was another case or two, but I can't recall right now.

Option Strict On is stricter than C# in some ways, but not as strict as C#
in other ways.

Signature

David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#

> > Although the object class has a ToString method, not all classes show this
> > method in their IntelliSense list of properties and methods.  Why?
[quoted text clipped - 5 lines]
> Please provide a specific example where implicit conversion succeeded with
> Option Strict on.
Chris - 28 Aug 2005 08:07 GMT
> One implicit conversion that compiles with Option Strict On is Char to String.
> e.g.,
[quoted text clipped - 5 lines]
> Option Strict On is stricter than C# in some ways, but not as strict as C#
> in other ways.

A char can be converted to a string w/o losing any precision.  Just like
you can convert an integer to a double w/o the possibility of losing any
data the same holds true for a char to a string.  It will allow you do
the the implicit conversion anytime you don't have the chance of losing
any data.

Chris
David Anton - 28 Aug 2005 15:16 GMT
Exactly - this was just an example of an implicit conversion that is allowed
in VB with Option Strict On (the second poster asked for an example).

It is strange that it wouldn't be allowed in C# however (while C# does allow
other implicit conversions though).

Signature

David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#

> > One implicit conversion that compiles with Option Strict On is Char to String.
> > e.g.,
[quoted text clipped - 13 lines]
>
> Chris
Scott M. - 28 Aug 2005 15:22 GMT
See my other replies.  This example has taken us away from the original
questions.

> Exactly - this was just an example of an implicit conversion that is
> allowed
[quoted text clipped - 24 lines]
>>
>> Chris
Scott M. - 28 Aug 2005 15:21 GMT
Not true.  As stated in my first reply, what Dave showed is not an example
of implicit conversion.  The "c" character is tells the CLR what data type
to use on the string in the first place (in this case: char).  Because of
that, the issue of casting becomes irrelevant because "a" is assigned as a
char in the first place.

>> One implicit conversion that compiles with Option Strict On is Char to
>> String.
[quoted text clipped - 14 lines]
>
> Chris
Scott M. - 28 Aug 2005 15:19 GMT
That's not an implicit conversion.  That's using a type literal to specify
the type to use in the first place, so that no conversion (cast) is needed
later.

> One implicit conversion that compiles with Option Strict On is Char to
> String.
[quoted text clipped - 20 lines]
>> with
>> Option Strict on.
David Anton - 29 Aug 2005 00:09 GMT
Not quite - "a"c is a character, not a string.  So to assign this to a string
without explicit casting is indeed an implicit cast.  The fact that there's
no doubt about the type of the right hand side of an assignment has nothing
to do with whether an implicit cast is taking place.
e.g.,
Dim c As Char = "a"c   'no casting
Dim s As String = "a"  'no casting
Dim s As String = CStr("a"c)  'explicit cast
Dim s As String = "a"c 'implicit cast
Signature

David Anton
www.tangiblesoftwaresolutions.com
Home of:
Clear VB: Cleans up outdated VB.NET code
Instant C#: Converts from VB.NET to C#
Instant VB: Converts from C# to VB.NET
Instant J#: Converts from VB.NET to J#

> That's not an implicit conversion.  That's using a type literal to specify
> the type to use in the first place, so that no conversion (cast) is needed
[quoted text clipped - 24 lines]
> >> with
> >> Option Strict on.
Scott M. - 29 Aug 2005 01:36 GMT
You are missing the point.  I'm not referring to the left hand side of the
expression.  I was talking about the type literal not being a cast.

In any case, this is not a scenario that I am asking about (strings and
chars) in the first place.

> Not quite - "a"c is a character, not a string.  So to assign this to a
> string
[quoted text clipped - 44 lines]
>> >> with
>> >> Option Strict on.
Scott M. - 29 Aug 2005 01:38 GMT
To follow up, whether or not precision is lost is not a factor when
considering if Option Strict is going to consider something an explicit or
implicit cast.

An Integer can be cast as a long without losing precision, but with Option
Strict on, you still need to explicitly cast that Integer as a Long if you
wish to use it as a long.

> Not quite - "a"c is a character, not a string.  So to assign this to a
> string
[quoted text clipped - 44 lines]
>> >> with
>> >> Option Strict on.
Scott M. - 29 Aug 2005 01:49 GMT
Ok, I just tried the Integer and Long scenario and, I'll admit, I got that
one wrong.

What I'm really trying to understand is why a DataSet would allow a string
value passed into a Row Item (which is of the object type) with Option
Strict On, but other data types wouldn't work and why ToString does not
appear on the member list of every class, since ToString is a member of the
Object class.

> To follow up, whether or not precision is lost is not a factor when
> considering if Option Strict is going to consider something an explicit or
[quoted text clipped - 53 lines]
>>> >> with
>>> >> Option Strict on.
Scott M. - 28 Aug 2005 15:30 GMT
> Please provide a specific example where implicit conversion succeeded with
> Option Strict on.

In a Console Application, you can do this (with Option Strict On):

Dim x As Integer = 10
Console.WriteLine("The value is" & x)

...here's another example (with Option Strict On) that works:

Assume we have a loosely-typed DataSet (ds) with a DataTable that has a
Column (RetailPrice) that is set to hold System.Double data:

ds.Tables(0).Rows(0).Item("RetailPrice") =  txtPrice.Text
NOTE:  While you may look at this and say that at design-time, no error is
seen because the DataSet doesn't know until run-time that the data is bad.
Fair enough, but when I run this code it does not throw any exceptions.  The
text data is converted to Double data and placed in the DataSet.

ALSO:  No one has addressed my first question of why, although Objects have
a ToString method, not ALL classes have it, however you can still use
ToString (with Option Strict turned on) on classes that don't show it in
their IntelliSense list of members.

Thanks.
Nigel Norris - 29 Aug 2005 13:29 GMT
Inline..

----
Nigel Norris

>> Please provide a specific example where implicit conversion succeeded
>> with Option Strict on.
[quoted text clipped - 14 lines]
> Fair enough, but when I run this code it does not throw any exceptions.
> The text data is converted to Double data and placed in the DataSet.

This is nothing to do with casting - the assignment is object = string, so
no cast is required. The conversion that is going on is an ADO.NET feature.
When storing your assigned value (an object) to a column of type double it
calls Convert.ToDouble(yourvalue). I'm not sure where (or even if) this is
documented, but that's what it does.

> ALSO:  No one has addressed my first question of why, although Objects
> have a ToString method, not ALL classes have it, however you can still use
> ToString (with Option Strict turned on) on classes that don't show it in
> their IntelliSense list of members.

Can you provide a specific example? IntelliSense always seems to list
ToString on any class I give it (as it should).
Scott M. - 29 Aug 2005 16:03 GMT
Inline:

> This is nothing to do with casting - the assignment is object = string, so
> no cast is required. The conversion that is going on is an ADO.NET
> feature. When storing your assigned value (an object) to a column of type
> double it calls Convert.ToDouble(yourvalue). I'm not sure where (or even
> if) this is documented, but that's what it does.

That sounds a lot like an implicit cast, which shouldn't happen with Option
Strict turned on (hence my question).  Also, why doesn't is do something
similar if I try to pass a short (rather than a string) in the same
circumstance?

>> ALSO:  No one has addressed my first question of why, although Objects
>> have a ToString method, not ALL classes have it, however you can still
[quoted text clipped - 3 lines]
> Can you provide a specific example? IntelliSense always seems to list
> ToString on any class I give it (as it should).

Sure:
Dim sb As New StringBuilder
sb.Append("<Product>" & CType(dr("Name"), String) & "</Product>")

If you type dr("Name") and then a dot (.), you will NOT see ToString listed
(although you can still type it and it will work).  This scenario also
happens in several other situations as well.
Damien - 30 Aug 2005 08:45 GMT
[snip]

> Sure:
> Dim sb As New StringBuilder
[quoted text clipped - 3 lines]
> (although you can still type it and it will work).  This scenario also
> happens in several other situations as well.

There is an option within the Text Editor/Basic options to disable
listing of "Advanced Members". If you turn that off, you should then
see the ToString method (assuming dr is a DataReader - you didn't give
us the right Dim). What I cannot seem to find is how to define your own
members as advanced - does anyone know of the attribute to use?

Damien
Nick Hall - 30 Aug 2005 12:02 GMT
Inline

> [snip]
>>
[quoted text clipped - 9 lines]
> There is an option within the Text Editor/Basic options to disable
> listing of "Advanced Members".

You're quite right, though it seems to me a slightly strange choice to make
that particular method "Advanced" - doesn't seem to quite fit the bill to
me.

>If you turn that off, you should then
> see the ToString method (assuming dr is a DataReader - you didn't give
> us the right Dim). What I cannot seem to find is how to define your own
> members as advanced - does anyone know of the attribute to use?
>
> Damien

I believe the attribute is System.ComponentModel.EditBrowsableAttribute with
a state of Advanced.

Regards,

Nick Hall
Damien - 30 Aug 2005 15:26 GMT
> Inline
> >
[quoted text clipped - 15 lines]
> that particular method "Advanced" - doesn't seem to quite fit the bill to
> me.

I think the "Advanced" is more of a "don't clutter up Intellisense for
things that they're unlikely to want to do" setting - and in this case,
I think that there are other more likely things to want to do with an
object. Whereas with, for instance, an Integer, it's a fairly common
thing with Integers to want to display them in text output, so it's
left as normal.

> >If you turn that off, you should then
> > see the ToString method (assuming dr is a DataReader - you didn't give
[quoted text clipped - 5 lines]
> I believe the attribute is System.ComponentModel.EditBrowsableAttribute with
> a state of Advanced.

Thanks Nick. Not sure I've got any use for it yet, but it always helps
in the future when someone asks me to do something and it tickles back
to the back of the brain :-)

Damien
Scott M. - 30 Aug 2005 14:21 GMT
Thanks Damien.  I agree that this doesn't seem like it would be an advanced
member (especially since even with that turned on you do see ToString in
many other situations).

> [snip]
>>
[quoted text clipped - 14 lines]
>
> Damien
Nigel Norris - 30 Aug 2005 15:22 GMT
>> This is nothing to do with casting - the assignment is object = string,
>> so no cast is required. The conversion that is going on is an ADO.NET
[quoted text clipped - 4 lines]
> That sounds a lot like an implicit cast, which shouldn't happen with
> Option Strict turned on (hence my question).

OK - we have to start getting precise about terminology. Since I'm a C#
user, what I say may need some VB adjustment.

A cast is a request to perform a conversion. There are implicit conversions
and explicit conversions. Implicit conversions are all information
preserving. Explicit conversions in general may lose information or fail.

The compiler will  perform implicit conversions automatically. In C# you
need to use a cast to request an explicit conversion. In VB, as I understand
it, if Option Strict is turned off, the compiler will provide explicit
conversions automatically as well. With Option Strict turned on, it behaves
like C#.

An assignment of the form object = string is an implicit conversion (all
reference types can be converted to object). Or, in another form, object is
a supertype of string and there is always an implict conversion to a
supertype.

So, you can assign a string value to a double column in a row quite happily
as far as the compiler is concerned, irrespective of the option setting. At
runtime ADO.NET calls the conversion on the passed in (string) object.

> Also, why doesn't is do something similar if I try to pass a short (rather
> than a string) in the same circumstance?

It does. Assigning a short to an object variable is an example of a boxing
conversion, which is another implicit conversion. So setting the column
value passes in a boxed short value. ADO.NET will then apply the appropriate
conversion to a double. This works OK for me (in VB!).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.