Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am fairly new to C# and am having trouble understanding certain aspects of classes. For instance, I have the following code:

public partial class Budget : UserControl
    {
        public Budget ()                //
        {
            InitializeComponent();      // Constructor
        }                               //
    }

    public class BudgetClass
    {    
        public BudgetClass(string category, long dollars)     // Constructor
        {                                                     // with 
            this.category = category;                         // values
            this.dollars = dollars;                           // I am 
        }                                                     // supplying.

        public string category { get; set; }       // Very confused here.
        public long dollars { get; set; }          // Not sure what this is doing??
    }

    public class ProposedBudgetCollection : Collection<BudgetClass>
    {
        public ProposedBudgetCollection()
        {
            Add(new BudgetClass("Total Personal Income", 0 )); // Adding objects
            Add(new BudgetClass("Net Salary", 5));             // with supplied
            Add(new BudgetClass("Bonuses", 5));                // data into List.
            Add(new BudgetClass("Money", 100));                //
        }
    }
}


Can anyone explain to me the "walkthrough" in plain English of what is going on here? More to the point, I am confused as to what is going on in the List. It seems that I am instantiating a new object of BudgetClass and placing the values that I am supplying into that object and then adding it to a list.

My ultimate goal is to learn how to replace the absolute data I am passing to the List with user input from a text field in a separate window. I can pass those values into my class easily enough, but I am having a very hard time adding them to the List. I feel that I need to understand this part before I can continue. Can anyone please help me?
Posted

First thing you understand is reference types vs value types. As BudgetClass (by the way, don't you think that creating a class under the name containing the word "Class", as well as collection class containing the word "Collection" is nor very reasonable, because of apparent tautology?) is a class, that is, a reference class, your collection is a collection of reference object, and the "real" objects are accessed through these references. So, when you obtain a reference from a collection (say, by index) and modify the object, you only modify what a reference points to, not the reference itself. This way, you modify the properties of some object accessible through the collection, but don't modify the collection itself. This is what you want. You replace the data pointed by the same references.

Of course, you can simply add a brand new object and remove some other object. "Removing" simply means removing its reference from the collection and "forgetting" it. The object becomes unreachable and due to this, will eventually destroyed by Garbage Collector (GC), and its memory will be reclaimed.

—SA
 
Share this answer
 
Comments
Abhinav S 3-Nov-12 0:50am    
5!
Sergey Alexandrovich Kryukov 3-Nov-12 18:11pm    
Thank you, Abhinav.
--SA
I would suggest working through .NET Book Zero[^] for some excellent basic help on these features of the language. You can also try the C# Tutorials[^] provided by Microsoft.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900