User Tools

Site Tools


vba:cast:cval

Table of Contents

[VBA] cVal()

cVal() versucht einen String in ein Nativetyp zu casten. Ähnlich wie cValue() aber generischer

Version 1.0.0 - 18.08.2017

Download cast_cval.bas (V-1.0.0)

Siehe auch [VBA] cValue(), cV(). Im Gegensatz dui cValue() sind hier die verschiedenen Datentypen nicht hinterlegt. Es gibt zB also auch eine kleine Zahl als Long aus.

Beispiele

Für die Ausgabe der Resultate verwendete ich die Funktion [VBA] print_r().

Einfach einige Beispiele

d cVal("1234")
<Long> 1234
 
d cVal("Hallo")
<String> 'Hallo'
 
d cVal("1")
<Long> 1
 
d cVal("Null")
<Null> 
 
d cVal("True")
<Integer> -1
 
d cVal("False")
<Integer> 0
 
d cVal(new FileSystemObject)
<FileSystemObject> 
 
d cVal(Nothing)
<Nothing> 
 
d cVal("Nothing")
<String> 'Nothing'

Code

cast_cval.bas
Attribute VB_Name = "cast_cVal"
'-------------------------------------------------------------------------------
'File         : cast_cVal.bas
'               Copyright mpl by ERB software
'               All rights reserved
'               http://wiki.yaslaw.info/dokuwiki/doku.php/vba/cast/cval
'Environment  : VBA 2010 +
'Version      : 1.0.0
'Name         : cVal
'Author       : Stefan Erb (ERS)
'History      : 18.08.2017 - ERS - Creation
'-------------------------------------------------------------------------------
Option Explicit
 
'/**
' * Es wird versucht, den Paramter in seine eigentlichen Typ zu wandeln. Gar mit Textausgaben oder parsen von SQL interessant
' * @example    cVal("Null") -> Null, cVal("True") -> -1
' * @param  Variant
' * @return Variant
' */
Private Function cVal(ByVal iValue As Variant) As Variant
On Error Resume Next
    Set cVal = iValue: cVal = iValue: cVal = eval(iValue)
End Function
 
vba/cast/cval.txt · Last modified: 18.08.2017 11:31:09 by yaslaw