In the code below, I'M having trouble with one line of code
MenuMaker menu = new MenuMaker() { Randomizer = new Random() };
The program is a simple windows Form with two main files, Form1.cs & MenuMaker.cs, the second one is a class.
What I'M trying to understand is why is the line of code above in the Form1.cs file instead of the MenuMaker.cs class where the rest of my fields are at, including the creation of my Random instance. The two files are below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DiscountSandwiches
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
MenuMaker menu = new MenuMaker() { Randomizer = new Random() };
label1.Text = menu.GetMenuItem();
label2.Text = menu.GetMenuItem();
label3.Text = menu.GetMenuItem();
label4.Text = menu.GetMenuItem();
label5.Text = menu.GetMenuItem();
label6.Text = menu.GetMenuItem();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DiscountSandwiches
{
public class MenuMaker
{
// fields
public Random Randomizer;
string[] Meats = { "Roast beef", "Salami", "Turkey", "Ham", "Pastrami" };
string[] Condiments = { "yellow mustard", "brown mustard", "honey mustard",
"mayo", "relish", "french dressing" };
string[] Breads = { "rye", "white", "wheat", "pumpernickel", "itelian bread", "a roll" };
// methods
public string GetMenuItem()
{
string randomMeat = Meats[Randomizer.Next(Meats.Length)];
string randomCondiment = Condiments[Randomizer.Next(Condiments.Length)];
string randomBread = Breads[Randomizer.Next(Breads.Length)];
return randomMeat + " with " + randomCondiment + " on " + randomBread;
}
}
}