0

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 application

Screenshot showing what happens when i click 'get dogs' button

Screenshot showing what happens when i click 'get dogs' button

Screenshot showing what happens when i click 'create 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 ";
    }
}
dymanoid
  • 14,771
  • 4
  • 36
  • 64
BingoDingo
  • 71
  • 7
  • 8
    Please don't post **all** the code you have. Provide only a [mcve] (**minimal** is important). – dymanoid Dec 06 '18 at 17:03
  • How does a `Dog` know about `Form1` when you don't pass it in as a parameters to any `Dog` function? – Neil Dec 06 '18 at 17:04
  • 2
    In `createDogsButton_Click` you have`Dog newDog = new Dog(.....` But you don't seem to be doing anything with the newly created dog. – 001 Dec 06 '18 at 17:09
  • 2
    The `DogChorus` class needs a `List` and you need to add to that when you create a new dog. – 001 Dec 06 '18 at 17:11
  • IMO the designer code can be removed from the question. It doesn't add anything. –  Dec 06 '18 at 17:13
  • @BingoDingo, you might want to read up on [C# Collections](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/collections) – 001 Dec 06 '18 at 17:26
  • @ekad Third time I had to write this. There is only one form. – LarsTech Dec 06 '18 at 20:21
  • This has nothing to do with passing data between forms. Johnny Mopp has already identified the issue. He creates newDog, and does nothing with it. He calls a function on DogChorus dog, but hasn't done anything with newDog, so DogChorus dog has no idea that newDog exists. – Aaron Dec 06 '18 at 20:26

1 Answers1

0

I did not read all of your code, based on the subject of your question i think you need to pass an instance of Form1 (via constructor) to your business logic classes.
Please see my comments inside the code in the example below:

Form1:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    // global variable of Dogs Class instance - pass it a Form1 instance
    public Dogs _Dogs;

    private void Form1_Load(object sender, EventArgs e)
    {
        // initilize Dogs class and pass this Form1 instance
        _Dogs = new Dogs(this);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // show textbox1 text from Dogs class
        _Dogs.showTextBox1Text();
        // you can also just pass the value of textbox1 to Dogs instance
        _Dogs.passedTextFromForm1(textBox1.Text);
    }
}

Dogs Class:

public class Dogs
{
    // global variable to hold form1
    public Form1 MyForm1;
    public Dogs(Form1 form1Istance)
    {
        this.MyForm1 = form1Istance;
    }

    public void showTextBox1Text()
    {
         // do not forget to set the access modifier of TextBox1 to public (you can do it in the Designer, the propery is "Modifier")
        System.Windows.Forms.MessageBox.Show("showTextBox1Text(): " + this.MyForm1.textBox1.Text);
    }

    public void passedTextFromForm1(string txtfromtextbox1)
    {
        // another way to share data between instances
        System.Windows.Forms.MessageBox.Show("passedTextFromForm1(): " + txtfromtextbox1);
    }
}

Access modifier of the TextBox control can be changed here (to public in order to share it with other classes):

enter image description here

Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
  • Ugh, a class should probably never know about the form. – LarsTech Dec 06 '18 at 20:08
  • @LarsTech that is why passedTextFromForm1() method is here – Jonathan Applebaum Dec 06 '18 at 20:12
  • 1
    Your Dogs class has a form reference. This is spaghetti code. What if the user wants to use the Dogs class in a web project? – LarsTech Dec 06 '18 at 20:15
  • I understand what you are saying but this is Winforms, also the question is: "class cannot see textboxes in another class" . And I honestly would love to see a better example regarding this specific question... – Jonathan Applebaum Dec 06 '18 at 20:19
  • 1
    What if the user wants to use the Dogs class on Form2? The user is asking the wrong question here. You need to teach them to pass the required "data" the class needs in order to perform its purpose. Passing the form is just the wrong thing to teach. – LarsTech Dec 06 '18 at 20:24
  • I don't want to get into a pedagogical argument but Based on OP`s code i think the opposite, i think he should learn about the functionality of instances, for the sake of teaching. maybe i am wrong but that was exactly my intention by giving this solution to this specific question. It's OK if you disagree and, of course, would love to see another example – Jonathan Applebaum Dec 06 '18 at 20:34