Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
C++
#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 ...
	for(cp=0;cp<5;cp++)
	{
		Console::WriteLine(chararray3[cp]);
	}
	Console::WriteLine(" ");					// space
	// String array contents to console ... expected result with subbstitution character "?" due to code-page-error/other
	for(cp=0;cp<5;cp++)
	{
		Console::WriteLine(strarray[cp]);
	}
	// Question is: why isn't THIS gonna happen ...  ????
	for(cp=0;cp<5;cp++)
	{
		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
Updated 24-Jan-12 9:31am
v8
Comments
Emilio Garavaglia 24-Jan-12 15:32pm    
retagged as C++/CLI

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

C++
for(cp=0;cp<5;cp++)


As for the casting, how about:

C++
for(cp=0;cp<5;cp++)
{
    Console::WriteLine((System::String ^)strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
}
 
Share this answer
 
Comments
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...
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.
 
Share this answer
 
Comments
RedDk 24-Jan-12 15:32pm    
Yes,
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<char>^myChars = gcnew array<char>{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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900