By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,156 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,156 IT Pros & Developers. It's quick & easy.

How to perform addition in an array

P: 1
i am new to c sharp. i am building a simple calculator with a text box and few buttons. i need to store values entered into the text box from string to char array so that i can check for the '+' and perform addition among the elements like 12+22+11. i need to add all this functionality to the = button because i'll be using the + button to just add data into the textbox like 1, 2 buttons etc. This is what i have in the = button

Expand|Select|Wrap|Line Numbers
  1. private void button3_Click(object sender, EventArgs e)
  2.         {
  3.             string s = textBox1.Text;
  4.             char[] c = s.TocharArray();
  5.             int i = 0;
  6.             for(;i<c.Length;i++)
  7.             {
  8.                 if(c[i].Equals('+'))
I can't figure out what to do next. Can someone help me complete the code and also figure out the errors in the existing code if any. need help urgently.
May 8 '10 #1
Share this Question
Share on Google+
3 Replies

Dheeraj Joshi
Expert 100+
P: 1,123
One way. Split the input String on "+". Then convert the array elements to integer data type. And add.

Remember you need to put validations to handle the extreme conditions or illegal inputs.

Dheeraj Joshi
May 8 '10 #2

Expert 2.5K+
P: 3,525

Expert 2.5K+
P: 3,525
May I suggest picking up a basic C# introductory book? It's not that people here don't want to be helpful, but there is a certain amount of basic learning work that one should really take upon themselves before asking for help. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" books at your look book seller or even public library... Asking a forum, any forum, to hand-hold you through it is just redundant. In many ways it disrespects the people who have invested dozens of hours in the on-line tutorials and those that spent thousands of hours in authoring books.

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tip, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Building an application Part 1
Building an application part 2
May 8 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.