9 Replies - 547 Views - Last Post: 07 November 2012 - 09:19 AM Rate Topic: -----

#1 Supaplex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 12

question calculator double a define in public (?)

Posted 07 November 2012 - 03:04 AM

Hello, i used a simple calculator code i found, now iam trying to make it somehow public, not sure how it should look like.

There's the code
namespace WindowsFormsApplication1
{


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public void button1_Click(object sender, EventArgs e)
        {
            double a = Convert.ToDouble(textBox1.Text);
            double b = Convert.ToDouble(textBox2.Text);

            textBox3.Text = (a + B)/>.ToString();
        }
    }
}



Problem is, when i want to have more buttons, i always have to define "a and b" again, i want it to be, more like that i define it ONCE, and then on buttons ill always just type text.Box3.Text = (a (function) B).ToString();

Is This A Good Question/Topic? 0
  • +

Replies To: question calculator double a define in public (?)

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 03:17 AM

Put it in a method, call the method. Pay attention to scope.
Was This Post Helpful? 0
  • +
  • -

#3 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 03:41 AM

Add 2 properties to the class, FirstNumber and SecondNumber of type double.
Parse input on Leave event from each textbox into one of the above mentioned.(I suggest using TryParse and ErrorProvider for data validation)
You'll have those values in memory and can call them from any method,without further declaration or initialization, as you wanted.
Be sure to check if the Text property of the text boxes contains something (with string.IsNullOrEmpty method), or you'll get an exception when performing operations on the values.
Was This Post Helpful? 1
  • +
  • -

#4 Supaplex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 12

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 03:54 AM

Ill try to manage something with this information, altough iam a complete newbie in a c#, and calculator is my completely first thing i started doing. Hope ill get into it and wont need more detailed explanation :P
Was This Post Helpful? 0
  • +
  • -

#5 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 04:13 AM

Further help:

Create a new Windows Form Application. Drag 2 textboxes and 1 button on it.
Above the constructor that was automatically generated, type prop. Press TAB twice. A snippet will appear. Set the type to double and give it a custom name. Repeat for other value.
Decide which property is for which textbox. Click a textbox. Press F4. Properties will appear. Press the lightning icon to see the events. Go to Leave and double click. The code designer will open, with a new method generated.
In that method, set one of the properties to the value of the Text property of the textbox you selected, parsed as double. You can achieve that with
 FirstNumber = double.Parse(txtBox1.Text); 
. Do the same for the second textbox.

To test it, double click the button on the form and write the following code:
 MessageBox.Show((FirstNumber + SecondNumber).ToString()); 

Be sure to replace the variables with your actual properties.

Now, press F5 to run the application. Insert values into the textboxes and press the button.
The sum of the numbers should appear.

Then , you can add as many buttons you want to perform operations, but be sure to validate your data so that operations can be performed. Search about data validation and ask further clarification.

Good luck!
Was This Post Helpful? 2
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 07:30 AM

Calculator: FAQ 31

But... Looking at your code sample you're what, maybe 2 weeks into a class. You aren't ready to be building an application from scratch. You don't know how much you don't know. You can't design until you have a better command of the coding language. For now, just follow the tutorials and pickup a couple "Learn C# in 30 days" type books to do in addition to your course work.


rookie: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#7 Supaplex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-November 12

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 08:20 AM

thanks for a detailed help, yet still i have a little complication...

Its with the first / second part

" Above the constructor that was automatically generated, type prop. Press TAB twice. A snippet will appear. Set the type to double and give it a custom name. Repeat for other value. "

Not completely sure which one's constructor..
When i just type prop. and press TAB twice, i pretty much automaticaly open properties of random thing that i have selected ( button, textbox or form ), and automatically start typing a text-name for it, and tabbing twice just selected stuff in properties.
Was This Post Helpful? 0
  • +
  • -

#8 coder3788  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 62
  • Joined: 06-November 12

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 08:38 AM

A constructor is a method that has no returning type and has the same name as the class that it belongs to. It's the method you call when you instantiate a new object from a class.
For a class named Form1, the constructor would be
 public Form1(){} 

It's not important to declare your properties above this constructor, the point is to declare them outside of any function, but inside the class. It's good to declare them before the constructor for readability This way,they are part of the class and you can use them in any function that belongs to this class.

Take note that when I say create 2 properties I'm referring to http://msdn.microsof.../bb384054.aspx.
It's not mandatory to type prop and hit 2xTAB, that's just a snippet you can use for convenience.
You can write a auto-implemented property this way :
 public double FirstNumber {get;set;} 

Read about constructors, fields, member functions, properties and other concepts of object-oriented programming and it will all be clear.
Was This Post Helpful? 1
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 08:57 AM

While I commend coder3788 for his patience and willingness to explain the ultra basics to you...

You have to take some responsibility for your own education. There are millions of noobies and only a hundred core members here. We simply can't hand-hold every rookie through their every question. We can't maintain thousands of threads for "What is a constructor?", "What does 'variable' mean?" and so on.

That's why we have a tutorials section and a learning C# series of articles.

I strongly urge you to not look at this site as your home for a free teacher/tutor because you don't want to buy a book. We don't do that. We help people advance their coding skills and help with code they have already written but are having trouble with. We don't do free program architecture/logic-design for those that figure it out (IE: We don't do your thinking for you.) But you have give us something to work with. At least read a couple books and a few on-line articles.

First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover. Do a couple hundred on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.

Then later you can start architecting your own simple stuff. Build a calculator. Build a DVD library program. Etc. Stuff that doesn't involve the complexity of a game. Then move up to games.



There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

Quote

Where do I start?


You start by learning a coding language FIRST.
Learn to plan before you type.
THEN you start designing software with a purpose.


If this sounds like you

Newbie/Rookie said:

I have a little programming experience but I need to write ...
read this section
Spoiler


Otherwise, you can just jump to the resources here:
Some of the tutorials below are for C# or Java not C, C++, VB.NET [...]. But the conceptual stuff of classes, object oriented design, events etc. are not language specific and should give you enough guidance in theory of program development for you to be able to look-up specific code example in your chosen coding language.



Resources, references and suggestions for new programmers. - Updated Oct 2012
Spoiler

Was This Post Helpful? 2
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5529
  • View blog
  • Posts: 11,839
  • Joined: 02-June 10

Re: question calculator double a define in public (?)

Posted 07 November 2012 - 09:19 AM

C# learning series right here on Dream in code
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1