Question

Movie Class

public class Movie
{
    #region Properties
    public string Name { get { return _name; } set { _name = value; } }
    public string Producer { get { return _producer; } set { _producer = value; } }
    public int Rating { get { return _rating; } }
    public Image Covor { get; set; }
    public string Description { get { return _description; } }
    public int ReleaseYear { get { return _releaseYear; } set { _releaseYear = value; }}
    #endregion

    #region Private Fields
    private string _name;
    private string _producer;
    private int _rating;
    private string _description;
    private int _releaseYear;
    #endregion

    #region Constructors
    public Movie()
    {
    }

    public Movie(string name, int yearRelease)
    {
        this._name = name;
        this._releaseYear = yearRelease;
    }

    public Movie(string name, int yearRelease, string producer)
    {
        this._name = name;
        this._releaseYear = yearRelease;
        this._producer = producer;
    }
    #endregion
}   

My attempt

foreach (DataRow movieRow in MovieTable().AsEnumerable())
{
    if (movieRow["Producer"] != DBNull.Value)
    {
        Movie movie = new Movie()
        {
            Name = (string)movieRow["Name"],
            Producer = (string)movieRow["Producer"],
            ReleaseYear = (int)movieRow["Release Year"]
        };
        movieList.Add(movie);
    }
    else
    {
        Movie movie = new Movie()
        {
            Name = (string)movieRow["Name"],
            ReleaseYear = (int)movieRow["Release Year"]
        };
        movieList.Add(movie);
    }
}

This is my code so far I'm trying to convert a Table to a List. The only problem is DBNull's.

I would like to update the entire table to a list, this works currently for 2 situations, but I need for the List to contain all the information if it exists. I could create elseif statements to handle every possible scenario but there has to be a way better way to figure out if the type is DBNull and if not set the property correctly.

If there's any confusion tell me what it is and I'll explain further.

Was it helpful?

Solution

One option is to incorporate the null-check into the set-statements wherever you need them, using shorthand code:

Name = (movieRow["Name"] == DBNull.Value) ? 
              (string)movieRow["Name"] : 
              string.Empty,

// Producer will be given a value if it exists, or null otherwise:
Producer = (movieRow["Producer"] == DBNull.Value) ? 
              (string) movieRow["Producer"] : 
              null,
...

You can replace string.Empty with null or vice versa of course, if that suits you better.


Edit: Just a very basic clarification, since you state you are new to programming: This shorthand notation means "if firstValue is true, return secondValue, otherwise, return thirdValue"

var result = firstValue ? secondValue : thirdValue;

OTHER TIPS

One option: you can check IsNull(column):

    Movie movie = new Movie()
    {
        Name = movieRow.IsNull("Name") 
               ? (string)null : (string)movieRow["Name"],
        Producer = movieRow.IsNull("Producer") 
               ? (string)null : (string)movieRow["Producer"],
        // etc..
    };
    movieList.Add(movie);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top