This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
vba:cast:cvalue [05.12.2014 13:47:07] yaslaw |
vba:cast:cvalue [19.10.2015 12:37:18] yaslaw |
||
---|---|---|---|
Line 1: | Line 1: | ||
<const> | <const> | ||
- | version=1.0.0 | + | version=1.3.0 |
- | vdate=05.12.2014 | + | vdate=19.10.2015 |
fname=cast_cvalue.bas | fname=cast_cvalue.bas | ||
ns=%NAMESPACE% | ns=%NAMESPACE% | ||
Line 9: | Line 9: | ||
====== [VBA] cValue(), cV() ====== | ====== [VBA] cValue(), cV() ====== | ||
- | ==Version %%version%% - %%vdate%%== | + | //cValue versucht einen String in ein Nativetyp zu casten.// |
- | cValue versucht einen String in ein Nativetyp zu casten. Die Funktion wird zum Beispiel in [[vba:cast:cdict]] verwendet. | + | ==Version %%version%% - %%vdate%%== |
+ | Die Funktion wird zum Beispiel in [[vba:cast:cdict]] verwendet. | ||
{{%%fname%%|Download %%fname%% (V-%%version%%)}} | {{%%fname%%|Download %%fname%% (V-%%version%%)}} | ||
Line 111: | Line 112: | ||
**iFLags** ist ein String, der das Verhalten cV() steuert. Die Flags sind3 Buchstaben de kombiniert werden können. | **iFLags** ist ein String, der das Verhalten cV() steuert. Die Flags sind3 Buchstaben de kombiniert werden können. | ||
- | ***s** Der Text Null ohne Delemiter wird als Wert Null intepretiert | + | ***s** Der Text Null ohne Delemiter wird als Wert Null intepretiert |
- | ***e** Ein leerer String wird als Null intepretiert | + | ***e** Ein leerer String wird als Null intepretiert |
- | ***b** Boolean-Text wird als Boolean intepretiert | + | ***b** Boolean-Text wird als Boolean intepretiert |
+ | ***d** Bei Delemited Strings den Delemiter entfernen. ' oder " gelten als Delemiter | ||
==== Beispiel ==== | ==== Beispiel ==== | ||
> Für die Ausgabe der Resultate verwendete ich die Funktion [[:vba:functions:print_r:]]. | > Für die Ausgabe der Resultate verwendete ich die Funktion [[:vba:functions:print_r:]]. | ||
Line 121: | Line 122: | ||
d cV("1234.5") | d cV("1234.5") | ||
<Date> 01.05.1234 | <Date> 01.05.1234 | ||
+ | |||
'Text Null | 'Text Null | ||
d cV("NULL") | d cV("NULL") | ||
<String> 'NULL' | <String> 'NULL' | ||
+ | |||
'Text Null intepretieren | 'Text Null intepretieren | ||
d cV("NULL", "n") | d cV("NULL", "n") | ||
<Null> | <Null> | ||
+ | |||
'Leerer String | 'Leerer String | ||
d cV("") | d cV("") | ||
<Empty> | <Empty> | ||
- | 'EMpty als Null zurückgeben | + | |
+ | 'Empty als Null zurückgeben | ||
d cV("", "e") | d cV("", "e") | ||
<Null> | <Null> | ||
+ | |||
'Boolean als Wert | 'Boolean als Wert | ||
d cV("TRUE") | d cV("TRUE") | ||
<String> 'TRUE' | <String> 'TRUE' | ||
+ | |||
'Boolean parsen | 'Boolean parsen | ||
d cV("TRUE", "b") | d cV("TRUE", "b") | ||
- | <Boolean> True</code> | + | <Boolean> True |
+ | |||
+ | 'Kombioniert. Nicht alle Flags kommen unbedigt zum tragen | ||
+ | ' Der String ist mit ' Als Deleimiter mitgeliefert | ||
+ | d cV("'NULL'", "nd") | ||
+ | <String> 'NULL' | ||
+ | </code> | ||
===== Code ===== | ===== Code ===== | ||
Line 147: | Line 160: | ||
<code vb>'/** | <code vb>'/** | ||
' * Dies ist die Minimalversion von cValue (V1.0.0): http://wiki.yaslaw.info/dokuwiki/doku.php/vba/cast/cvalue | ' * Dies ist die Minimalversion von cValue (V1.0.0): http://wiki.yaslaw.info/dokuwiki/doku.php/vba/cast/cvalue | ||
- | ' * Der 2te Paramtersteuert das Null-Verhalten('seb'): n: Der Text Null ohne Delemiter wird als Wert Null intepretiert, e: Ein leerer String wird als Null intepretiert, b: Boolean-Text wird als Boolean intepretiert | + | ' * Der 2te Paramtersteuert das Null-Verhalten('seb'): |
+ | ' * n: Der Text Null ohne Delemiter wird als Wert Null intepretiert, | ||
+ | ' * e: Ein leerer String wird als Null intepretiert, | ||
+ | ' * b: Boolean-Text wird als Boolean intepretiert | ||
+ | ' * d: Bei Delemited Strings den Delemiter entfernen. ' oder " gelten als Delemiter | ||
' */ | ' */ | ||
Public Function cV(ByVal iValue As Variant, Optional ByVal iFlags As String) As Variant | Public Function cV(ByVal iValue As Variant, Optional ByVal iFlags As String) As Variant | ||
Line 163: | Line 180: | ||
If rxDa Is Nothing Then Set rxDa = CreateObject("VBScript.RegExp"): rxDa.pattern = "^#(.*)#$" | If rxDa Is Nothing Then Set rxDa = CreateObject("VBScript.RegExp"): rxDa.pattern = "^#(.*)#$" | ||
If rxDa.Test(str) Then cV = CDate(rxDa.execute(str)(0).subMatches(0)): Exit Function | If rxDa.Test(str) Then cV = CDate(rxDa.execute(str)(0).subMatches(0)): Exit Function | ||
- | If rsDs Is Nothing Then Set rsDs = CreateObject("VBScript.RegExp"): rsDs.pattern = "^([""'])(.*)\1$" | + | If InStr(flg, "D") Then |
- | If rsDs.Test(str) Then Set sm = rsDs.execute(str)(0).subMatches: cV = Replace(sm(1), "\" & sm(0), sm(0)): Exit Function | + | If rsDs Is Nothing Then Set rsDs = CreateObject("VBScript.RegExp"): rsDs.pattern = "^([""'])(.*)\1$" |
+ | If rsDs.Test(str) Then Set sm = rsDs.execute(str)(0).subMatches: cV = Replace(sm(1), "\" & sm(0), sm(0)): Exit Function | ||
+ | End If | ||
cV = iValue | cV = iValue | ||
- | End Function</code> | + | End Function |
+ | </code> |