473,387 Members | 1,892 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,387 software developers and data experts.

type cast won't work

Given the following lines of code . . .

System::Collections::Generic::List<unsigned intls(1000);
String^ datum = "12345";

How would I change datum into an unsigned integer value and put it in ls[0]?
I've already tried . . .

ls[0] = (unsigned int)datum;

.. . . but the compiler was not able to perform the coversion.

Daniel
Jul 18 '08 #1
12 1105
Hi Daniel,
Given the following lines of code . . .

System::Collections::Generic::List<unsigned intls(1000);
String^ datum = "12345";

How would I change datum into an unsigned integer value and put it in
ls[0]? I've already tried . . .

ls[0] = (unsigned int)datum;
ls[0] = System::Convert::ToUInt32(datum);

--
SvenC
Jul 18 '08 #2
That gives me an unhandled exception error.

Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:C9**********************************@microsof t.com...
Hi Daniel,
>Given the following lines of code . . .

System::Collections::Generic::List<unsigned intls(1000);
String^ datum = "12345";

How would I change datum into an unsigned integer value and put it in
ls[0]? I've already tried . . .

ls[0] = (unsigned int)datum;

ls[0] = System::Convert::ToUInt32(datum);

--
SvenC

Jul 18 '08 #3
Hi Daniel,
That gives me an unhandled exception error.
Did you even try to figure out what the problem is?
It is not System::Convert::ToUInt32(datum).
You are accessing ls[0] when element 0 does not exist.

Please follow the MSDN documentation to learn how to use
the .Net framework classes.

From your posts I would really suggest you stick with C#
which gives you less opportunity for mistakes. When you
program with the .Net framework you have absolutely no
benefit in using C++/CLI.

--
SvenC
Jul 18 '08 #4
Daniel wrote:
Given the following lines of code . . .

System::Collections::Generic::List<unsigned intls(1000);
String^ datum = "12345";

How would I change datum into an unsigned integer value and put it in ls[0]?
I've already tried . . .

ls[0] = (unsigned int)datum;

. . . but the compiler was not able to perform the coversion.
Daniel:

Not related to your question, but in C++/CLI you should write

String^ datum = L"12345";

--
David Wilkinson
Visual C++ MVP
Jul 18 '08 #5
That's a suggestion I consider seriously. However, I am a college student,
and the class assignments require the use of C++. I'll have to pick up C#
later.

Is C# a very low level language comparable to C++?

Daniel

"SvenC" <Sv***@nospam.nospamwrote in message
news:AD**********************************@microsof t.com...
Hi Daniel,
>That gives me an unhandled exception error.

Did you even try to figure out what the problem is?
It is not System::Convert::ToUInt32(datum).
You are accessing ls[0] when element 0 does not exist.

Please follow the MSDN documentation to learn how to use
the .Net framework classes.

From your posts I would really suggest you stick with C#
which gives you less opportunity for mistakes. When you
program with the .Net framework you have absolutely no
benefit in using C++/CLI.

--
SvenC

Jul 18 '08 #6
Does the "L" in front of the "12345" represent "long"?

"David Wilkinson" <no******@effisols.comwrote in message
news:eh**************@TK2MSFTNGP03.phx.gbl...
Daniel wrote:
>Given the following lines of code . . .

System::Collections::Generic::List<unsigned intls(1000);
String^ datum = "12345";

How would I change datum into an unsigned integer value and put it in
ls[0]? I've already tried . . .

ls[0] = (unsigned int)datum;

. . . but the compiler was not able to perform the coversion.

Daniel:

Not related to your question, but in C++/CLI you should write

String^ datum = L"12345";

--
David Wilkinson
Visual C++ MVP

Jul 18 '08 #7
Daniel wrote:
Does the "L" in front of the "12345" represent "long"?
Daniel:

Yes, I think so. But the actual meaning is that it a string of wchar_t, as
opposed to "12345" which is an array of char.

On Windows, char is 8 bits, and wchar_t is 16 bits. All strings in .NET are
16-bit strings.

--
David Wilkinson
Visual C++ MVP
Jul 18 '08 #8
Daniel wrote:
That's a suggestion I consider seriously. However, I am a college student,
and the class assignments require the use of C++. I'll have to pick up C#
later.

Is C# a very low level language comparable to C++?
Daniel:

Are you sure that your college course is intending you to use C++/CLI? That
would be a very strange thing to do, IMHO. Remember, C++/CLI is *not* C++; it is
a *different* language.

No, I would say that C# is a higher level language than C++, because in its
normal safe mode it prevents you from doing low-level things (like manipulate
pointers) that can be dangerous. It also has garbage collection, which takes
care of memory management.

