Boring. Ok, if it’s boring, then find a clearly written piece of documentation that shows you the absolutely easy way to convert an integer to a string in C++/CX? Go ahead I’ll wait. I just spent the morning trying to find clear step by step procedures on how to convert an integer to string for use in XAML and C++. Sure I know how to do this in ANSI C++, but that wasn’t my goal, I needed the correct string to enter an integer into one of the XAML Textbox or Textblock controls. And if you are a device driving kind of developer, this is likely not for you. But basically you use the .ToString() to do the conversion (that is a period followed by the ToString() at the end there is the open and close parentheses.
Here is how:
Create a Windows Store App in C++
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button x:Name="ExampleButton" Content="Button" HorizontalAlignment="Left" Height="142" Margin="78,290,0,0"
VerticalAlignment="Top" Width="572" Tapped="ExampleTapped" />
<TextBlock x:Name="txtTextBlock" HorizontalAlignment="Left" Height="107" Margin="78,10,0,0"
TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="572" FontSize="36"/>
<TextBox x:Name="txtTextBox" HorizontalAlignment="Left" Height="100" Margin="78,147,0,0"
TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="572" FontSize="36"/>
</Grid>
void IntegerToStringConversion::MainPage::ExampleTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::TappedRoutedEventArgs^ e)
{
int integer8 = 8;
double double20 = 20.0;
txtTextBlock->Text=
"TextBlock " + integer8.ToString() + " Double " + double20.ToString();
txtTextBox->Text=
integer8.ToString() + " TextBox";
}
If you forget to add the parentheses after the “ToString()” you will likely see errors like the following:
c:\users\sstokes\documents\visual studio 2012\projects\integertostringconversion\mainpage.xaml.cpp(48): error C3867: 'default::int32::ToString': function call missing argument list; use '&default::int32::ToString' to create a pointer to member 1>c:\users\sstokes\documents\visual studio 2012\projects\integertostringconversion\mainpage.xaml.cpp(48): error C2679: binary '+' : no operator found which takes a right-hand operand of type 'Platform::String ^(__cdecl default::int32::* )(void)' (or there is no acceptable conversion) 1> could be 'built-in C++ operator+(volatile const Platform::Object ^, volatile const Platform::String ^)' 1> or 'built-in C++ operator+(volatile const Platform::String ^, volatile const Platform::Object ^)' 1> or 'built-in C++ operator+(volatile const Platform::String ^, volatile const Platform::String ^)' 1> while trying to match the argument list '(Platform::String ^, Platform::String ^(__cdecl default::int32::* )(void))'
The Windows 8 Samples do not clearly call out how to work with the XAML Textboxes, TextBlocks and similar as this is considered pretty low level. Hence the reason I am writing about it. But the documentation generally online in any form on any site for this subject is written by knowledgable C++ programmers and there is a great deal of information to get out there about the C++/CX. So the writers assume that you have worked with XAML and C++ or that you can figure out easily how to work with XAML and C++ in that you have been working with something other then console apps or supplying DLL types of classes to C#, etc. Of course, the XAML and C++ is very similar to C#, but it is tricky.
For example the illustration given here is quite simple, but why a period with the “ToString” and the “->” operator with the Text property on the textblock and text box? What is the difference?
integer8.ToString is a postfix-expression represents a value of struct, class, or union type, and name names a member of the specified structure, union, or class. The value of the operation is that of name and is an l-value if postfix-expression is an l-value.
txtTextBox->Text is a postfix-expression represents a pointer to a structure, union, or class, and name names a member of the specified structure, union, or class. The value is that of name and is an l-value. The –> operator dereferences the pointer. Therefore, the expressions e–>member and (*e).member (where e represents a pointer) yield identical results (except when the operators –> or * are overloaded).
Legal Note:
Restrictions: