Re: Quick Ctype Question



Thank You,

Miro


"Jack Jackson" <jjackson@xxxxxxxxxxxxxxxx> wrote in message news:3e7sq3hope1tku1fvlpduti2agrm3gh5i2@xxxxxxxxxx
On Sat, 9 Feb 2008 14:35:56 -0500, "Miro" <miro@xxxxxxxxx> wrote:

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().

.



Relevant Pages

  • Re: Quick Ctype Question
    ... Basically its showing how to use the format of the binding to upper the name ... vs using the CType function. ... Every object has a ToString() method. ... For a String object, ToString ...
    (microsoft.public.dotnet.languages.vb)
  • Re: String.Format vs + operator
    ... With Format, the format ... string is parsed, and that is going to require some overhead. ... versus the readability/maintainability of the code, ... ToString() will automatically be called on other parts as well. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Guid.NewGuid - character
    ... I assume you mean if you call ToString() on the generated Guid. ... The format of the string returned by guid.ToStringis xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. ... Browse into any of the overloads that take in a format string to see the different formats that could be returned. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: String.Format vs + operator
    ... With Format, the format ... string is parsed, and that is going to require some overhead. ... versus the readability/maintainability of the code, as I think that the call ... If any of the parts of a string concatenation contains a string literal, ToString() will automatically be called on other parts as well. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Date format detection
    ... Specifies the locale for which the date string is to be formatted. ... date format for this locale. ... the system default-date format for the specified locale. ... be enclosed within single quotation marks in the date format picture. ...
    (borland.public.delphi.thirdpartytools.general)

Loading