help converting code from C to C#

  • Thread starter Thread starter Mountain Bikn' Guy
  • Start date Start date
M

Mountain Bikn' Guy

This code is used in a simple example calculator. It has a text box that
displays the result. It performs addition, subtraction, multiplication, etc.
on two operands. For the operands it accepts digits 0..9 and a decimal
point. The insert() method isn't very clear to me. Can anyone help me
convert the code shown below to C#?
Thanks!

enum { PRECISION = 14 };

//-------------------------------------------------------------------
void Calc::clear() {
myDisplay[0] = ' ';
myDisplay[1] = '0';
myDisplay[2] = '\0';
myIns = &myDisplay[1];
SetDlgItemText(myHwnd, IDC_DISPLAY, myDisplay);
}
//...................................................................
void Calc::insert(int keyId) {
if (myIns < &myDisplay[PRECISION - 1]) {
*myIns++ = (keyId == IDC_POINT) ? '.' : keyId - IDC_0 + '0';
*myIns = '\0';
SetDlgItemText(myHwnd, IDC_DISPLAY, myDisplay);
}
}


class Calc {
public:
Calc() {}

// helper methods...
void clear();
void insert(int keyId);

private:
char myDisplay[40];
char *myIns;
};
 
Hello Mountain

Apparently the insert function is called in response to a button click, it
checks it the length of the text displayed is less than 14, if so appends
the key text to the string and updates the display.
A c# equivalent would be something like this

void CalcButton_Click(object sender, EventArgs e)
{
if(txtDisplay.Text.Length < 14)
{
Button btn = (Button)sender;
txtDisplay.Text += btn.Text;
}
}

Best regards,
Sherif
 
Hi Sherif,
Thanks for your reply. In the C code was trying to figure out how the
decimal place and leading zero were handled. At first glance it seemed to be
in the code I posted. I assumed the variable that held the address of the
current string element (myIns) was helping handle this logic, but that
doesn't seem to be the case.

I ended up writing the following code, but it is some really crappy code.
I'm going to have to look deeper and refactor a few things...

private void Insert(char keyChar)
{
if (this.myDisplay.Length < PRECISION)
{
if (keyChar != '.')//TODO: clean this logic up
{
if (myDisplay == " 0")
{
this.myDisplay = " ";
}
else if (myDisplay == "-0")
{
this.myDisplay = "-";
}
}
this.myDisplay += keyChar.ToString();
mainForm.DisplayTextBox = myDisplay;
}
}

If you have any other suggestions, let me know. Thanks!

Regards,
Mountain

Sherif ElMetainy said:
Hello Mountain

Apparently the insert function is called in response to a button click, it
checks it the length of the text displayed is less than 14, if so appends
the key text to the string and updates the display.
A c# equivalent would be something like this

void CalcButton_Click(object sender, EventArgs e)
{
if(txtDisplay.Text.Length < 14)
{
Button btn = (Button)sender;
txtDisplay.Text += btn.Text;
}
}

Best regards,
Sherif

Mountain Bikn' Guy said:
This code is used in a simple example calculator. It has a text box that
displays the result. It performs addition, subtraction, multiplication, etc.
on two operands. For the operands it accepts digits 0..9 and a decimal
point. The insert() method isn't very clear to me. Can anyone help me
convert the code shown below to C#?
Thanks!

enum { PRECISION = 14 };

//-------------------------------------------------------------------
void Calc::clear() {
myDisplay[0] = ' ';
myDisplay[1] = '0';
myDisplay[2] = '\0';
myIns = &myDisplay[1];
SetDlgItemText(myHwnd, IDC_DISPLAY, myDisplay);
}
//...................................................................
void Calc::insert(int keyId) {
if (myIns < &myDisplay[PRECISION - 1]) {
*myIns++ = (keyId == IDC_POINT) ? '.' : keyId - IDC_0 + '0';
*myIns = '\0';
SetDlgItemText(myHwnd, IDC_DISPLAY, myDisplay);
}
}


class Calc {
public:
Calc() {}

// helper methods...
void clear();
void insert(int keyId);

private:
char myDisplay[40];
char *myIns;
};
 
Back
Top