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

  • Thread starter Thread starter Scott M.
  • Start date Start date
S

Scott M.

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?
 
Scott M. said:
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.
 
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#
 
David said:
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
 
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#
 
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.
 
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.
 
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.
 
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#
 
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.
 
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.
 
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.
 
Back
Top