473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Type casts

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

Jun 27 '06 #1
6 1184
You should use enumeration that can simplify your life and the person
who uses the class:

private enum Types
{
Uint = uint,
Int = int
Long = long
}

il***********@gmail.com escreveu:
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


Jun 27 '06 #2
jo**********@hotmail.com wrote:
You should use enumeration that can simplify your life and the person
who uses the class:

private enum Types
{
Uint = uint,
Int = int
Long = long
}

il***********@gmail.com escreveu:
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


Hi Joao,

I'm afraid that's not valid syntax.

--
Hope this helps,
Tom Spink
Jun 27 '06 #3
As I gather it, mType contains the actual type of the value you have put
in the object? Then you can just do like this:

mInternal = external.Text;

The ChangeType method doesn't do anything at all, as you are converting
the object the same type as it already is.
il***********@gmail.com wrote:
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

Jun 27 '06 #4
Not quite - the conversion is necessary since as the property on the
external object implies it is just returning a string. So I need to
convert it to the appropriate type.

Novice

Göran Andersson wrote:
As I gather it, mType contains the actual type of the value you have put
in the object? Then you can just do like this:

mInternal = external.Text;

The ChangeType method doesn't do anything at all, as you are converting
the object the same type as it already is.
il***********@gmail.com wrote:
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


Jun 27 '06 #5
I don't know what you're using this for but it sounds like a prime candidate
for generics.

Define your class as Foo<T> and use Convert.ChangeType(externalText, T).

--
Jeffrey Hornby
Hornby Consulting, Inc.

"il***********@gmail.com" wrote:
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

Jun 28 '06 #6
Generics are .Net 2, right?

If not, then I guess I should start using them - otherwise, I'm not
allowed to upgrade since we don't have a way to upgrade our user base's
version of .Net.

Thanks,
Novice

Jeffrey Hornby wrote:
I don't know what you're using this for but it sounds like a prime candidate
for generics.

Define your class as Foo<T> and use Convert.ChangeType(externalText, T).

--
Jeffrey Hornby
Hornby Consulting, Inc.

"il***********@gmail.com" wrote:
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


Jun 28 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Busin | last post by:
In the code below, should i be casted to "unsigned int" type first before passing it into f()? Thanks! void f(unsigned int); int main() { unsigned short i; i = 23; /* Should i be casted to...
14
by: jimmy | last post by:
Hi, Please forgive me if this is elementary. I can't seem to find the solution anywhere. // foo() is only declared/defined in Derived Base* base = new Derived(); ((Derived*)base)->foo(); ...
20
by: Xiaoshen Li | last post by:
Hi, I am reading the book "Concepts of programming languages" by R. W. Sebesta. He said: "One of the most important reasons why C is both liked and disliked is its lack of complete type...
10
by: lovecreatesbeauty | last post by:
Why (type*)pointer isn't equal to *(type**)pointer, In the code snippet, it shows that: (int *) == (int **) , (int *) != (*(int **)) . Does type-casting change the address? or...
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
16
by: danielbuus | last post by:
....or, to put it perhaps more communicative, something like this: Type someObjectsType = someObject.GetType(); someObjectsType newObject = new someObjectsType(); Is this possible? If so, how?...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
9
by: Hamish M | last post by:
Hi I am interested in opinions on this topic. I have heard that a suffix is not a good solution and type casts are much better for example. ...
27
by: Noah Roberts | last post by:
What steps do people take to make sure that when dealing with C API callback functions that you do the appropriate reinterpret_cast<>? For instance, today I ran into a situation in which the wrong...
21
by: Chad | last post by:
Okay, so like recently the whole idea of using a Union in C finally sunk into my skull. Seriously, I think it probably took me 2 years to catch on what a Union really is. Belated, I mentioned this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.