0

I'm a little bit lost right now among objects and references. I'm building a small project with some different classes and winForms.

This is a short description of my project: I have a MainForm and when i click 'add new movie' button, the MovieForm opens where I enter information about a movie. When I click the 'save' button I create a NewMovie object of the information in MovieForm.

Next step is to save this NewMovie object to a file. And it's here I'm a bit lost in how to grab the data from this NewMovie object somewhere else from another class, like the MovieManager from where I then use an object of the FileManager to save the data?

In the MainForm I have this code to detect when the button 'add new movie' is clicked in the MovieForm:

MovieForm movieForm = new MovieForm();
if (movieForm.ShowDialog() == DialogResult.OK)
{
Do something here?
}

Could I reach or pass the new MovieForm object here? How do I do then? In my project I also have a MovieManager. One way is to pass the object to that class? Should I create an object of MovieManager in MovieForm and pass the data that way after I have created the NewMovie object?

Preciate some help and ideas! Thanks!

3D-kreativ
  • 9,053
  • 37
  • 102
  • 159

3 Answers3

2

After ShowDialog has finished, the reference to movieForm is still valid. Thus, you can create a public property in your MovieForm:

class MovieForm {
    ...
    public NewMovie Result { get; private set; }
    ...
}

You set this value in your MoveForm (when the form is closed or when some Save button is pushed), and then you can read it in your Main Form and pass it to your MovieManager:

MovieForm movieForm = new MovieForm();      
if (movieForm.ShowDialog() == DialogResult.OK)      
{      
    NewMovie newMovie = movieForm.Result;
    myMovieManager.CreateNewMovie(newMovie);
}      
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Hi, I get an error message of the properties in MovieForm: Inconsistent accessibility: property type 'MyMovies.NewMovie' is less accessible than property 'MyMovies.MovieForm.Result' ?? – 3D-kreativ Jun 01 '12 at 07:47
  • @3D-kreativ: Either change the property from `public` to `internal`, or put `public` in front of the `class NewMovie` declaration. – Heinzi Jun 01 '12 at 09:10
  • And know it all work, but there still a lot of work left to be done, but this was the critical part. From here I guess it's a little bit easier, now when I can reach the data. But why do you use the line: NewMovie newMovie = movieForm.Result; ? How does it work? I used just this line: movieManager.AddMovie(movieForm.Result); Is there a some major difference? Thanks for the help! :) – 3D-kreativ Jun 01 '12 at 10:20
  • @3D-kreativ: Your line is perfectly fine (and it's what I would use). I just used two lines to make the example as simple as possible. – Heinzi Jun 01 '12 at 10:24
0

Create a property in your MovieForm Class and pass its value while instance creation.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

You should return newly created instance of Movie from the MovieForm and store it in the manager.

public class MovieManager
{
  List<Movie> movies;

  public void AddMovie(Movie movie)
  {
    movies.Add(movie);
  }

  public Save()
  {
  }
}

public class Movie
{
  public string Name;
}

public class MainForm
{
  MovieManager manager;

  private void NewMovieClick(...)
  {
     using(var form = new MovieForm())
     {
       if(form.ShowDialog(this) == DialogResult.OK)
       {
         manager.Add(from.Movie);
       }
     }
  }
}

public class MovieForm
{
  public Movie Movie;
}
Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42