XAML/C++: String Conversion from textboxes

Well this is a continuing series of rambling blogs about string conversion from textboxes in XAML/C++ to numbers.  Kind of boring.

Ok let's take a look at the code:

1. First store the textbox input into a String Class.  The String^ is not a full feature string manipulation tool, why?  Likely because there are other classes that you can use that are included in the "standard" library, and Microsoft is attempting to be respectful of the C++ community.  And annoy me.  But it is more important to be respectful to the history and community of the C++ coding.  Use the String^ to get the data out of the textbox and then move it into your favorite C++ process for string manipulations, in my case I am going with wstring (see: https://msdn.microsoft.com/en-us/library/windows/apps/hh699879.aspx).

 

Use the following line, with the using statements:

  • using namespace Platform;
  • using namespace std;

In the pch.h make sure to add the following (don't worry about which one is used right now):

//my package additions
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <iostream>
#include <sstream>

Then in an event, like a button tap event (let's stay focused on touch, this will work with your left mouse button as well):

String^ strIntNumber = txtIntegerNumber->Text;

Add a integer declaration:

int T1;

What is that symbol made up of a hyphen and greater than symbol: –> , this symbol is the member access operator (see Member Access Operators: . and –>). 

This symbol -> might be confused with the "Structure dereference operator" , so see: https://www.cplusplus.com/articles/EN3hAqkS/ and https://www.cplusplus.com/articles/z186b7Xj/ for more about the "structure dereference operator". 

But the T1s->Data() is a reference to a member access operator and is discussed in the article: https://msdn.microsoft.com/en-us/library/windows/apps/hh825860(v=vs.110).aspx, but in general the Data returns a pointer to the string array. 

wstring declares a string of wide characters. wstring is a typedef that specializes the template class basic_string to contain wchar_t string data. wstring inherits the capabilities of the basic_string class; this includes operators and methods.  For more information about basic_string see: https://msdn.microsoft.com/en-us/library/syxtdd4f.aspx

wstring ws1( T1s->Data() );

Now we will convert the string to an integer, using the wstringstream and naming it converter, recall that the T1s->Data() creates a character array and the wstring consumes the char array so that you can work with it.  Isn't that weird, the text goes in to the converter and then is pumped into an integer, the process is done automagically.

wstringstream converter;
  //convert vars to integers
converter << ws1;
converter >> T1;

Working code is shown at: https://code.msdn.microsoft.com/Super-Simple-XAMLCCX-0052e692/view/Reviews

What if the enduser puts a non-number in the textbox?  Stay tuned.  More to follow.