Good afternoon expert,
Studying C#. Looking at example 2 at
http://download.iticentral.com/DevCentral/pdf/intro_Csharp.pdfUsed this example to create console application which displays the sentence
'The apple varieties ar Macintosh, Granny, and Cortland.' Inane to be sure
but hey, you gotta start somewhere.
I hate console applications so I morphed the console app into a Windows
appl using a button and a label. I kept the public class containing an instance
variable and a contruct with the same name as the class in a separate
file and the two files which do this are callled AppleMain.cs and Apple.cs
below.
Trying to go the next step create a windows app where a button displays the varieties.
I can get it to work as a single file, Apple_Win_Btn.cs, below. Attempted to
create a separate cs file in which a public class called Apple is created and objects
are created like Apple mac = new Apple(Macintosh") and then those objects are referred to
in onClick method like label1.Text = ("The apple varietys are " + Apple.Apple.Mac.Text);
and a couple of other ways but I can't get it to work. This gives me an error
No overload for method 'Apple' takes '1' arguments and
Apple.Apple' does not contain a definition for 'Apple'
How can I separate Apple class and refer to it in the form method?
Thanks.
Allen in Dallas
+++++++++++++AppleMain.cs+
++++++++++
++++
// using apple console application with Windows message box
using System;
using System.Windows.Forms;
namespace Apple
{
class AppleMain
{
// main entry point for application
static void Main( string[] args )
{
//
// TODO: Add code to start application here
//
Apple mac = new Apple("Macintosh");
Apple gra = new Apple("Granny Smith");
Apple cor = new Apple("Cortland");
mac.outputVariety();
gra.outputVariety();
cor.outputVariety();
// MessageBox.Show( "Apple varietys are " + mac.outputVariety() + "," + gra.outputVariety() + "," + cor.outputVariety(), MessageBoxButtons.OK, MessageBoxIcon.Information
);
MessageBox.Show( "The apple varietys are " + mac.outputVariety() + ", " + gra.outputVariety() + ", " + cor.outputVariety(), "Display apple varietys", MessageBoxButtons.OK, MessageBoxIcon.Information
);
} // end Main
} // end class AppleMain
} // end namespace Apple
+++++++++++++Apple.cs+++++
++++++++++
using System;
namespace Apple
{
public class Apple
{
private string variety = "";
public Apple(string appleVariety)
{
this.variety = appleVariety;
}
public string outputVariety()
{
return this.variety;
//System.Console.WriteLine
(variety);
}
}
}
++++++++++++++++++Apple_Wi
n_Btn.cs++
++++++++++
+++++
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace Apple
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Butto
n button1;
public System.Windows.Forms.Label
label1;
private System.ComponentModel.Cont
ainer components = null;
public Form1()
{
// Required for Windows Form Designer support
InitializeComponent();
}
/// Clean up any resources being used.
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Butto
n();
this.label1 = new System.Windows.Forms.Label
();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(80, 16);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(168, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Display apple varieties";
this.button1.Click += new System.EventHandler(this.b
utton1_Cli
ck);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 64);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(272, 24);
this.label1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 110);
this.Controls.Add(this.lab
el1);
this.Controls.Add(this.but
ton1);
this.Name = "Form1";
this.Text = "Display apple varieties";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
public string varietiesMac;
public string varietiesGra;
public string varietiesCor;
private void button1_Click(object sender, System.EventArgs e)
{
varietiesMac = "Macintosh";
varietiesGra = "Granny";
varietiesCor = "Cortland";
//label1.Text = ("The apple varietys are Macintosh, Granny and Cortland");
label1.Text = ("The apple varietys are " + varietiesMac + ", " + varietiesGra + ", " + varietiesCor + ".");
}
}
}