Quick Ctype Question

M

Miro

Hi,

During one of my books chapters there is an event created in the form load
with:

AddHandler tbName.DataBindings("Text").Format, AddressOf FormatName

and then the sub underneith it is as follows:

Private Sub FormatName(ByVal sender As Object, ByVal e As
ConvertEventArgs)

If tbName.Tag <> "PARSE" Then
e.Value = CType(e.Value, String).ToUpper() 'My Question is with
this line right here
End If
tbName.Tag = "FORMAT"
End Sub


===
Basically its showing how to use the format of the binding to upper the name
of the database.
I was looking at the e.value = ctype line and replaced it with this:

e.Value = e.Value.ToString.ToUpper

I'm just wondering if there really is a difference between my line of code
vs using the CType function.
I'm assuming both lines would always give out the same results and they
really are identical.

Thanks,

Miro
 
C

Cor Ligthert[MVP]

Miro,

The ToString method is a method of object, created there to be overloaded in
every class that derives from object, (and that is every class).

It can return everything however in the case of value mostly something as
CType(x,String) or any more efficient method.

Therefore you can mostly use as best ToString, because that is then the most
optimalized methoed.

Cor
 
J

Jack Jackson

Hi,

During one of my books chapters there is an event created in the form load
with:

AddHandler tbName.DataBindings("Text").Format, AddressOf FormatName

and then the sub underneith it is as follows:

Private Sub FormatName(ByVal sender As Object, ByVal e As
ConvertEventArgs)

If tbName.Tag <> "PARSE" Then
e.Value = CType(e.Value, String).ToUpper() 'My Question is with
this line right here
End If
tbName.Tag = "FORMAT"
End Sub


===
Basically its showing how to use the format of the binding to upper the name
of the database.
I was looking at the e.value = ctype line and replaced it with this:

e.Value = e.Value.ToString.ToUpper

I'm just wondering if there really is a difference between my line of code
vs using the CType function.
I'm assuming both lines would always give out the same results and they
really are identical.

In general ToString() and CType() are not the same, although in some
cases like this one they will return the same value.

Every object has a ToString() method. For a String object, ToString
will just return the string value of the object. For numerical
objects like Integer, Decimal, etc., ToString will convert the numeric
value to a string. For objects that have no intrisic value that can
be represented by a string (like a DataTable), ToString might return
the name of the object, or the class of the object, or whatever the
designer of the class chooses.

CType(obj, String) will try to convert the value to a String. This
will fail (raise an exception) for any object that does not have a
known conversion to string.

In this case, since you know that e.Value is a string, either will
work. It would be faster to use DirectCast() instead of CType(), but
I don't know about the performance of that relative to ToString().
 
M

Miro

Thank You,

Miro


Jack Jackson said:
In general ToString() and CType() are not the same, although in some
cases like this one they will return the same value.

Every object has a ToString() method. For a String object, ToString
will just return the string value of the object. For numerical
objects like Integer, Decimal, etc., ToString will convert the numeric
value to a string. For objects that have no intrisic value that can
be represented by a string (like a DataTable), ToString might return
the name of the object, or the class of the object, or whatever the
designer of the class chooses.

CType(obj, String) will try to convert the value to a String. This
will fail (raise an exception) for any object that does not have a
known conversion to string.

In this case, since you know that e.Value is a string, either will
work. It would be faster to use DirectCast() instead of CType(), but
I don't know about the performance of that relative to ToString().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top