Wrox Programmer Forums
|
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2009, 06:21 PM
Registered User
 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to cashpot
Default Error: Expected class, delegate, or interface struct

Hello,

I am new to programming and attemting to work my way through your course. I am working on chapter 3; Interger Division app,

I figured out why I was getting errors on the "private void" statements. I needed to add the calc code inside the last bracket.

Now the program compiles without errors, and looks great. It just does not work. I can enter values into poerand 1 and operand2 and I can click the calculate button. and nothing happens. Not even the exit button works. The code is "exact" as in the book.

Below is my code:

using System;
using System.Windows.Forms;
publicclassfrmMain : Form
{
privateLabel label2;
privateTextBox txtOperand1;
privateTextBox txtOperand2;
privateTextBox txtResult;
privateButton btnCalc;
privateButton btnExit;
privateButton btnClear;
privateLabel label1;
#region Windows Code
privatevoid InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.txtOperand1 = new System.Windows.Forms.TextBox();
this.txtOperand2 = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.btnCalc = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label1.Location = new System.Drawing.Point(32, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(126, 15);
this.label1.TabIndex = 0;
this.label1.Text = " Enter first interger value:";
//
// label2
//
this.label2.AutoSize = true;
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label2.Location = new System.Drawing.Point(17, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(142, 15);
this.label2.TabIndex = 1;
this.label2.Text = "Enter second interger value:";
//
// txtOperand1
//
this.txtOperand1.Location = new System.Drawing.Point(183, 27);
this.txtOperand1.Name = "txtOperand1";
this.txtOperand1.Size = new System.Drawing.Size(100, 20);
this.txtOperand1.TabIndex = 2;
//
// txtOperand2
//
this.txtOperand2.Location = new System.Drawing.Point(183, 54);
this.txtOperand2.Name = "txtOperand2";
this.txtOperand2.Size = new System.Drawing.Size(100, 20);
this.txtOperand2.TabIndex = 3;
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(28, 93);
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(244, 20);
this.txtResult.TabIndex = 4;
//
// btnCalc
//
this.btnCalc.Location = new System.Drawing.Point(28, 143);
this.btnCalc.Name = "btnCalc";
this.btnCalc.Size = new System.Drawing.Size(75, 23);
this.btnCalc.TabIndex = 5;
this.btnCalc.Text = "Calculate";
this.btnCalc.UseVisualStyleBackColor = true;
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(196, 142);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 23);
this.btnExit.TabIndex = 6;
this.btnExit.Text = "Exit";
this.btnExit.UseVisualStyleBackColor = true;
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(111, 202);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 23);
this.btnClear.TabIndex = 7;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Visible = false;
//
// frmMain
//
this.ClientSize = new System.Drawing.Size(292, 252);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalc);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.txtOperand2);
this.Controls.Add(this.txtOperand1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "frmMain";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public frmMain()
{
InitializeComponent();
}
publicstaticvoid Main()
{
frmMain main = newfrmMain();
Application.Run(main);
}
privatevoid btnCalc_Click(object sender, EventArgs e)
{
bool flag;
float operand1;
float operand2;
float answer;
// Input Step
// Check first input...
flag = float.TryParse(txtOperand1.Text, out operand1);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
txtOperand1.Focus();
return;
}
// Check second input
flag = float.TryParse(txtOperand2.Text, out operand2);
if (flag == false)
{
MessageBox.Show("Enter a whole number", "Input Error");
txtOperand2.Focus();
return;
}
// Process Step
answer = operand1 / operand2;
// Display Step
txtResult.Text = operand1.ToString() + " divided by " +
operand2.ToString() +
" equals " + answer.ToString();
txtResult.Visible =
true;
}
privatevoid btnExit_Click(object sender, EventArgs e)
{
Close();
}
}

Last edited by cashpot; November 5th, 2009 at 12:18 PM.. Reason: I added Help comments for clarity of question
 
Old November 5th, 2009, 04:21 PM
Registered User
 
Join Date: Nov 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to cashpot
Default Where did this code come from?

Hello Dr. Purdum,

My original problem was with the Interger Division prm in chap. 3. I got errors on compile. I figured out that I needed to add the books code before the last curly bracket at the end of the .cs file. Next, I had an issue of program compiling and not working, just looking pretty.

After grinding it out line by line and two program versions later I finally firgured out why I had no action on my program.

It seems that the Template file from chap. 2 creates a #region Windows Code section and it gets populated during the course of building the form ( I am guessing) Anyway, for some reason my application fails to add two lines of code in both the btnCalc section and the btnExit section. They are as follows:

this.btnCalc.Click += new System.EventHandler(this.btnCalc_Click); &
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);

I found this discrepency by going through your code line by line and compairing it to my code.

I am using Visual C# 2008 Express Edition with SP1.

If I am to complete your textbook course, I will need to know why my program fails to insert thee correct code into the templated area of the .cs file.

Thank you in advance for your help.

Best regards,
Christopher
 
Old November 8th, 2009, 08:44 PM
Friend of Wrox
 
Join Date: Sep 2008
Posts: 234
Thanks: 0
Thanked 32 Times in 30 Posts
Default

I'm away from my home on vacation, so I can't check for sure, but did you include the System references as suggested on page 37?
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Main.java:88: 'class' or 'interface' expected samnachilomo Java Basics 1 December 13th, 2006 09:07 AM
class and struct variables are confusing webworldman C# 2 August 7th, 2006 03:42 AM
interface and abstract class sreenu.pocha C# 2 July 26th, 2006 07:18 AM
denotes a 'field' where a 'class' was expected jdang67 C# 1 November 12th, 2004 03:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.