D
Dave Taylor
I have a DataTable with three string columns, "Value", "Format" and "Type"
However, some of the rows contain numbers and dates in the Value field. So
I would like to be able to format the output based on the format specifier
in the Format field. The problem is, since the Value field is a string, it
doesnt properly "format".
So I need to convert to an object of the type specified by "Type"...ok, so
Activator.CreateInstance() BUT the problem is, the constructors dont take a
string argument.
So how do I create a new object of the specified type without knowing what
arguments it will need? There are no empty constructors that I can find.
I am mostly using DateTime, Single, Integer and String values as my "Type"
My weak attempt so far is shown below...any help is greatly appreciated.
Thanks
Dave Taylor
dv = CType(source.List, DataView)
drv = dv(rowNum)
szF = drv("Format")
szT = drv("Type")
szV = drv("Value")
'Convert the string to the appropriate type
If (szF <> "") Then
t = Type.GetType(szT)
v = Activator.CreateInstance(t) '**Error occurs here because of no 'empty
constructors' available
Try
v = szV
szV = Microsoft.VisualBasic.Format(v, szF)
Catch ex As InvalidCastException
'Ignore, just output the unformatted data if there is an error converting
End Try
End If
However, some of the rows contain numbers and dates in the Value field. So
I would like to be able to format the output based on the format specifier
in the Format field. The problem is, since the Value field is a string, it
doesnt properly "format".
So I need to convert to an object of the type specified by "Type"...ok, so
Activator.CreateInstance() BUT the problem is, the constructors dont take a
string argument.
So how do I create a new object of the specified type without knowing what
arguments it will need? There are no empty constructors that I can find.
I am mostly using DateTime, Single, Integer and String values as my "Type"
My weak attempt so far is shown below...any help is greatly appreciated.
Thanks
Dave Taylor
dv = CType(source.List, DataView)
drv = dv(rowNum)
szF = drv("Format")
szT = drv("Type")
szV = drv("Value")
'Convert the string to the appropriate type
If (szF <> "") Then
t = Type.GetType(szT)
v = Activator.CreateInstance(t) '**Error occurs here because of no 'empty
constructors' available
Try
v = szV
szV = Microsoft.VisualBasic.Format(v, szF)
Catch ex As InvalidCastException
'Ignore, just output the unformatted data if there is an error converting
End Try
End If