I'm aware that many questions are similar, but possibly because many relate to C# that I can't easily translate to VB.Net, I can't find a way for the following anywhere. A function Test gets a parameter TypToValidate that specifies a Type to check against.
The code in the function reads a chunk of a line from a text into a String variable. Based on what is read, that string may be "abc" or "0.307" or "12" or whatever else. The function then has to check if that string can be cast into the Type specified by TypeToValidate. If so, the function returns the validated chunk of text as a string; if not, it throws an error.
Function Test(... , Optional ByVal TypeToValidate as Type = String) as String
ChunkOfText = SomeFile.ReadLine
If TypeOf(ChunkOfText) Is TypeToValidate Then
Return ChunkOfText
Else
Throw ex As ApplicationException ' or whatever like this
End If
...
I understand that 'Type' cannot itself be used as a TYPE, the TYPE of Types, conaining Types as values. I.o.w., I cannot pass a type to a function, or can I?
Obviously, I could use
Function Test(..., Optional ByVal TypeToValidate as String = "String") as String
and pass a String (like 'Byte', 'Integer', 'Int32', ...) to the function instead of a Type, but that does't seem very professional... and allows for errors. Any help, preferably in VB.Net?