none
Object value overwritten RRS feed

  • Question

  • I have a class with a List.  I'm creating a list of values entered into a textbox.  When I add the first value, the object count is 1 and contains the value entered.  I clear the textbox after each object.  After creating a second object, the object count is 2.  The problem is Object1 value is the same as Object2 value, which they shouldn't be

    Class object = new Class();
    object.SetValue(textbox.text);
    Class.object.Add(object);
    textbox.text = "";

    I have done the same with another class list without issue.  It was working before and I haven't changed the code.  Is this known to happen?  

    Thursday, August 22, 2019 11:59 PM

Answers

All replies

  • Hi RichardDunneBSc,

    Thank you for posting here.

    >>I have a class with a List.

    First, where is your list in your code?

    Second, could you provide SetValue method, which will help us to reproduce your problem.

    Third, are you sure you could run the following code without compile warning?I think the following code exists some problems, please check it.

    object.SetValue(textbox.text);

    Last but not least, I make a simple code that may meet your requirements.

    Code:

     List<int> list = new List<int>();
            int count = 0;
            private void Button1_Click(object sender, EventArgs e)
            {
       
                if(count>=list.Count)
                {
                    MessageBox.Show("done");
                }
                else
                {
                    textBox1.Text = list[count].ToString();
                }
                count++;
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                list.Add(1);
                list.Add(2);
                list.Add(3);
                list.Add(4);
                list.Add(5);
            }

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, August 23, 2019 2:53 AM
    Moderator
  • Greetings Richard.

    I would just like to add to Jack's request for extra code and also to say that "Class" is a shithouse name for a class, and "object" is an even worse name for an object.

    It looks like Class.object (as opposed to the object "Class object" declared just above it) is static, which might have something to do with your problem.

    You really manage to pack maximum confusion into just three or four lines of code.


    Friday, August 23, 2019 3:05 AM
  • Greetings again Richard.

    I couldn't resist the challenge of trying to work out what the hell you're trying to do and what might have gone wrong (sorry, but it really is confusing). Would I be correct in guessing you are using the keyword "static" a bit too often?

    Something like this?

    using System;
    using System.IO;
    using System.Collections.Generic;
    
    
    namespace ConsoleApp
    {
       class Program
       {
          static void Main(string[] args)
          {
             MyClass mc1 = new MyClass();
             mc1.SetValue("ABC");
             MyClass.MyClassList.Add(mc1);
    
             MyClass mc2 = new MyClass();
             mc2.SetValue("XYZ");
             MyClass.MyClassList.Add(mc2);
    
             foreach (MyClass mc in MyClass.MyClassList)
             {
                Console.WriteLine(mc.GetValue());
             }
          }
       }
    
       public class MyClass
       {
          public static List<MyClass> MyClassList { get; } = new List<MyClass>();
    
          // This must NOT be static if we want to be able to set it 
          //differently for each instance.
          static string Value { get; set; }
    
          public void SetValue(string val)
          {
             Value = val;
          }
    
          public string GetValue()
          {
             return Value;
          }
       }
    
       
    }
    

    Friday, August 23, 2019 3:55 AM
  • The problem was of course static.  Removed static from the class member, all good now.  Thanks.
    Friday, August 23, 2019 7:26 AM