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";
}
Your problem is that your fields have
private
modifier. This means, that another code outsideDog
class can't modify your fields. Also I recommend to use auto-generated properties:So, if you want to modify fields, properties or methods everywhere outside your class, you should declare them with
public
modifier. In your case:After you have created a
Dog
object (Dog dog = new Dog();
), you can change your properties like this:Okay, I have created an form like you, At the Create button I created new
Dog
.Here is my
Dog
class,Here is the Create button's click (And I have
Dog
list to add Dogs to it);At GetDogs Button's
Click
I loop over all dogs and add them intolistbox
.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.Why did we do that ?
At
listbox's selected index change event
, first we need to find theDog
inside our list by usingElementAt()
. Then Check textbox of BarkSound, if it is not empty set it to existing Dog'sBarkingSound
,Tried to explain with both way, Hope that one of them will be useful for you.