I followed this tutorial to the letter and kept thinking it was something that I did in the code but downloaded the provided zip file and realized that the +, -, *, / buttons don't work as expected right off the bat. Have to hit the C button, then they work just fine. Just a small bug I found in using this.
-DZ
Basic Calculator in C#
#47
Posted 14 July 2011 - 01:38 AM
Ok so I'm a total noob to programming. I followed the instructions, I made the gui buttons. When it came to the next part about adding the "globals" I tryed copy and pasting it at the top of the form's code as said to do I'm not sure if maybe I'm just placing it in the wrong spot but this is the error I'm getting.
Error 1 { expected
Error 2 Invalid token '{' in class, struct, or interface member declaration
and here's the code as it stands.
This my first time posting here so please no flaming. I'm just very new and very eager to learn all that I can. Thanks you for reading and helping me in advance.
Error 1 { expected
Error 2 Invalid token '{' in class, struct, or interface member declaration
and here's the code as it stands.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
//variables to hold operands
private double valHolder1;
private double valHolder2;
//Varible to hold temporary values
private double tmpValue;
//True if "." is use else false
private bool hasDecimal = false;
private bool inputStatus = true;
//variable to hold Operater
private string calcFunc;
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
This my first time posting here so please no flaming. I'm just very new and very eager to learn all that I can. Thanks you for reading and helping me in advance.
#48
Posted 14 July 2011 - 01:32 PM
Hi HighHopes,
The problem is the second curly brace ('{'). That denotes the start of the body of the Form1 class, and all your methods and variables must be within a class. So, all your variable declarations need to be after that curly brace, something like this:
All methods and variables must be within the body of a class like that.
Stick with it though. It's coming up to a year now since I was writing my first ever C# program (other than 'Hello, World!'). Guess what, it was a calculator. Mine was in the console but whatever...Pick up a C# book (this is the one I used to get me started), work through it, keep practising, ask questions in the C# forum when you need extra clarification, and you'll pick it up in no time!
Also, we have an work in progress series of tutorials that will work you through the core aspects of the C# language - Here.
Always remember that however good others seem at programming, they all started exactly where you are now
The problem is the second curly brace ('{'). That denotes the start of the body of the Form1 class, and all your methods and variables must be within a class. So, all your variable declarations need to be after that curly brace, something like this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form { //start of class body
//variables to hold operands
private double valHolder1;
private double valHolder2;
//Varible to hold temporary values
private double tmpValue;
//True if "." is use else false
private bool hasDecimal = false;
private bool inputStatus = true;
//variable to hold Operater
private string calcFunc;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
} //end of class body
}
All methods and variables must be within the body of a class like that.
Stick with it though. It's coming up to a year now since I was writing my first ever C# program (other than 'Hello, World!'). Guess what, it was a calculator. Mine was in the console but whatever...Pick up a C# book (this is the one I used to get me started), work through it, keep practising, ask questions in the C# forum when you need extra clarification, and you'll pick it up in no time!
Also, we have an work in progress series of tutorials that will work you through the core aspects of the C# language - Here.
Always remember that however good others seem at programming, they all started exactly where you are now
This post has been edited by CodingSup3rnatur@l-360: 14 July 2011 - 01:42 PM
|
|






MultiQuote



|