Newbie question: Cstr vs toString

J

John

Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code given
below). This function returns a value of type object so I need to convert it
to string. If I use the function as below (using ToString) it works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the bound
fields. This brings me to question; what is the difference between CStr and
tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String, ByVal
SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function
 
L

Lloyd Sheen

John said:
Hi

I have a WinForm app with a bound form. When user enters a value in field
rateid I lookup the respective rate amount from a table and assign it to
field rate.I am using the DLookup function to achieve this (full code
given below). This function returns a value of type object so I need to
convert it to string. If I use the function as below (using ToString) it
works fine;

Me.txtRate.Text = DLookup(<parameters here>).ToString

but if I use it with a CStr

Me.txtRate.Text = CStr(DLookup(<parameters here>))

and then browse through 2-3 record the form stops showing data in the
bound fields. This brings me to question; what is the difference between
CStr and tostring that could be causing the above problem?

Thanks

Regards

=======

Function DLookup(ByVal SearchFld As String, ByVal SearchTbl As String,
ByVal SearchCriteria As String) As Object
Dim Cmd As OleDb.OleDbCommand
Dim Reader As OleDb.OleDbDataReader

Cmd = New OleDb.OleDbCommand("SELECT " & SearchFld & " FROM " & SearchTbl
& " WHERE " & SearchCriteria, DBConnection())
Reader = Cmd.ExecuteReader()

If (Reader.Read()) Then
DLookup = Reader.GetValue(0)
Else
DLookup = DBNull.Value
End If

Reader.Close()
Reader = Nothing
Cmd = Nothing
End Function

The main difference is that cstr is a function which is part of the VB
language which depending on the overload will convert an object to a string
while .tostring is a function which any object can override with their own
implementation. The Object implementation will simply return the object
type name so it will never fail while using cstr on objects which cannot be
converted to a string easily will fail.

LS
 
O

\(O\)enone

Lloyd said:
The Object implementation will simply
return the object type name so it will never fail while using cstr on
objects which cannot be converted to a string easily will fail.

....The exception to that being when the object is Nothing, at which point
object.ToString fails with a Null Reference Exception, whereas CStr(object)
simply returns Nothing.
 
C

Cor Ligthert[MVP]

John,

To make the confusion even more complicated for you.

ToString is one of the standard members from Object where all classes derive
from in Net. It is an overridable method.

http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx

In the Object.Values (that is where all Values derive from including a
struct) is this method overriden to convert a Value to a string.

Cstr is a function from VB as it is in the Cxx group of functions, which
convert values to other values. The String is a special reference type
threathen as Value, probably that is the reason why the Cstr exist.

In VB I use whenever I need to convert forever the Cxx functions, however
never the Cstr as it is so common in dotNet to use the overridden base
functions like ToString.

Cor
 
J

Joergen Bech

While the VB compiler may recognize the CStr command,
it is eventually comiled to a call to something in
Microsoft.VisualBasic.CompilerServices.Conversions -
and quite often (but not always) the end result comes from a call
to the object's ToString method.

Regards,

Joergen Bech
 
S

SurturZ

I agree with Patrice I think the problem is occuring whenever you trigger the
DBNull return value. I'm surprised it even works, I get the following error:
"Conversion from type 'DBNull' to type 'String' is not valid."

Changing the line

DLookup = DBNull.Value

to

DLookup = ""

might work, but it might also break code elsewhere that tests for the DBNull
return.

You can get away with .ToString in most cases, but sometimes .ToString will
return a description of the object, rather than the contents of the object
converted to a String. CStr (or CType) is usually more reliable, *IF* you are
sure of your input data type.
 

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