type cast won't work

D

Daniel

Given the following lines of code . . .

System::Collections::Generic::List<unsigned int> ls(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
 
S

SvenC

Hi Daniel,
Given the following lines of code . . .

System::Collections::Generic::List<unsigned int> ls(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);
 
D

Daniel

That gives me an unhandled exception error.

Daniel

SvenC said:
Hi Daniel,
Given the following lines of code . . .

System::Collections::Generic::List<unsigned int> ls(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);
 
S

SvenC

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.
 
D

David Wilkinson

Daniel said:
Given the following lines of code . . .

System::Collections::Generic::List<unsigned int> ls(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";
 
D

Daniel

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 said:
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.
 
D

Daniel

Does the "L" in front of the "12345" represent "long"?

David Wilkinson said:
Daniel said:
Given the following lines of code . . .

System::Collections::Generic::List<unsigned int> ls(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";
 
D

David Wilkinson

Daniel said:
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.
 
D

David Wilkinson

Daniel said:
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.
 
D

Daniel

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
 
H

Hendrik Schober

Daniel said:
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
 
D

David Wilkinson

Daniel said:
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?
 
H

Hendrik Schober

David Wilkinson said:
[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top