0

Umm, i'm creating some subscription form and the max is 5 subscriptions.

So, i have a class Member and in the class i'm using the Get&Set method thingy(i've finally sorta learnt this get&set thing haha &i hope i'm using it correctly).

private string name;
private string address;        
public string Name { get { return name; } set { name = value; } }
public string Address { get { return address; } set { address = value; } } 

In the class form

Member[] memberArray = new Member[5];
Member memberSub = new Member();

BtnAdd_Click

memberSub.Name = TbName.Text;
memberSub.Address = TbAddress.Text;
for (int i = 0; i < memberArray.Length; i++)
{
    if (memberArray[i] == null)
    {
        memberArray[i] = memberSub;
        MessageBox.Show(TbName.Text + " has been added as a subscriber.");
        break;
    }

}

When i click display, it'll show the last input, and the front inputs are lost, idk why it's happening and there is an error? i think it's cause of the null array data, but i'm not sure.

Unless there is something wrong with my display button codes..?

for (int i = 0; i < memberArray.Length; i++)
{
    rtbDisplay.Text += "Name: " + memberArray[i].Name + Environment.NewLine
             + "Address: " + memberArray[i].Address + Environment.NewLine
             "----------------------------------------------------"
             + Environment.NewLine;
} 
Tzah Mama
  • 1,547
  • 1
  • 13
  • 25
Marceee
  • 61
  • 1
  • 8
  • possible duplicate of [List overwriting data on all positions](http://stackoverflow.com/questions/2744489/list-overwriting-data-on-all-positions) – Eugene Podskal Jun 29 '14 at 10:02

2 Answers2

2

you are changing one object state and passing it to all of the array items,so at last you will only have the last entered input in all array item because they are all referring to single object memberSub and other array items also have memberSub reference so they will display same result,you need to instantiate each object of array separate and then set their properties like this:

 for (int i = 0; i < memberArray.Length; i++)
 {
  if (memberArray[i] == null)
  {
   memberArray[i] = new Member();
   memberArray[i].Name = TbName.Text;
   memberArray[i].Address = TbAddress.Text;
   MessageBox.Show(TbName.Text + " has been added as a subscriber.");
   break;
  }

 }
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
1

You are only using one Member instance, so you will end up with an array full of references to the same instance. When you assign the object to the array, it's just the reference that is stored, it doesn't create a copy of the object to store in the array.

Instead of using a single Member instance, create a new instance each time that you want to store it in the array.

Just declare a reference for the object:

Member memberSub;

Then when you need to use one, create a new one:

memberSub = new Member();
memberSub.Name = TbName.Text;
memberSub.Address = TbAddress.Text;

You can consider using a list instead of an array:

List<Member> memberList = new List<Member>(5);

It's easier to add items to a list. You just have to keep track of the length of the list. Something like:

if (memberList.Count < 5) {
  memberList.Add(memberSub);
} else {
  MessageBox.Show("No more members allowed.");
}

When you display the members, just loop through all the items in the list. As there are no empty items in the list you don't need to skip any items. You can access the items in the list just as you access the items in an array. The only change is that the size of the list is in the Count property where you use Length for the array:

for (int i = 0; i < memberArray.Count; i++) {
  ...
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Right. This also works thanks!! But just clarifying, using `Member memberSub = new Member();` means that i'm only creating one instance? Which was why it overwrote the prev input? and `Member memberSub;` means there's no single instance so it adds on? – Marceee Jun 29 '14 at 10:24
  • oh. sorry. i just re read your explanation. hahaha i get it~ thanksss – Marceee Jun 29 '14 at 10:25
  • @EhsanSajjad: An array will work just fine, but a list fits better for a growing collection of items. If you use an array you have a mix of used and unused items in it to consider, with a list you only have the used items. – Guffa Jun 29 '14 at 13:51