Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CStr? 1

Status
Not open for further replies.

Ovatvvon

Programmer
Feb 1, 2001
1,514
0
0
US
Does anyone know what the CStr is used for. In what circumstances you would want to use it, or what it accomplishes?
-Ovatvvon :-Q
 
It converts the argument into a string.
Often useful for making sure you're comparing two strings, or to make sure you're passing a string into a Parameter object that expects one. <insert witticism here>
codestorm
 
Could you give a code example of when you'd use it...or how you'd use it? -Ovatvvon :-Q
 
(copied from VBScript Help file)
Dim MyDouble, MyString
MyDouble = 437.324 ' MyDouble is a Double.
MyString = CStr(MyDouble) ' MyString contains &quot;437.324&quot;.


<insert witticism here>
codestorm
 
So why would you want to do that? It would work just the same by coding:

Dim MyDouble, MyString
MyDouble = 437.324
MyString = MyDouble


wouldn't it? Why would you want to use CStr?

-Ovatvvon :-Q
 
for example, you can use it when you want to merge two integer insteads of add two integer.


a = 10
b = 20

c = a + b ' the result will be '30'
d = cstr(a) + cstr(b) ' the result will be '1020'

hope this helpful ^^
H2
 
Seams like it would be less typeing to just put:

a = 10
b = 20

c = a & b


What makes CStr more desireable or more efficient? I'm not trying to sound annoying or anything...I just see it as more logical to do the same task with less typing. So I figure I'm not understanding somthing and just want to know what it is that makes the CStr so much better!??

Thanks... -Ovatvvon :-Q
 
Hmmm.. did a little test using CStr and was surprised with results. Had thought perhaps it would be quicker to use CStr to concatenate a string and a number, rather than just letting VBS figure it out. But nope, it actually slows it down. Go figure. <insert witticism here>
codestorm
 
I personaly use STR instead of CStr cuz CStr did some problems to me... so i use STR...
if i try to pass an string to CSTR it makes an error...
but when i use STR instead no error... ________
George, M
 
It's all about subtypes. In vbscript, everything is a subtype of the 'variant' datatype. So this:

Dim MyDouble, MyString
MyDouble = 437.324
MyString = MyDouble

would subtype 'myString' to be of subtype, double, since you have assigned it a numeric value. Notice no quotation marks. So even though you have named the variable 'myString', it's not a string at all. It's a number.

The single best explanation I saw here was from codestorm:

Often useful for making sure you're comparing two strings, or to make sure you're passing a string into a Parameter object that expects one.

If you have a stored procedure that expects a string value:

@someString varchar(20)

and you send it this:

dim someNum
someNum = 6.54
commandObject.parameters(&quot;@someString&quot;).value = someNum

you will get a runtime error every time.

On the other hand, if you send it:

commandObject.parameters(&quot;@someString&quot;).value = cstr(someNum)

you will receive no such error.

Your observation:

c = a & b

resulting in string concatenation is, in fact, true. But that has to do with your use of the concatenation operator, and so vbScript is 'smart' enough to know what you are trying to do. However, it's going to result in a slower operation than doing this:

dim a, b, c
a = &quot;a&quot;
b = &quot;b&quot;
c = a & b

Because the concatenation operator is native to the string subtype, it will go faster, since there's no implicit call to cstr, which there is in your example.

Because of these types of nuances and such, it's hard to understand why you would want to cast (cint, cstr, clng, etc..) variables to their respective types. But if you don't you lose performance, and when you're on the web, performance is EVERYTHING. The more you let vbScript do for you (rather than doing something for vbScript), the slower your application is going to be.

ASP.Net is going to enforce this even further through the available use of C# in your web applications. Your applications will have the ability to run much much faster through the use of managed code. C# (the next generation of C languages) doesn't make it so easy on you when it comes to data types. It forces you to use them correctly, where VBScript does not.

hope that clears it up a little. :)
Paul Prewett
penny.gif
penny.gif
 
Thank you for that detailed response Paul!
Clears it up all the way. :-D
-Ovatvvon :-Q
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top