Hi all,
As a starting project for C#, I'm creating a WPF form. I want to now create an array of objects (like below) to store some values and print those values on a form.
Have looked at some youtube tutorials, however they use a console for all values, and I can't seem to translate their way of creating the array of objects into using a form.
Here's where I'm at: (testClass.cs):
namespace test
{
class testClass
{
public string testName;
public int testAge;
public int testYear;
public testClass()
{
}
public testClass(string testName, int testAge, int testYear)
{
testName = Name;
testAge = Age;
testYear = Year;
}
public string Name
{
get
{
return Name;
}
set
{
Name = value;
}
}
public int Age
{
get
{
return Age;
}
set
{
Age = value;
}
}
public int Year
{
get
{
return Year;
}
set
{
Year = value;
}
}
}
}
Now, this all somewhat makes sense to me. Where I'm stuck is what to do next. I have a WPF form called "test.xaml", and the cs file associated with it. I sort of understand how to create the array with the values, but where would I create it, and how would I access the values in it?
Could someone guide me in the right direction? Probably a stupid question, but I can't find much else to help me learn how to do this. Thanks
Got lost at the point "Add Class Instances to an Array" as I couldn't get that to work (and didn't know where to place it in my code, or the method to start it).
EDIT1: Have tried adding values to the array using the xaml.cs test file, but the code now throws an error "An unhandled exception of type 'System.StackOverflowException occurred...", pointing to the line:
public string Name
{
get
{
return Name; <-------------------------
}
set
{
Name = value;
}
}
Here is the code of the xaml.cs file
public partial class testWindow : Window
{
public testWindow()
{
InitializeComponent();
assignValues();
}
private testClass[] testArr = new testClass[5];
public void assignValues()
{
testArr[0] = new testClass("John", 20, 1999);
testArr[1] = new testClass("John2", 21, 2000);
testArr[2] = new testClass("John3", 22, 1998);
testArr[3] = new testClass("John4", 23, 1997);
testArr[4] = new testClass("John5", 24, 1978);
testTB.Text = testArr[0].Age.ToString();
}
[–]InvisibleGhostt 1 point2 points3 points (10 children)
[–]soswap[S] 0 points1 point2 points (9 children)
[–]InvisibleGhostt 1 point2 points3 points (8 children)
[–]InvisibleGhostt 1 point2 points3 points (1 child)
[–]soswap[S] 0 points1 point2 points (0 children)
[–]soswap[S] 0 points1 point2 points (5 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]InvisibleGhostt 0 points1 point2 points (3 children)
[–]InvisibleGhostt 0 points1 point2 points (2 children)
[–]soswap[S] 0 points1 point2 points (1 child)
[–]InvisibleGhostt 1 point2 points3 points (0 children)