我试图通过文本框将新变量设置为我的Filed(实例)值为新值...但我不知道如何制作它...这是我的代码希望我能从别人那里获得一些见解。
private string barkSound;
private string breed;
private int dogHeight;
private string dogColour;
private static int noOfLegs;
所有get和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";
}
答案 0 :(得分:2)
好的,我创建了一个像你一样的表单。在创建按钮我创建了新的Dog
。
这是我的Dog
课程,
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(); } }
}
这是“创建”按钮的点击(我有Dog
列表,可以添加狗狗);
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);
}
在GetDogs Button的Click
我循环所有的狗并将它们添加到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);
}
}
问题不明确,也许你想改变狗的现有价值。
首先,我们需要在Dog
类进行更改。
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(); } }
}
为什么我们这样做?
在listbox's selected index change event
,首先我们需要使用Dog
在列表中找到ElementAt()
。然后检查BarkSound的文本框,如果它不为空,则将其设置为现有的狗BarkingSound
,
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
Dog TempDog = dogs.ElementAt(listBox1.SelectedIndex);
TempDog.BarkingSound = textBox1.Text;
}
}
试图用两种方式解释,希望其中一个对你有用。
答案 1 :(得分:0)
您的问题是您的字段包含private
修饰符。这意味着,Dog
类之外的另一个代码无法修改您的字段。我还建议使用自动生成的属性:
SomeProperty { get; set; }
因此,如果要在类外的任何地方修改字段,属性或方法,则应使用public
修饰符声明它们。在你的情况下:
public string BarkSound;
public string Breed;
public int DogHeight;
public string DogColour;
public static int NoOfLegs;
创建Dog
对象(Dog dog = new Dog();
)后,您可以更改以下属性:
dog.BarkSound = barkSoundTextBox.Text;