I have an application which displays dogs.
"HelloDogs", a 'dog.cs' class cannot see textboxes inside 'form1.cs'
Can someone shed some light on this?
I am trying to create a new dog using user defined variables inputted into a textbox
its as if im trying to use the Dog() constructor in DogChorus, but it wont accept the textbox.Text contents for the arguments for Dog(int, string, string), and cannot see them because theyre in form1.cs
inside the form1.designer.cs i even changed the textBox's to public, but that didnt work either...
Screenshot showing application
Screenshot showing what happens when i click 'get dogs' button
Screenshot showing what happens when i click 'create dogs' button
namespace HelloDogs
There are 4 classes i have to show you; Form1.cs
, Form1.Designer.cs
Dog.cs
and DogChorus.cs
Form1.cs :
public partial class Form1 : Form
{
DogChorus dogs;
DogChorus dog;
public Form1()
{
InitializeComponent();
dogs = new DogChorus();
dog = new DogChorus();
}
private void button1_Click(object sender, EventArgs e)
{
dogDetailsRichTextBox.AppendText(dogs.GetOutput());
}
private void createDogsButton_Click(object sender, EventArgs e)
{
Dog newDog = new Dog(Convert.ToInt32(dogHeightTextBox.Text), dogColourTextBox.Text, dogBreedTextBox.Text);
dogDetailsRichTextBox.AppendText(dog.GetMoreOutput());
}
}
Form1.Designer.cs :
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.dogDetailsRichTextBox = new System.Windows.Forms.RichTextBox();
this.dogBreedTextBox = new System.Windows.Forms.TextBox();
this.dogHeightTextBox = new System.Windows.Forms.TextBox();
this.dogColourTextBox = new System.Windows.Forms.TextBox();
this.dogBarkSoundTextBox = new System.Windows.Forms.TextBox();
this.createDogsButton = new System.Windows.Forms.Button();
this.enterBreedLabel = new System.Windows.Forms.Label();
this.enterDogHeightLabel = new System.Windows.Forms.Label();
this.enterDogColourLabel = new System.Windows.Forms.Label();
this.enterBarkSoundLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(292, 119);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(122, 23);
this.button1.TabIndex = 0;
this.button1.Text = "get dogs";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// dogDetailsRichTextBox
//
this.dogDetailsRichTextBox.Location = new System.Drawing.Point(15, 148);
this.dogDetailsRichTextBox.Name = "dogDetailsRichTextBox";
this.dogDetailsRichTextBox.Size = new System.Drawing.Size(399, 328);
this.dogDetailsRichTextBox.TabIndex = 1;
this.dogDetailsRichTextBox.Text = "";
//
// dogBreedTextBox
//
this.dogBreedTextBox.Location = new System.Drawing.Point(156, 12);
this.dogBreedTextBox.Name = "dogBreedTextBox";
this.dogBreedTextBox.Size = new System.Drawing.Size(258, 20);
this.dogBreedTextBox.TabIndex = 2;
//
// dogHeightTextBox
//
this.dogHeightTextBox.Location = new System.Drawing.Point(156, 39);
this.dogHeightTextBox.Name = "dogHeightTextBox";
this.dogHeightTextBox.Size = new System.Drawing.Size(258, 20);
this.dogHeightTextBox.TabIndex = 3;
//
// dogColourTextBox
//
this.dogColourTextBox.Location = new System.Drawing.Point(156, 66);
this.dogColourTextBox.Name = "dogColourTextBox";
this.dogColourTextBox.Size = new System.Drawing.Size(258, 20);
this.dogColourTextBox.TabIndex = 4;
//
// dogBarkSoundTextBox
//
this.dogBarkSoundTextBox.Location = new System.Drawing.Point(156, 93);
this.dogBarkSoundTextBox.Name = "dogBarkSoundTextBox";
this.dogBarkSoundTextBox.Size = new System.Drawing.Size(258, 20);
this.dogBarkSoundTextBox.TabIndex = 5;
//
// createDogsButton
//
this.createDogsButton.Location = new System.Drawing.Point(156, 119);
this.createDogsButton.Name = "createDogsButton";
this.createDogsButton.Size = new System.Drawing.Size(130, 23);
this.createDogsButton.TabIndex = 6;
this.createDogsButton.Text = "Create Dogs";
this.createDogsButton.UseVisualStyleBackColor = true;
this.createDogsButton.Click += new System.EventHandler(this.createDogsButton_Click);
//
// enterBreedLabel
//
this.enterBreedLabel.AutoSize = true;
this.enterBreedLabel.Location = new System.Drawing.Point(12, 19);
this.enterBreedLabel.Name = "enterBreedLabel";
this.enterBreedLabel.Size = new System.Drawing.Size(116, 13);
this.enterBreedLabel.TabIndex = 7;
this.enterBreedLabel.Text = "Enter the breed of dog:";
//
// enterDogHeightLabel
//
this.enterDogHeightLabel.AutoSize = true;
this.enterDogHeightLabel.Location = new System.Drawing.Point(12, 46);
this.enterDogHeightLabel.Name = "enterDogHeightLabel";
this.enterDogHeightLabel.Size = new System.Drawing.Size(118, 13);
this.enterDogHeightLabel.TabIndex = 8;
this.enterDogHeightLabel.Text = "Enter the height of dog:";
//
// enterDogColourLabel
//
this.enterDogColourLabel.AutoSize = true;
this.enterDogColourLabel.Location = new System.Drawing.Point(12, 73);
this.enterDogColourLabel.Name = "enterDogColourLabel";
this.enterDogColourLabel.Size = new System.Drawing.Size(118, 13);
this.enterDogColourLabel.TabIndex = 9;
this.enterDogColourLabel.Text = "Enter the colour of dog:";
//
// enterBarkSoundLabel
//
this.enterBarkSoundLabel.AutoSize = true;
this.enterBarkSoundLabel.Location = new System.Drawing.Point(12, 96);
this.enterBarkSoundLabel.Name = "enterBarkSoundLabel";
this.enterBarkSoundLabel.Size = new System.Drawing.Size(142, 13);
this.enterBarkSoundLabel.TabIndex = 10;
this.enterBarkSoundLabel.Text = "Enter the bark sound of dog:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(431, 488);
this.Controls.Add(this.enterBarkSoundLabel);
this.Controls.Add(this.enterDogColourLabel);
this.Controls.Add(this.enterDogHeightLabel);
this.Controls.Add(this.enterBreedLabel);
this.Controls.Add(this.createDogsButton);
this.Controls.Add(this.dogBarkSoundTextBox);
this.Controls.Add(this.dogColourTextBox);
this.Controls.Add(this.dogHeightTextBox);
this.Controls.Add(this.dogBreedTextBox);
this.Controls.Add(this.dogDetailsRichTextBox);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox dogDetailsRichTextBox;
public System.Windows.Forms.TextBox dogBreedTextBox;
public System.Windows.Forms.TextBox dogHeightTextBox;
public System.Windows.Forms.TextBox dogColourTextBox;
public System.Windows.Forms.TextBox dogBarkSoundTextBox;
private System.Windows.Forms.Button createDogsButton;
private System.Windows.Forms.Label enterBreedLabel;
private System.Windows.Forms.Label enterDogHeightLabel;
private System.Windows.Forms.Label enterDogColourLabel;
private System.Windows.Forms.Label enterBarkSoundLabel;
}
Dog.cs :
class Dog
{
private string barkSound;
private string breed;
private int dogHeight;
private string dogColour;
private static int noOfLegs;
public string Breed
{
get { return breed; }
set { breed = value; }
}
public int DogHeight { get => dogHeight; set => dogHeight = value; }
public string DogColour { get => dogColour; set => dogColour = value; }
public static int NoOfLegs { get => noOfLegs; set => noOfLegs = value; }
private string dogSpeech;
public Dog()
{
barkSound = "Woof!";
breed = "cocker spaniel";
}
public Dog(int dogHeight, string dogColour, string breed)
{
this.dogHeight = dogHeight;
this.dogColour = dogColour;
this.breed = breed;
}
private bool IsBig(int number)
{
if (number < 50)
{
return false;
}
else
{
return true;
}
}
public string GetSpeech()
{
if (IsBig(dogHeight))
{
dogSpeech = "Hello. I am a " + dogColour + " " + breed + ". I am a big dog, my height is " + dogHeight + " cm. " + barkSound;
}
else
{
dogSpeech = "Hello. I am a " + dogColour + " " + breed + ". I am not a big dog, my height is " + dogHeight + " cm. " + barkSound;
}
return dogSpeech;
}
public void SetSound(String barkSound)
{
this.barkSound = barkSound;
}
}
DogChorus.cs :
class DogChorus
{
Dog lady;
Dog tramp;
Dog chief;
Dog bingo;
Dog newDog;
public DogChorus() {
lady = new Dog();
tramp = new Dog();
tramp.SetSound("Ruff!");
chief = new Dog(55, "blue", "bulldog");
bingo = new Dog(45, "red", "boxer");
newDog = new Dog(0,"","");
}
public string GetOutput(){
return lady.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + tramp.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + chief.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n " + bingo.GetSpeech() + " I have " + Dog.NoOfLegs + " legs \n";
}
public string GetMoreOutput()
{
return newDog.GetSpeech() + " I have " + Dog.NoOfLegs + "legs \n ";
}
}