Click here to Skip to main content
11,927,190 members (51,603 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++/CLI Managed CLR
#using <System.dll>
using namespace System;
int main()
	//Two arrays: __wchar_t and String
	array<__wchar_t^>^ chararray3 = gcnew array<__wchar_t^>(5);
	array<String^>^ strarray = gcnew array<String^>(5);
	//values into arrays
	chararray3[0] = L'\u2550';
	chararray3[1] = L'\u2551';
	chararray3[2] = L'\u2552';
	chararray3[3] = L'\u2553';
	chararray3[4] = L'\u2554';
	// note: that compiler generates warning C4566 when generating next array "cannot be represented in current code page (1252)"
	strarray[0] = "\u2550";
	strarray[1] = "\u2551";
	strarray[2] = "\u2552";
	strarray[3] = "\u2553";
	strarray[4] = "\u2554";
	int cp;
	// __wchar_t array contents to console ...
	Console::WriteLine(" ");					// space
	// String array contents to console ... expected result with subbstitution character "?" due to code-page-error/other
	// Question is: why isn't THIS gonna happen ...  ????
		Console::WriteLine((__wchar_t) strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
	/* wth ... converting on the fly! 
		Why not! It's a string but it's also a literal unicode value identical to the ones 
		contained in the array of __wchar_t that expands to console value without complaint in the above!?
	return 0;
Posted 24-Jan-12 9:17am
Edited 24-Jan-12 10:31am
Emilio Garavaglia 24-Jan-12 15:32pm
retagged as C++/CLI
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

To begin with you should change c++ into cp++


As for the casting, how about:

    Console::WriteLine((System::String ^)strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
RedDK 24-Jan-12 15:11pm
I know what "casting" means, in fact I use it in the title of this question ...

Should I have used "convert" alone?

This example runs, by the way...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You cannot cast a string to a char. Just because you have a single character in your string does not make it into a character constant, it remains a string but with only one character in it.
RedDK 24-Jan-12 15:32pm
That seems to be what the compiler is telling me. But you can see that what I'm trying to do here is write to console a character represented by a unicode string found in a string array, right?
RedDK 24-Jan-12 16:16pm
Let me rephrase that attack on submitted knowledge:

Take this array, fregzample. Character array ... holding string literals.

array^myChars = gcnew array{L'z','a',L'\u0306',L'\u01FD',L'\u03B2',L'\xD8FF',L'\xDCFF'};

See my point?
Richard MacCutchan 25-Jan-12 3:54am
You are still mixing types incorrectly. A System::String^ is not the same as a character array, nor is it the same as a character so you cannot cast one to the other. You need to look at the function you are trying to use (Console::WriteLine()) and check what types you can pass it; see here for a sample.
RedDK 25-Jan-12 12:38pm
Quote from page linked:

"No code example is currently available or this language may not be supported."

Thanks ...
Richard MacCutchan 25-Jan-12 12:46pm
It works fine for me, maybe it's your regional setting. Try searching MSDN for references to Console::WriteLine() and see if it comes up with some useful information.
If you still do not understand the difference between all these types then I can only suggest you go back to your reference documentation and read up on it. Sorry but I don't have a link to managed C++, although I expect MSDN has lots.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.151126.1 | Last Updated 24 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100