|
Dinamic VB Expressons
|
Global functions can be reached from any element of flat file schema. Here is the list of standard VB functions that you can use:
Public ReadOnly Pi As Double = Math.PI
Public Function Sin(ByVal value As Double) As Double
Return Math.Sin(value)
End Function
Public Function Cos(ByVal value As Double) As Double
Return Math.Cos(value)
End Function
Public Function Exp(ByVal base As Double, ByVal pexp As Double) As Double
Return Math.Pow(base, pexp)
End Function
Public Function Rnd() As Double
Microsoft.VisualBasic.Randomize()
Return Microsoft.VisualBasic.Rnd
End Function
Public Function [Mod](ByVal x As Double, ByVal y As Double) As Double
Return x Mod y
End Function
Public Function Abs(ByVal val As Double) As Double
If val < 0 Then
Return -val
Else
Return val
End If
End Function
Public Function Sqrt(ByVal v As Double) As Double
Return Math.Sqrt(v)
End Function
Public Function Power(ByVal v As Double, ByVal e As Double) As Double
Return Math.Pow(v, e)
End Function
Public Function Trunc(ByVal value As Double, Optional ByVal prec As Integer = 0) As Integer
value -= 0.5 / Math.Pow(10, prec)
Return CInt(Math.Round(value, prec))
End Function
Public Function Round(ByVal value As Object) As Double
Return Math.Round(CDbl(value))
End Function
Min(double,...), Max(double,...)
Public Function Min(ByVal v1 As Double, ByVal v2 As Double, _
Optional ByVal v3 As Double = Double.MaxValue, _
Optional ByVal v4 As Double = Double.MaxValue, _
Optional ByVal v5 As Double = Double.MaxValue) As Double
Dim res As Double = v1
If v2 < res Then res = v2
If v3 < res Then res = v3
If v4 < res Then res = v4
If v5 < res Then res = v5
Return res
End Function
Public Function Max(ByVal v1 As Double, ByVal v2 As Double, _
Optional ByVal v3 As Double = Double.MinValue, _
Optional ByVal v4 As Double = Double.MinValue, _
Optional ByVal v5 As Double = Double.MinValue) As Double
Dim res As Double = v1
If v2 > res Then res = v2
If v3 > res Then res = v3
If v4 > res Then res = v4
If v5 > res Then res = v5
Return res
End Function
Private Shared daylist() As String = _
{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
MonthList() - Is zero based string array of months.
Private Shared monthlist() As String = _
{"January", "February", "March", "April", "May", "June", _
"July", "August", "September", "October", "November", "December"}
Now(), Today() - Get current time and date.
Public Function Now() As DateTime
Return Microsoft.VisualBasic.Now
End Function
Public Function Today() As DateTime
Return Microsoft.VisualBasic.Today
End Function
Date(integer, integer, integer) - Convert from numbers to date.
Public Function [Date](ByVal year As Integer, ByVal month As Integer, _
ByVal day As Integer) As DateTime
Return New Date(year, month, day)
End Function
Public Function Year(ByVal d As DateTime) As Integer
Return d.Year
End Function
Public Function Month(ByVal d As DateTime) As Integer
Return d.Month
End Function
Public Function Day(ByVal d As DateTime) As Integer
Return d.Day
End Function
Public Function Weekday(ByVal d As DateTime) As Integer
Select Case d.DayOfWeek
Case DayOfWeek.Sunday
Return 1
Case DayOfWeek.Monday
Return 2
Case DayOfWeek.Tuesday
Return 3
Case DayOfWeek.Wednesday
Return 4
Case DayOfWeek.Thursday
Return 5
Case DayOfWeek.Friday
Return 6
Case Else '' Saturday
Return 7
End Select
End Function
Public Function FormatDate(ByVal value As DateTime, ByVal format As String) As String
Dim res As String = vbNullString
Dim i As Integer
Dim code As Char
Dim word As String
If String.IsNullOrEmpty(format) Then format = "d m y"
Dim dd As Integer = value.Day
For i = 0 To format.Length - 1
word = ""
code = format.Chars(i)
Select Case code
Case "w"c
word = daylist(value.DayOfWeek())
Case "x"c
word = Substr(daylist(value.DayOfWeek()), 1, 3)
Case "d"c
Dim suff As String
If dd >= 11 And dd <= 13 Then
suff = "th"
ElseIf (dd Mod 10) = 1 Then
suff = "st"
ElseIf (dd Mod 10) = 2 Then
suff = "nd"
ElseIf (dd Mod 10) = 3 Then
suff = "rd"
Else
suff = "th"
End If
word = dd.ToString &suff
Case "e"c
word = dd.ToString()
Case "f"c
word = PadLeft(dd.ToString(), 2, "0")
Case "m"c
word = monthlist(value.Month() - 1)
Case "n"c
word = value.Month().ToString
Case "o"c
word = PadLeft(value.Month().ToString, 2, "0")
Case "p"c
word = Substr(monthlist(value.Month() - 1), 1, 3)
Case "y"c
word = value.Year().ToString
Case "z"c
word = PadLeft((value.Year() Mod 100).ToString, 2, "0")
Case Else
word = code
End Select
res &= word
Next
Return res
End Function
Public Function Trim(ByVal strValue As String) As String
Return Microsoft.VisualBasic.Trim(strValue)
End Function
Public Function LeftTrim(ByVal strValue As String) As String
Return Microsoft.VisualBasic.LTrim(strValue)
End Function
Public Function RightTrim(ByVal strValue As String) As String
Return Microsoft.VisualBasic.RTrim(strValue)
End Function
Format(string), Lower(string), Upper(string), WCase(string) - Format string functions.
Public Function Format(ByVal value As Object, ByVal style As String) As String
Return Microsoft.VisualBasic.Format(value, style)
End Function
Public Function Lower(ByVal value As String) As String
Return Microsoft.VisualBasic.LCase(value)
End Function
Public Function Upper(ByVal value As String) As String
Return Microsoft.VisualBasic.UCase(value)
End Function
Public Function WCase(ByVal value As String) As String
If Len(value) = 0 Then Return ""
Return Microsoft.VisualBasic.UCase(value.Substring(0, 1)) &_
Microsoft.VisualBasic.LCase(value.Substring(1))
End Function
Replace(string,string,string), SubStr(string,intger,integer),Mid(string,intger,integer), Len(str) - Operations with strings.
Function Replace(ByVal base As String, ByVal search As String, ByVal repl As String) As String
Return base.Replace(search, repl)
End Function
' Zero based index
Public Function Substr(ByVal s As String, ByVal from As Integer, _
Optional ByVal len As Integer = Integer.MaxValue) As String
If s Is Nothing Then Return String.Empty
from -= 1
If from < 1 Then from = 0
If from >= s.Length Then from = s.Length
If from + len > s.Length Then len = s.Length - from
Return s.Substring(from, len)
End Function
' Non-zero based index
Public Function [Mid](ByVal value As String, ByVal startIndex As Integer, _
ByVal length As Integer) As String
Return value.Substring(startIndex - 1, length)
End Function
Public Function Len(ByVal str As String) As Integer
Return Microsoft.VisualBasic.Len(str)
End Function
Soundex(string), Soundex(string,integer) - Functions for string matching.
' Standard Soundex function where resulting value length is four
Public Shared Function Soundex(ByVal Word As String) As String
Return Soundex(Word, 4)
End Function
' Generic function with length defined by the user
Public Shared Function Soundex(ByVal Word As String, ByVal Length As Integer) As String
' source: http://www.codeproject.com/KB/aspnet/Soundex.aspx
' Value to return
Dim Value As String = ""
' Size of the word to process
Dim Size As Integer = Word.Length
' Make sure the word is at least two characters in length
If (Size > 1) Then
' Convert the word to all uppercase
Word = Word.ToUpper()
' Conver to the word to a character array for faster processing
Dim Chars() As Char = Word.ToCharArray()
' Buffer to build up with character codes
Dim Buffer As New System.Text.StringBuilder
Buffer.Length = 0
' The current and previous character codes
Dim PrevCode As Integer = 0
Dim CurrCode As Integer = 0
' Append the first character to the buffer
Buffer.Append(Chars(0))
' Prepare variables for loop
Dim i As Integer
Dim LoopLimit As Integer = Size - 1
' Loop through all the characters and convert them to the proper character code
For i = 1 To LoopLimit
Select Case Chars(i)
Case "A"c, "E"c, "I"c, "O"c, "U"c, "H"c, "W"c, "Y"c
CurrCode = 0
Case "B"c, "F"c, "P"c, "V"c
CurrCode = 1
Case "C"c, "G"c, "J"c, "K"c, "Q"c, "S"c, "X"c, "Z"c
CurrCode = 2
Case "D"c, "T"c
CurrCode = 3
Case "L"c
CurrCode = 4
Case "M"c, "N"c
CurrCode = 5
Case "R"c
CurrCode = 6
End Select
' Check to see if the current code is the same as the last one
If (CurrCode <> PrevCode) Then
' Check to see if the current code is 0 (a vowel); do not proceed
If (CurrCode <> 0) Then
Buffer.Append(CurrCode)
End If
End If
' If the buffer size meets the length limit, then exit the loop
If (Buffer.Length = Length) Then
Exit For
End If
Next
' Padd the buffer if required
Size = Buffer.Length
If (Size < Length) Then
Buffer.Append("0"c, (Length - Size))
End If
' Set the return value
Value = Buffer.ToString()
End If
' Return the computed soundex
Return Value
End Function
Public Function Int(ByVal value As Object) As Integer
Return CInt(value)
End Function
Public Function [Date](ByVal value As String, ByVal format As String) As DateTime
Return DateTime.ParseExact(value, format, _
System.Globalization.CultureInfo.InvariantCulture)
End Function
Public Function Dec(ByVal value As Object) As Double
Return CDbl(value)
End Function
Public Function Chr(ByVal c As Integer) As String
Return Chr(c)
End Function
Public Function Money(ByVal d As Object) As String
'' Output a currency formatted string from input value d
'' Zero values and inputs that cannot be converted to numbers return empty string
Dim p As Double
Try
If TypeOf d Is String Then
p = Math.Round(Val(d), 4)
Return Format(p, "c")
Else
p = Math.Round(CType(d, Double), 4)
If p = 0 Then
Return ""
Else
Return Format(p, "c")
End If
End If
Catch ex As Exception
Return ""
End Try
End Function
Public Function FileExists(ByVal f As String) As Boolean
Return (FileInfo(f).Exists)
End Function
Public Function FileInfo(ByVal f As String) As IO.FileInfo
Dim altFilename As String = Me._schema.Path & f
Dim fi As IO.FileInfo
fi = New IO.FileInfo(altFilename)
If fi.Exists = False Then
fi = New IO.FileInfo(f)
End If
Return fi
End Function
Public Function [If](ByVal cond As Boolean, ByVal TrueValue As Object, _
ByVal FalseValue As Object) As Object
If cond Then
Return TrueValue
Else
Return FalseValue
End If
End Function
Public Function PadLeft(ByVal str As String, ByVal wantedlen As Integer, _
Optional ByVal addedchar As String = " ") As String
Do While Len(str) < wantedlen
str = addedchar &str
Loop
Return str
End Function
Public Function ChCR() As String
Return vbCr
End Function
Public Function ChLF() As String
Return vbLf
End Function
Public Function ChCRLF() As String
Return vbCrLf
End Function
Public Function Entry(ByVal n As Integer, ByVal s As String, _
Optional ByVal delim As String = ",") As String
Dim pos, pos2, l As Integer
l = s.Length
' first find the starting pos
For i As Integer = 0 To n
If pos2 >= l Then Return Nothing
pos = s.IndexOf(delim, pos2)
If pos < 0 Then
pos = s.Length
End If
If i = n Then
Return s.Substring(pos2, pos - pos2)
End If
pos2 = pos + 1
Next
Return Nothing
End Function
Public Function Index(ByVal s As String, ByVal search As String, _
Optional ByVal delim As String = ",") As Integer
'' Copied from common.globals
Dim pos, pos2, l As Integer
l = s.Length
'' first find the starting pos
For i As Integer = 0 To Integer.MaxValue
If pos2 >= l Then Return 0
pos = s.IndexOf(delim, pos2)
If pos < 0 Then
pos = s.Length
End If
If s.Substring(pos2, pos - pos2) = search Then
Return i
End If
pos2 = pos + 1
Next
Return 0
End Function
Public Function Inlist(ByVal search As String, ByVal list As String, _
Optional ByVal delim As String = ",") As Boolean
If Len(delim) = 0 Then Return False
If Len(list) = 0 Then
If Len(search) = 0 Then Return True Else Return False
End If
'' speed could be improved using indexof repeatidly
For Each entry As String In list.Split(delim.Chars(0))
If entry = search Then
Return True
End If
Next
Return False
End Function
Public Function Split(ByVal s As String, Optional ByVal delimiter As String = ",") As String()
Return s.Split(delimiter.Chars(0))
End Function
Function DbNull() As System.DBNull
Return System.DBNull.Value
End Function
Public Function FormatInteger(ByVal value As Integer, ByVal format As String) As String
Return value.ToString(format)
End Function
There are also global functions that get schema or process specific information:
Constant(name As String) - Gets value of Global Variable by name
Public Function Constant(ByVal name As String) As String
If Not _schema Is Nothing Then Return _schema.Constants(name).Value
Return Nothing
End Function
Start() - Gets time when current validation process was started
Public Function Start() As Date If Not _schema Is Nothing Then Return _schema.Start Return Nothing End FunctionDuration() - Gets duration of the current validation process.
Public Function Duration() As TimeSpan If Not _schema Is Nothing Then Return _schema.ExecutionTime Return Nothing End FunctionCurrentFile() - Gets the object of the file that is buing validated.
[CurrentFile.AliasName]
Public Function CurrentFile() As DataTable If Not _schema Is Nothing Then Return _schema.CurrentFile Return Nothing End Function