Click here to Skip to main content
14,265,562 members
   

C#

 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:44
memberBrian_TheLion29-May-19 14:44 
GeneralRe: C# using get set. Why is this failing? Pin
Dave Kreskowiak29-May-19 16:26
mveDave Kreskowiak29-May-19 16:26 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 18:23
memberBrian_TheLion29-May-19 18:23 
GeneralRe: C# using get set. Why is this failing? Pin
lmoelleb2-Jun-19 22:46
memberlmoelleb2-Jun-19 22:46 
AnswerRe: C# using get set. Why is this failing? Pin
OriginalGriff28-May-19 20:41
protectorOriginalGriff28-May-19 20:41 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 0:36
memberBrian_TheLion29-May-19 0:36 
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 0:43
protectorPete O'Hanlon29-May-19 0:43 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:41
memberBrian_TheLion29-May-19 14:41 
Hi Griff.

Your change worked when there was a test for the value of 20 but when I changed the test value to 30 then 0 was displayed on the form for the amount of gold, then when I clicked on the Test button the gold value changed to 35.

What I'm aiming at on this test is for 20 to first to appear on the form for the Gold value after the program is run, then when I press the Test button the gold value should change to 30 and in the test have 5 added to the 30 giving a gold value of 35.

Here is my code
On the main program I have

SuperAdventure.cs

<pre lang="c#"></pre>
public partial class SuperAdventure : Form
{
private Player _player;

public SuperAdventure()
{
InitializeComponent();

Location location = new Location(1, "Home", "This is your house.");


_player = new Player(10, 10, 20, 0, 1);

Update();

}

// TEST Button on Form
private void btnTest_Click(object sender, EventArgs e)
{
_player.Gold = 30;

Update();
}
public void Update()
{
lblHitPoints.Text = _player.CurrentHitPoints.ToString();
lblGold.Text = _player.Gold.ToString();
lblExperience.Text = _player.ExperiencePoints.ToString();
lblLevel.Text = _player.Level.ToString();
}
}
}



Player.cs code

namespace Engine
{
public class Player
{
// This is needed for _player = new Player(10, 10, 20, 0, 1,);
public int CurrentHitPoints { get; set; }
public int MaximumHitPoints { get; set; }
private int _Gold = 0;
public int Gold
{
get
{
return _Gold;
}

set
{
if (value == 30)
{
value += 5;
_Gold = value;
}
}
}
public int ExperiencePoints { get; set; }
public int Level { get; set; }

//In order to use lists – we need one variable or property to hold a collection of objects that are the same class
public List<InventoryItem> Inventory { get; set; }
public List<PlayerQuest> Quests { get; set; }


// This is needed for lblHitPoints.Text = _player.CurrentHitPoints.ToString() etc;

public Player(int currentHitPoints, int maximumHitPoints, int gold, int experiencePoints, int level)// : base(currentHitPoints, maximumHitPoints)
{
CurrentHitPoints = currentHitPoints;
MaximumHitPoints = maximumHitPoints;
Gold = gold;
ExperiencePoints = experiencePoints;
Level = level;

Inventory = new List<InventoryItem>();
Quests = new List<PlayerQuest>();


}
}
}
</pre>
GeneralRe: C# using get set. Why is this failing? Pin
Pete O'Hanlon29-May-19 18:57
protectorPete O'Hanlon29-May-19 18:57 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 19:55
memberBrian_TheLion29-May-19 19:55 
GeneralRe: C# using get set. Why is this failing? Pin
OriginalGriff29-May-19 1:11
protectorOriginalGriff29-May-19 1:11 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 3:30
mveBillWoodruff29-May-19 3:30 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 20:14
memberBrian_TheLion29-May-19 20:14 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff29-May-19 22:48
mveBillWoodruff29-May-19 22:48 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion30-May-19 1:20
memberBrian_TheLion30-May-19 1:20 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff30-May-19 4:33
mveBillWoodruff30-May-19 4:33 
GeneralRe: C# using get set. Why is this failing? Pin
BillWoodruff31-May-19 20:12
mveBillWoodruff31-May-19 20:12 
AnswerRe: C# using get set. Why is this failing? Pin
Gerry Schmitz29-May-19 3:52
mveGerry Schmitz29-May-19 3:52 
GeneralRe: C# using get set. Why is this failing? Pin
Brian_TheLion29-May-19 14:52
memberBrian_TheLion29-May-19 14:52 
QuestionWinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Ken Guru28-May-19 4:46
memberKen Guru28-May-19 4:46 
AnswerRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Eddy Vluggen28-May-19 5:42
mveEddy Vluggen28-May-19 5:42 
GeneralRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Ken Guru28-May-19 22:00
memberKen Guru28-May-19 22:00 
GeneralRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Eddy Vluggen28-May-19 23:30
mveEddy Vluggen28-May-19 23:30 
GeneralRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
Ken Guru29-May-19 4:24
memberKen Guru29-May-19 4:24 
AnswerRe: WinForms, Window with FormBorderStyle.None and Taskbar moved doesn't adjust to new size Pin
BillWoodruff28-May-19 15:52
mveBillWoodruff28-May-19 15:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.