--
David Wilkinson
Visual C++ MVP
Jul 18 '08 #9
The class doesn't require me to use C++/CLI. However, I have the restraint
of using Visual C++ and whatever will work in the VC++ IDE, whether it be C,
C++, C++/CLI, or Assembly.

Daniel

"David Wilkinson" <no******@effisols.comwrote in message
news:uk**************@TK2MSFTNGP03.phx.gbl...
Daniel wrote:
>That's a suggestion I consider seriously. However, I am a college
student, and the class assignments require the use of C++. I'll have to
pick up C# later.

Is C# a very low level language comparable to C++?

Daniel:

Are you sure that your college course is intending you to use C++/CLI?
That would be a very strange thing to do, IMHO. Remember, C++/CLI is *not*
C++; it is a *different* language.

No, I would say that C# is a higher level language than C++, because in
its normal safe mode it prevents you from doing low-level things (like
manipulate pointers) that can be dangerous. It also has garbage
collection, which takes care of memory management.

--
David Wilkinson
Visual C++ MVP

Jul 18 '08 #10
Daniel wrote:
The class doesn't require me to use C++/CLI. However, I have the restraint
of using Visual C++ and whatever will work in the VC++ IDE, whether it be C,
C++, C++/CLI, or Assembly.
I suggest you go and get a real good book teaching C++ before you
continue. If you can cope with a steep learning curve, have a look
at Koenig/Moo (www.acceleratedcpp.com). It gives you a short (250
pages) introduction and I consider it a very good one. (I teach
C++.) If you think that's too steep a curve, Lippman's 4th edition
(http://www.amazon.com/dp/0201721481/) is just as good, only much
longer (1000 pages). You could also look for good beginner's books
at the ACCU's book review site (www.accu.org).
Daniel
[...]
Schobi
Jul 18 '08 #11
Daniel wrote:
The class doesn't require me to use C++/CLI. However, I have the restraint
of using Visual C++ and whatever will work in the VC++ IDE, whether it be C,
C++, C++/CLI, or Assembly.
Daniel:

Then let me put it differently. I think that you would be best served by not
using C++/CLI, because

(a) It is a complex language, not suited to an introductory programming course.

(b) Learning C++/CLI will likely not pay off in the future, because Microsoft
has abandoned the attempt to promote C++/CLI as a first class .NET language.

If you are taking a C++ course, stick to standard C++. As Schobi suggests, get a
good book which treats only standard C++. Another good one (I am told) is Bruce
Eckel's "Thinking in C++".

Have you tried talking to your instructor about these issues?

--
David Wilkinson
Visual C++ MVP
Jul 18 '08 #12
David Wilkinson <no******@effisols.comwrote:
[...]
If you are taking a C++ course, stick to standard C++. As Schobi suggests, get a
good book which treats only standard C++. Another good one (I am told) is Bruce
Eckel's "Thinking in C++".
Ah, that one I never read, so I keep forgetting to
recommend it. Sorry.
Have you tried talking to your instructor about these issues?
Very good idea.
Schobi

--
Sp******@gmx.de is never read
I'm HSchober at gmx dot de
"I guess at some point idealism meets human nature and
explodes." Daniel Orner
Jul 20 '08 #13

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

Similar topics

15
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int)...
8
by: Joe | last post by:
I have a web service which returns many types (classes) to match the return type. I want to cast the return type to the actual class type. For example: namespace Test { class MyClass { ...
11
by: JohnR | last post by:
I'm trying to find a way to create a variable of a given type at runtime where I won't know the type until it actually executes. For example, dim x as object = "hi" x is declared as an object...
2
by: Martin v. Löwis | last post by:
I've been working on PEP 353 for some time now. Please comment, in particular if you are using 64-bit systems. Regards, Martin PEP: 353 Title: Using ssize_t as the index type Version:...
31
by: dragoncoder | last post by:
Consider the code class A { private: int a; }; int main(void) { A x; int* ptr = (int*)&x;
1
by: Tommaso Caldarola | last post by:
delegate void SomeDelegate<T>() where T : BaseClass; my_event(object sender, myEventArgs e) { //e.Tag object is SomeDelegate<DerivedClass> SomeDelegate<BaseClass> del =...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
7
by: llothar | last post by:
When i use -W4 on visual c 7.0 i get warning C4054 translator1.c(1703) : warning C4054: 'type cast' : from function pointer 'void * (__cdecl *)(se_agent *)' to data pointer 'void *'...
1
by: ceestand | last post by:
I'm trying to make a change to an object depending contextually on it's type. The following code, even though I pass the Int32 into the function as an Object, still knows it's an Int32. public...
8
by: Oliver Graeser | last post by:
Hi All, I'm coming from Java to C++ and this is one of the very last problems I have so far... In Java, if I have, say, a class SISNode that extends NetworkNode, I can have a function that...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.