PC Review


Reply
Thread Tools Rate Thread

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

 
 
Scott M.
Guest
Posts: n/a
 
      27th Aug 2005
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?


 
Reply With Quote
 
 
 
 
Peter van der Goes
Guest
Posts: n/a
 
      28th Aug 2005

"Scott M." <s-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      28th Aug 2005
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.

--
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#


"Peter van der Goes" wrote:

>
> "Scott M." <s-(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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.
>
> --
> Peter [MVP Visual Developer]
> Jack of all trades, master of none.
>
>
>

 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      28th Aug 2005
David Anton wrote:
> 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.
>


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
 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      28th Aug 2005
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).

--
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#


"Chris" wrote:

> David Anton wrote:
> > 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.
> >

>
> 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
>

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      28th Aug 2005
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.


"David Anton" <(E-Mail Removed)> wrote in message
news:5A7F829C-DD32-41BB-B3CB-(E-Mail Removed)...
> 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.
>
> --
> 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#
>
>
> "Peter van der Goes" wrote:
>
>>
>> "Scott M." <s-(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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.
>>
>> --
>> Peter [MVP Visual Developer]
>> Jack of all trades, master of none.
>>
>>
>>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      28th Aug 2005
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.


"Chris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> David Anton wrote:
>> 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.
>>

>
> 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



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      28th Aug 2005
See my other replies. This example has taken us away from the original
questions.


"David Anton" <(E-Mail Removed)> wrote in message
news:6C57C6A0-A875-46CE-AA17-(E-Mail Removed)...
> 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).
>
> --
> 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#
>
>
> "Chris" wrote:
>
>> David Anton wrote:
>> > 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.
>> >

>>
>> 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
>>



 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      28th Aug 2005
> 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.


 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      29th Aug 2005
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
--
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#


"Scott M." wrote:

> 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.
>
>
> "David Anton" <(E-Mail Removed)> wrote in message
> news:5A7F829C-DD32-41BB-B3CB-(E-Mail Removed)...
> > 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.
> >
> > --
> > 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#
> >
> >
> > "Peter van der Goes" wrote:
> >
> >>
> >> "Scott M." <s-(E-Mail Removed)> wrote in message
> >> news:(E-Mail Removed)...
> >> > 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.
> >>
> >> --
> >> Peter [MVP Visual Developer]
> >> Jack of all trades, master of none.
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When "ToString" does not equal "ToString" Ayende Rahien Microsoft Dot NET 7 8th Oct 2004 01:57 PM
When "ToString" does not equal "ToString" Ayende Rahien Microsoft Dot NET Framework 6 8th Oct 2004 07:41 AM
Re: When "ToString" does not equal "ToString" John Smith Microsoft Dot NET 0 7th Oct 2004 05:51 PM
Re: When "ToString" does not equal "ToString" John Smith Microsoft Dot NET Framework 0 7th Oct 2004 05:51 PM
Re: When "ToString" does not equal "ToString" John Smith Microsoft C# .NET 0 7th Oct 2004 05:51 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:50 PM.