473,834 Members | 1,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(in t 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 2511
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_Clic k(object sender, EventArgs e)
{
if(txtDisplay.T ext.Length < 14)
{
Button btn = (Button)sender;
txtDisplay.Text += btn.Text;
}
}

Best regards,
Sherif

"Mountain Bikn' Guy" <vc@attbi.com > wrote in message
news:Ws8Cb.5043 26$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(in t 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.ToStrin g();
mainForm.Displa yTextBox = myDisplay;
}
}

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

Regards,
Mountain

"Sherif ElMetainy" <el************ *@wayout.net.NO SPAM> wrote in message
news:eG******** ******@TK2MSFTN GP09.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_Clic k(object sender, EventArgs e)
{
if(txtDisplay.T ext.Length < 14)
{
Button btn = (Button)sender;
txtDisplay.Text += btn.Text;
}
}

Best regards,
Sherif

"Mountain Bikn' Guy" <vc@attbi.com > wrote in message
news:Ws8Cb.5043 26$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(in t 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
2775
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 here..... -------- // // a set of simple C# wrappers over the NT Defragmenter APIs //
4
1771
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();} Board board = new Board(8); if ( board.fillInRemainingLevels() ) board.print(); else System.out.println("Couldn't find solution");
4
2370
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
1984
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 an external method (using DLLImport). The problem is that the processing speed is very inconsistent- it may run very FAST during certain points but then it runs VERY slow at other moments (especially when it's getting to the end of the table:...
4
4509
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 below. the c++ code uses a byte array (msg) to hold a serial message. since the message can be of different layouts, the array is cast to the appropriate layout "type" (struct SERIAL_CAN_MSG_TYPE, in this example). to complicate things, there is an...
32
12527
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 Set wsName = DBEngine.Workspaces(0) Set dbName = wsName.OpenDatabase(CurrentProject.FullName) Set rsName = dbName.OpenRecordset("SQL Statement")
16
5454
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 learning all help would be greatly apperciated. Attribute VB_Name = "Module1" Option Explicit
10
5731
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 error message. here is my code. import java.util.Scanner;
1
1783
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 converting the different datatypes. I'm no star in programming C++ and always have had problems with pointers. Help is greatly appreciated. Here's the code: BOOL EXAPI
10
3248
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 is the string I get after tokenizing a bigger string. The number on the right is the number I get after the conversion.
0
9796
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10789
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10504
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10544
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10214
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5624
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.