Matt Hintzke
Matt Hintzke

Reputation: 8004

Class persistence in C#

For a class project I am creating an RSS reader in C#. I have classes built for Channels, Feeds, and Articles.

My main class MainView has a List Channels that will hold all the channels.

A Channel is just an organization class to hold feeds. (ie "Sports", "Technology" could be channels). A Channel has a List feeds that holds all the feeds. So if you have a channel, "Sports", and you create an RSS feed for ESPN, then I instantiate a Feed class.

However, I am not sure how to make my Channels List in the MainView class persist across all other classes. When I want to Add a Channel I create a pop-up form class (class addChannel) that allows user input. But in order to access the Channels List in the MainView I have to pass it into the constructor of the addChannel which just copies the List correct? So now when I manipulate the list in the addChannel class, I am not modifying the original right?

I am just so used to C where I can just pass pointers around and modify the original variable directly in memory. So before I continue making my program worst, I want to see if I am doing this all correct or not.

Let me know if there is any specific code you would like me to post.

This code is in my MainView class

 private void addBtn_Click(object sender, EventArgs e)
        {

            addChannel newChannel = new addChannel(channelTree, Channels);
            newChannel.Show();

        }

 public List<Channel> Channels;

And this code is in the addChannel class

private void button1_Click(object sender, EventArgs e)
        {


            // I want to access the channelTree treeView here
            channel = new Channel(this.channelTitle.Text, this.channelDesc.Text);

            // Save the info to an XML doc
            channel.Save();

            // So here is where I want to add the channel to the List, but am not sure if it persists or not
            channels.Add(channel);


            // Now add it to the tree view
            treeView.Nodes.Add(channel.Title);

            this.Close();
        }

Upvotes: 2

Views: 3045

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52675

Assuming you're not resetting MainView.Channels someplace (e.g. this.Channels = new List<Channels>; or this.Channels = GetMeSomeChannels(); then when you call channels.Add(channel); this is adding to same list since both variables reference the same List.

For example the following demo Passes a List<string> to another class. The other class will then add a string to the list. Then that new string is observed by both classes.

using System;
using System.Collections.Generic;

public class Test
{


    public static void Main()
    {
        List<string> Channels = new List<string>() {"a","b", "c"};
        AddChannel ac = new AddChannel(Channels);
                ac.AddSomthing("foo");

                foreach(var s in Channels)
        {
            Console.WriteLine(s);
        }

    }


}
public class AddChannel 
{
        private List<string> Channels {get;set;}
    public AddChannel(List<string> Channels )
        {
        this.Channels = Channels ; 
    }

        public void AddSomthing(string s)
        {
            this.Channels.Add(s);
        }

}

Additional reading

Upvotes: 1

Related Questions