473,386 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

help converting code from C to C#

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;
};
Nov 15 '05 #1
2 2495
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" <vc@attbi.com> wrote in message
news:Ws8Cb.504326$HS4.3902552@attbi_s01...
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;
};

Nov 15 '05 #2
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" <el*************@wayout.net.NOSPAM> wrote in message
news:eG**************@TK2MSFTNGP09.phx.gbl...
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" <vc@attbi.com> wrote in message
news:Ws8Cb.504326$HS4.3902552@attbi_s01...
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;
};


Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
4
by: jipster | last post by:
hi, i need help converting this java program to C++...are there any programs or nething that could be used to do this faster?? class hw2 { static public void main (String args) { hw2.test();}...
4
by: eight02645999 | last post by:
hi i need help with converting a piece of perl code to python ...... my $start = '\'; my $file = '\'; my $end = '\'; ..... while(<FILE>) #open a file {
17
by: VM | last post by:
In my Windows app, I'm running a batch process that's composed of a FOR loop that'll run 15,000 times (datatable row count), copy cthe data of each row -3 fields- to a struct, and send the strct to...
4
by: kelli | last post by:
i am new to c# so if this is a trivial problem, forgive me! i've searched the web and after 2 days still cannot solve it. i am converting a c++ application to c# - the problem code is listed...
32
by: robert d via AccessMonster.com | last post by:
I'm looking at converting DAO to ADO in my app. All of my DAO connections are of the following structure: Dim wsName As DAO.Workspace Dim dbName As DAO.Database Dim rsName As DAO.Recordset ...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
10
by: minterman | last post by:
1. the program needs to convert btu to jules 2. Convert calories to joules 3. Convert joules to joules 4 exit the program. if the user type anything other than 1-4 the program should print a...
1
by: danmilkman | last post by:
Hi Y'all Can someone help me convert the next few lines of code from c++ to c#? I've given it a try myself but my program keeps crashing when I use my c# code. I think the difficulty lies in...
10
by: Hank Stalica | last post by:
I'm having this weird problem where my code does the following conversion from string to float: 27000000.0 -27000000.00 2973999.99 -29740000.00 2989999.13 -2989999.25 The number on the left...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.