I am trying to write a small program to keep track of my movie collection but I can't seem to get the streamwriter(I think thats what its called to work). Right now I am working on writing to a text file. I had a couple of questions though, will I have to read from the text file after I write to it to keep track of the movie collection? The list will only hold information added at runtime and once the program is closed it won't save the information right? hence the need for the text file? the text file will save the info from runtime to runtime correct? I don't know where I am going wrong so could someone explain it to me? I would really appreciate it. I'm planning to work on the reading part but an example would help. I will appreciate any help. here is my code.

namespace WindowsFormsApplication1
{
    public partial class frmMovies : Form
    {
        private List<muvie> Movies = new List<muvie>();
        public frmMovies()
        {
            InitializeComponent();
        }
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            
            muvie newmovie = new muvie()
            {
            newmovie = txtNew.Text.Trim(),

        };

            Movies.Add(newmovie);
            lbMovie.Items.Add(newmovie);
            Movies.Sort();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            
        }
        class Text
        {
            static void Main()
            {
                TextWriter tw = new StreamWriter("Movie.txt");
                tw.WriteLine(newmovie);
                tw.Close();
            }
        }
        class muvie
        {
            public string newmovie 
            { get; set;}
            public override string ToString()
            {
                return newmovie;
            }
        }
    }
}

Recommended Answers

All 10 Replies

I am trying to write a small program to keep track of my movie collection but I can't seem to get the streamwriter(I think thats what its called to work). Right now I am working on writing to a text file.

What is or is not happening?

I had a couple of questions though, will I have to read from the text file after I write to it to keep track of the movie collection? The list will only hold information added at runtime and once the program is closed it won't save the information right? hence the need for the text file? the text file will save the info from runtime to runtime correct?

You are correct. The text file is so you don't have to reenter all your data each time (or you could use an xml file, a database, etc.).

I have an error " the name 'newmovie' does not exist in the current context', so I haven't even got to run the program yet. Do I need to change someting in the Text Class and then call that in the add button event handler or something? I don't see how having the tw.writeline(newmovie); in the class is going to do anything because it isn't in the add event handler but I am newbie programmer and have never worked with streamwriter or reader.

That's not your problem. There is no variable named 'newmovie' that is in scope in your main method. Did you mean to iterate through your Movie collection and write that data?

Well what I want to do is be able to add my movies to a text file through the program but, I want them to be in alphabetical order. So I can print the list later. I was playing around and actually wrote to movie.txt with this

StreamWriter MovieWrite = new StreamWriter("Movie.txt");

but, will that method allow me to sort the movies or do I need to add the movies to the list then write the data from the list to the text file? which kinda looks like you thought thats where I was headed right? I'm really not sure so if you could suggest the best method, it would help.

So the variable in class muvie isn't class level?(if I have my scopes right) which would mean newmovie isn't available to all methods and functions right?

Maybe this will help you

class FakeClass {
    // Class variable
    // Always available. Since it's static, you access through the class itself,
    // not an instance of the class:
    // FakeClass.staticValue = 3;
    static int staticValue;

    // Instance variable
    // Available throughout the instance of the class.
    // FakeClass myInstance = new FakeClass();
    // myInstance.classValue = 4;
    int classValue; // Only available when you have an instance of the class

    // Method variable
    // Only available in the method in which it is declared
    public MyMethod() {
        int methodValue = 4;
    }

    // Others:
    public MyMethod() {
        int otherValue = 5;
        while (otherValue != 7) {
            int counter = 2; // only available in the while loop
            otherValue = otherValue + counter;  // We can 'reach' out of the while
                                                // block to access otherValue
        }
        counter = 19;  // ERROR! counter is no longer in scope since we are outside
                       // the while block
    }
}

So basically the muvie class is out of scope for my functions in my program right? If I am understanding your example correctly? Could you suggest a good way to accomplish what I am trying to? I am willing to work at it myself but I just want to do it the right way rather than what I think is best, which could could be fubar. should I go with the class streamreaders or the local ones? All I have to look to for advice is my C# and VB for dummies books, a lot of the stuff you find online is outdated. Thank you for the help though.

Why am I having such hard time wrapping my head around these programming concepts? could someone put this example in even more laymans terms please. I know thats probably a really basic example but I am still lost. in Momeraths example he never uses static value or class value or does he? is that what the method does?

why am I throwing an exception with the Movies.Sort();? when I add a second movie to the list it throws "failed to compare two elements in the array".

To use the build in sort your objects must implement the interface IComparable, and your class muvie does not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.