Click here to Skip to main content
Rate this: bad
good
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 ...
	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 24-Jan-12 9:17am
RedDk10.3K
Edited 24-Jan-12 10:31am
v8
Comments
Emilio Garavaglia at 24-Jan-12 15:32pm
   
retagged as C++/CLI
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To begin with you should change c++ into cp++
 
for(cp=0;cp<5;cp++)
 
As for the casting, how about:
 
for(cp=0;cp<5;cp++)
{
    Console::WriteLine((System::String ^)strarray[cp]); // error C2440 'type cast' : cannot connvert from 'System::String^' to 'wchar_t' !!!
}
  Permalink  
Comments
RedDK at 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
good
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.
  Permalink  
Comments
RedDK at 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 at 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 at 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 at 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 at 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
0 OriginalGriff 290
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 149
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 24 Jan 2012
Copyright © CodeProject, 1999-2014
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