Textbox to change instance value

2019-09-20 16:18发布

问题:

I'm trying to set new variable into my Filed (instance) value into new value through text box... but I've no idea how to make it.. here is my code wish I can get some insight from someone else.

private string barkSound;
private string breed;
private int dogHeight;
private string dogColour;
private static int noOfLegs;

All the get and set has been set.

public Dog()   
{
    barkSound = "Woof!";
    breed = "cocker spaniel";
    dogHeight = 10;
    dogColour = "white";   
}

public string GetSpeech()
{
    dogSpeech = "Hello. I am a " + breed + ". " + barkSound + "\n I'm " 
                    + (IsBig()? "Big" : "Small") +", i got " + DogHeight +" CM, Colour is " + dogColour + ". I have " + noOfLegs + " Legs" ;
    return dogSpeech;
}

public Dog()
{
    barkSound = "Woof!";
    breed = "cocker spaniel";
    // Question(C) add Constructor for dogHeight. dogColour and breed.
    dogHeight = 10;
    dogColour = "white";
}

回答1:

Okay, I have created an form like you, At the Create button I created new Dog.

Here is my Dog class,

public class Dog
        {
            private string barkSound;
            private string breed;
            private int dogHeight;
            private string dogColour;
            public Dog(string bark, string breed, int height, string color)
            {
                this.barkSound = bark;
                this.breed = breed;
                this.dogHeight = height;
                this.dogColour = color;

            }
            public string BarkingSound { get { return this.barkSound; } }
            public string BreedName { get { return this.breed; } }
            public string Height { get { return this.dogHeight.ToString(); } }
        }

Here is the Create button's click (And I have Dog list to add Dogs to it);

List<Dog> dogs = new List<Dog>();
private void createBtn_Click(object sender, EventArgs e)
        {
            Dog myDog = new Dog(textBox1.Text, textBox2.Text, int.Parse(textBox3.Text), textBox4.Text);
            dogs.Add(myDog);
        }

At GetDogs Button's Click I loop over all dogs and add them into listbox.

 private void button1_Click(object sender, EventArgs e)
        {   listBox1.Items.Clear();
            foreach (var item in dogs)
            {
                string text = "Hello. I am a " + item.BreedName + ". " + item.BarkingSound + "etc";
                listBox1.Items.Add(text);
            }
        }

The question is not clear, maybe you want to change the existing value of the dog.

So first we need to make change at Dog class.

public class Dog{
        private string barkSound;
        private string breed;
        private int dogHeight;
        private string dogColour;
        public Dog(string bark, string breed, int height, string color)
        {
            this.barkSound = bark;
            this.breed = breed;
            this.dogHeight = height;
            this.dogColour = color;

        }
        public string BarkingSound { get { return this.barkSound; }  set { this.barkSound = value; }  } // so we can set variables like this dog.BarkingSound = "bla"
        public string BreedName { get { return this.breed; } }
        public string Height { get { return this.dogHeight.ToString(); } }
    }

Why did we do that ?

At listbox's selected index change event, first we need to find the Dog inside our list by using ElementAt(). Then Check textbox of BarkSound, if it is not empty set it to existing Dog's BarkingSound,

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                Dog TempDog = dogs.ElementAt(listBox1.SelectedIndex);
                TempDog.BarkingSound = textBox1.Text;
            }

        }

Tried to explain with both way, Hope that one of them will be useful for you.



回答2:

Your problem is that your fields have private modifier. This means, that another code outside Dog class can't modify your fields. Also I recommend to use auto-generated properties:

SomeProperty { get; set; }

So, if you want to modify fields, properties or methods everywhere outside your class, you should declare them with public modifier. In your case:

public string BarkSound;
public string Breed;
public int DogHeight;
public string DogColour;
public static int NoOfLegs;

After you have created a Dog object (Dog dog = new Dog();), you can change your properties like this:

dog.BarkSound = barkSoundTextBox.Text;