lunes, 30 de abril de 2007

Converting from Char to String on Visual C++ 2005

If working on a Windows Form application, under the name spaces we declare: char juan[20];. It should look like this:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
char juan[20];

Code for button1

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//This For is to fill with blank spaces all of the values in the Char arrays so that they don't save values from former processes.
for(int i=0;i<50;i++){
juan[i]=' ';
}
for (int i = 0 ; i
<textBox1->Text->Length ;i++)
juan[i] = textBox1-
>Text[i];
}


Code for button2

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
textBox2->Text="";
for (int i = 0 ; i< 20; i++)
textBox2->Text += Char::ToString(juan[i]);
}


Explanation:

The user of the program is going to write something on textBox1. But all what the user writes on textBox1 is by default considered and taken as String type. But we want to convert all what is oin textBox1 from its default String type to Char type. So first of all we declare an array with Char type, like this:

char juan[20];

Now we are ready to write code for the button1, and its work will be to transform the String information introduced by the user from button1, to Char type.

Once the information has been converted from String to Char, we already have data on our Char array, but we can't see it because it is just in memory. If we want to see in a textBox what is in our Char array, we must convert what is in the array, from Char to String, why?, well because a textBox would be capable to print out easily a String type, but never a Char type. To print out directly a Char element into a textBox is just impossible. So the button2 of our program would be the responsible to perform such conversion from Char to String and finally we will see on textBox2 what is saved in our Char array.

Spanish version/Versión en español

Si se está trabajando en una Aplicación de Windows Forms, bajo los nombres de espacio declaramos: char juan[20];. Debería verse así:

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
char juan[20];

Código para el button1

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//Con este For se llenan de espacios en blanco todos los valores de los arreglos Char para que no queden valores de procesos anteriores.
for(int i=0;i<50;i++){
juan[i]=' ';
}

for (int i = 0 ; i<textBox1->Text->Length ;i++)
juan[i] = textBox1-
>Text[i];
}


Código para el button2

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
textBox2->Text="";
for (int i = 0 ; i< 20; i++)
textBox2->Text += Char::ToString(juan[i]);
}

Explicación:

El usuario del programa va a escribir algo en el textBox1. Pero todo lo que el usuario escriba en el textBox1 es por defecto considerado y tomado como tipo String. Pero nosotros queremos convertir todo lo que esté en el textBox1 de su valor por defecto String al tipo Char. Por tanto lo primero es declarar un arreglo con tipo Char, así:

char juan[20];

Ahora estamos listos para escribir código en el button1, y su trabajo será transformar la información String introducida por el usiario desde el button1, al tipo Char.

Una vez que la información ha sido convertida de String a Char, ya tenemos datos en nuestro arreglo Char, pero no podemos verla porque está solamente en memoria. Si queremos ver en un textBox lo que está en nuestro arreglo Char, tenemos que convertir lo que está en el arreglo, de Char a String, ¿por qué?, bien porque un textBox sería capaz de imprimir fácilmente un tipo String, pero nunca un tipo Char. Imprimir directamente un elemento Char en un textBox es simplemente imposible. Por ello el button2 de nuestro programa sería el responsable de realizar esa conversión de Char a String y finalmente veremos en el textBox2 lo que se ha guardado en nuestro arreglo Char.

Jaime Montoya
jaimemontoya@jaimemontoya.com
www.jaimemontoya.com