0

I have a small array of movies which shows various information about each movie, including the start time and the running time. I want to add new movies to the listings through user input (by filling the details into textboxes and pressing a button), and I am able to add the name, genre and age rating, but I am struggling to add the times.

My array code is as follows:

List<Movies> movies = new List<Movies>();
InitializeArrayList();
public void InitializeArrayList()
        {
            movies.Add(new Movies("The Avengers", "Action", "PG-13", 18, 05, 3, 15));
            movies.Add(new Movies("Inside Out", "Adventure", "PG", 16, 25, 1, 52));
            movies.Add(new Movies("Jurassic World", "Action/Adventure", "PG-13", 20, 10, 2, 04));
        }

So the time is read in the format of two ints: one for the hour and one for the minutes. It prints in the following format:

18:05

The following is my code snippet for adding:

private void button6_Click(object sender, EventArgs e)
        {
            movies.Add(new Movies(textBox1.Text, textBox2.Text, textBox3.Text, Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox4.Text), Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox5.Text), 
            this.DisplayData();
        }

In this part, DisplayData() prints all the information into the textboxes to view the array. I know this is not correct, but I'm not sure how to fix it. Does anyone know how I can get it to read two integers from the textbox and print it out?

3
  • 1
    Int32.TryParse is your friend Commented Nov 17, 2015 at 22:34
  • Thanks for the response; I just looked into it but I'm not fully sure how to implement it... I'm a bit of a newbie with C# ! Commented Nov 17, 2015 at 22:53
  • I think that Steve missed the point that you want 2 integers from the same textbox. Commented Nov 17, 2015 at 23:02

2 Answers 2

1

I'm not sure if this is the best way, but it's the first that comes to my mind.
If you know that you'll always use the same character to separate hours and minutes, in your example ":", I would read the input string, split it to an array, then parse both strings in the array.

private void button6_Click(object sender, EventArgs e)
{
    string[] runtime = textBox4.Text.Split(':');
    int hours, minutes = 0;
    if (!int.TryParse(runtime[0], out hours) || !int.TryParse(runtime[1], out minutes))
    {
        //handle exception
    }
     movies.Add(new Movies(textBox1.Text, textBox2.Text, textBox3.Text, hours, minutes, Convert.ToInt32(textBox5.Text), Convert.ToInt32(textBox5.Text), 
    this.DisplayData();
}

You should replace other Convert.ToInt32 with int.Parse or int.TryParse also.

Sign up to request clarification or add additional context in comments.

Comments

0

You can split and parse the string explicitly, and that will work "fine". You may find that the code becomes harder to maintain if you are dealing with different cultures where time formatting is done differently.

IMHO a somewhat better alternative would be to recognize that what's really in the TextBox is a time value, and let .NET parse that for you. You can extract the important pieces as needed:

private void button6_Click(object sender, EventArgs e)
{
    DateTime showTime = DateTime.Parse(textBox4.Text);
    TimeSpan duration = TimeSpan.Parse(textBox5.Text);

    movies.Add(new Movies(textBox1.Text, textBox2.Text, textBox3.Text,
        showTime.Hour, showTime.Minute, duration.Hours, duration.Minutes, 
    this.DisplayData();
}

IMHO this also makes the code a lot more readable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.