Re: What does Val return?

Tech-Archive recommends: Fix windows errors by optimizing your registry



Gordon Bentley-Mix wrote:
The VBA help says the Val function "[r]eturns the numbers contained
in a string as a numeric value of appropriate type." Does this mean
that the data type of the returned value will automatically converted
to the data type of the variable that's calling the function?

For example, in the following contruction

Dim myValue as Long
Dim myString as String
myString = "123 Main Street"
myValue = Val(myString)

will Val return "123" but will it be returned as a Long or do I need
to use CLng to convert it? I know I can just be sloppy and let VBA
force the conversion, but I would prefer to be precise.

It's hard to tell because VBA does do implicit conversions during
assignments. The closest I can come to an answer is by changing the
declaration of myValue to a Variant and watching the Locals window while
single-stepping through the macro. It shows the data type to be
"Variant/Double", so I suspect that the initial return of the Val function
is a Double.

However, I'm not sure it's worth writing an explicit CLng conversion. The
interpreter might be smart enough to look at the variable being used on the
left of the equal sign, and call an override of the Val function that
already returns a Long. You'd have to peek at the proprietary interpreter
code to find out.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.


.



Relevant Pages

  • Re: Inserting string into SQL Server image column
    ... There actually is a reason why we need to use the image data type, ... conversion from string accomplished what I needed to do. ... >> because it can't convert a string to a byte array. ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Where are the CONVERT & CAST functions in MDB?
    ... but I cannot the SQL type-conversion functions that works in SQL Server ... Each function coerces an expression to a specific data type. ... CVar Variant Same range as Double for numerics. ... you can document your code using the data-type conversion ...
    (microsoft.public.access.modulesdaovba)
  • Re: Where are the CONVERT & CAST functions in MDB?
    ... I thought those functions were only for VBA code, not SQL. ... Each function coerces an expression to a specific data type. ... CVar Variant Same range as Double for numerics. ... you can document your code using the data-type conversion ...
    (microsoft.public.access.modulesdaovba)
  • Re: How to find the datatype of a variable?
    ... "upgrades" it to a long or a double. ... I'm aware of the type conversion issue you mentioned, ... Besides the Variant-explict data type examples the Mike provided (which I ... that trailing ampersand to force the numeric to be a Long is ...
    (microsoft.public.vb.general.discussion)
  • Re: Why is java considered a language for "web" or "internet" programming?
    ... you would be right in that writing a number cruncher in a dynamically typed language is not very smart or efficient (I would use C. ... I assume what you think is happening is that whenever a variable is used the interpreter has to run through a large if-then-else statement to decide the data type and that that is what will take time. ... A small perl internals tip, when a numerical variable is used in a string context and the value is converted to string, the converted string is cached in the variable object so as to not have to perform the conversion again later on. ...
    (comp.lang.java.help)