I need to write a simpe calc. program to add, mutiply, subtract and divide. I want to have two textboxes that I enter numbers in and when I click a button, +,-,/ or * a third text box will display the result.
Rather than declaring my variables inside each instance of a click event I would like to declare them globally so I just need to perfom the calculation on them and display result. However declaring them globally doesn't work for me. I;ve tried the variables inside the btnAdd click event which works, but as I said I'd prefer to declare them onece globally. Anyone see where I'm going wrong?

Public Class Form1
    Dim number1 As Double = CDbl(txtNum1.Text)
    Dim number2 As Double = CDbl(txtNum2.Text)
    Dim total As Double = txtTotal.Text

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        total = number1 + number2
        lblOperator.Text = "+"
    End Sub
End Class

What is wrong with the following in btnAdd_Click ?
Dim total As Double = CDbl(txtNum1.Text) + CDbl(txtNum2.Text)
That way you don't need number1 and number2.
Now convert total back to text and update txtTotal.Text.

If I use that I will need to reuse it in all my other events (btnSubtract, btnMutiple etc). I haven't put them in yet but they will need to be there too. I think that's overuse of code. though maybe I could just declare them globally once.
Your code is fine, I got similar code to work myself just thought might be easier way

though maybe I could just declare them globally once

Yes, you can do that

Public Class Form1
  Dim number1 As Double
  Dim number2 As Double
  Dim total As Double
.
.
.
End Class

But the point where your code goes wrong is

Public Class Form1
  Dim number1 As Double = CDbl(txtNum1.Text)
  Dim number2 As Double = CDbl(txtNum2.Text)
  Dim total As Double = txtTotal.Text
.
.
.
End Class

All those three variables get their values at the form load and they never change, which you are expecting from the calculator. In fact, that code should raise an error.

Like ddanbe showed, you can make all those arithmetic operations as (almost) one-liners txtTotal.Text = (CDbl(txtNum1.Text) + CDbl(txtNum2.Text)).ToString

If I use that I will need to reuse it in all my other events (btnSubtract, btnMutiple etc). I haven't put them in yet but they will need to be there too. I think that's overuse of code.

Declaring three (even same) variables in four event handlers is far from overuse of the code. After all, you may get more readable and easily maintainable code with some "overuse" of the code. Also, with modern compilers and computers you won't gain anything in the memory consumption and/or speed by using global variables. At least in a simple calculator application.

I think the best way to go is ddambe's way, or doing everything in one spot. this is c#, vb is similiar. TextBox3.Text = Convert.ToString(Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox2.Text));

Hi

My name Is vihanga, I am from Sri Lanka, Studing at Westly College, Colombo.

your main problem is the declaring variable . you declare it as double and trying to assign text to is so just assign numeric value and at the time you press +-*/ keys change the values if you send me your email adderss I can send you a fully functionable code for calculater.

My email address is saliya _sw@ yahoo.com

Hi
My name Is vihanga, I am from Sri Lanka, Studing at Westly College, Colombo.
your main problem is the declaring variable . you declare it as double and trying to assign text to is so just assign numeric value and at the time you press +-*/ keys change the values if you send me your email adderss I can send you a fully functionable code for calculater.
My email address is saliya _sw@ yahoo.com

OP asked for help about declaring global variables and he/she did get a few answers to that question and I believe he/she is happy with those answers.

He/she did not ask for calculator code! The OP could have easily googled the code if he/she needed that!

DaniWeb has also PM functionality, so you can directly contact a person. Did you know that? I don't see your point of putting an email to public and offering code via email?! Does it come with some pre-compiled "test" executable perhaps?

This article has been dead for over six months. Start a new discussion instead.