Some .ToString questions

emaduddeen

Well-known member
Joined
May 5, 2010
Messages
171
Location
Lowell, MA & Occasionally Indonesia
Programming Experience
Beginner
Hi Everyone,

I have some .ToString questions.

I noticed that when I try to use this I don't get any "null" errors when processing values from a database.

VB.NET:
strMyPhone = objParentsDataReader("Emergency Phone")).ToString

Could you explain why using or not using .ToString make the difference?

Also is it always good to include .ToString and does it make the program execute slower?

Maybe there is a document that tells use the best time to use .ToString?

Thanks.

Truly,
Emad
 
DataReader.Item property return type is Object and may return various types of values. This may or may not be a string value, but if it is you can use ToString to "convert" it to String type. Intellisense error correction will suggest that you use CStr(theObject) when assigning anything not a String type to a String variable. This is also something that may be better if the object value doesn't refer to anything, ie Nothing, since ToString is an instance method and if at runtime (Nothing).ToString happens you get a NullReferenceException. Other options would be Ctype or DirectCast. In some cases you could have a numeric value, and you'll see that ToString have several overloads that allows arguments to control the format of the conversion to String, for example converting a Single to String with two decimals, or converting a Date value to String with a given display format.
 
Back
Top