I
illegal.prime
Hi all, I have a class that holds a member of type object. This member
could end up being:
uint
int
long
ulong
and possibly some other int types I haven't mentioned. I also provide
secondary constructor that accepts the type as a parameter and stores
it in another member - i.e.:
Foo (Type type)
{
// other code
mType = type;
}
In a subsequent method, I end up doing this:
if (mType == typeof(int))
mInternal = Convert.ToInt32(external.Text);
else if (mType == typeof(uint))
mInternal = Convert.ToUInt32(external.Text);
....
Ideally I would rather do something like this:
mInternal = (mType) external.Text;
Any chance there is an alternate way to do this since the above doesn't
work.
After writing all of that I just noticed that there is a member in
Convert called ChangeType that does exactly what I want - by using it
thusly:
mInternal = Convert.ChangeType(external.Text, mType);
If anyone has anything else they want to add - please do so. I.E. if
my usage is improper/bad or if the basic issue I'm encountering could
be solved in a better way.
Thanks,
Novice
could end up being:
uint
int
long
ulong
and possibly some other int types I haven't mentioned. I also provide
secondary constructor that accepts the type as a parameter and stores
it in another member - i.e.:
Foo (Type type)
{
// other code
mType = type;
}
In a subsequent method, I end up doing this:
if (mType == typeof(int))
mInternal = Convert.ToInt32(external.Text);
else if (mType == typeof(uint))
mInternal = Convert.ToUInt32(external.Text);
....
Ideally I would rather do something like this:
mInternal = (mType) external.Text;
Any chance there is an alternate way to do this since the above doesn't
work.
After writing all of that I just noticed that there is a member in
Convert called ChangeType that does exactly what I want - by using it
thusly:
mInternal = Convert.ChangeType(external.Text, mType);
If anyone has anything else they want to add - please do so. I.E. if
my usage is improper/bad or if the basic issue I'm encountering could
be solved in a better way.
Thanks,
Novice