object of the Order class is not being instantiated - c#

Still learning c# and messing with GUI's, I ran into this problem and I have looked at the code over and over again and do not understand why my Order class will not be instantiated. Evrything looks good to me. Is there a basic concept I am missing here?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newOrder = new Order ();
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
private void listBox_SelectedIndexChanged(object sender, EventArgs e)
{
newOrder.Entree = this.listBox.Text;
}
public class Order
{
public string[] pastryEntree = new string[] { "Baklava", "Croissant", "Blueberry Muffin" };
public decimal[] pastryPrice = new decimal[] { 3.00m, 2.50m, 1.75m };
private string entree;
private decimal entreePrice;
public Order()
{
entree = "";
entreePrice = 0;
}
public string Entree
{
get
{
return entree;
}
set
{
entree = value;
SetEntreePrice();
}
}
public decimal EntreePrice
{
get
{
return entreePrice;
}
}
public void SetEntreePrice()
{
for (int i = 0; i < pastryPrice.Length; i++)
{
if (pastryEntree[i] == entree)
{
entreePrice = pastryPrice[i];
}
}
}
}
}
}

You have not defined newOrder as a member of your Form1 class, and so the variable is undeclared when you try to instantiate it.
public partial class Form1 : Form
{
Order newOrder;
....
}

You might want to replace this:
private void Form1_Load(object sender, EventArgs e)
{
newOrder = new Order ();
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
with
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < newOrder.menuEntree.Length; i++)
{
Order newOrder = new Order ();
this.listBox.Items.Add(newOrder.menuEntree[i]);
}
}
It is likely that you want to declare order within the for-loop. Otherwise all the orders would be the same instance.

Related

List is not set value to the control from presenter

I'm trying to make a list of numericUpDown.value and, when I press the button, it sets a random value from the presenter; but, when I press the button nothing happen, I see the value change but I think it just replace the numeric control with a value instead of set value.
The view :
public partial class Form1 : Form, IForm1
{
public List<decimal> _valueList { get; set; }
public Form1()
{
InitializeComponent();
this._valueList = new List<decimal> { numericUpDown1.Value, numericUpDown2.Value, numericUpDown3.Value, numericUpDown4.Value, numericUpDown5.Value, numericUpDown6.Value };
}
public List<decimal> ValueList
{
get => _valueList; set => _valueList = value;
}
public event EventHandler ButtonClick;
private void button1_Click(object sender, EventArgs e)
{
ButtonClick?.Invoke(sender, e);
}
}
Interface:
public interface IForm1
{
List<decimal> ValueList { get; set; }
event EventHandler ButtonClick;
}
Presenter:
public class PresenterForm1
{
private IForm1 _form1;
public PresenterForm1(IForm1 form1)
{
_form1 = form1;
form1.ButtonClick += ButtonClick;
}
private void ButtonClick(object sender, EventArgs e)
{
var random = new Random();
for(int i = 0; i < 6; i++)
{
_form1.ValueList[i] = random.Next(0, 10);
}
}
If I change the type to List<NumericUpDown> it works, but I think this is the wrong way:
foreach (var item in _form1.ValueList)
{
item.Value = random.Next(0, 99);
}

Movie ticket price homework,how to conbine two kind of tickets label's price?

The correct function,should be Lb1SumF PLUS Lb2SumF equal Lb3SumF,but when i input the number of ticket, my program didn't display the correct function of it.
Here's a link! is the design.
Here is my code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
TX1.Text = "0";
TX1.Focus();
TX2.Text = "0";
textBox2.Text = "10000";
}
private void TX1_TextChanged(object sender, EventArgs e)
{
try
{
int sum;
sum = Convert.ToInt32(TX1.Text) * Convert.ToInt32(Lb1PriceF.Text);
Lb1SumF.Text = Convert.ToString(sum);
}
catch
{
Lb1SumF.Text = "";
}
}
private void TX2_TextChanged(object sender, EventArgs e)
{
try
{
int sum;
sum = Convert.ToInt32(TX2.Text) * Convert.ToInt32(Lb2PriceF.Text);
Lb2SumF.Text = Convert.ToString(sum);
}
catch
{
Lb2SumF.Text = "";
}
}
private void Lb3SumF_TextChanged(object sender, EventArgs e)
{
try
{
int summ;
summ = Convert.ToInt32(Lb1SumF.Text) + Convert.ToInt32(Lb2SumF.Text);
Lb3SumF.Text = Convert.ToString(summ);
}
catch
{
Lb3SumF.Text = "";
}
}

The name does not exist in current context - How to access it?

In my code i have defined string repeatnumber and assigned it into for loop. However, i need it to be accesible when im creating a button click event. Not sure how to do it.
Can anyone give me some tips?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string[] assignments = new string[] { "A", "b", "c", "d", "e", "f" };
Random rnd = new Random();
string[] randomingArray = assignments.OrderBy(x => rnd.Next()).ToArray();
string repeatNumber = "";
List<ImageSource> animals = new List<ImageSource>();
for (int i = 1; i < 100; i++)
{
if (i == 9)
{
repeatNumber = randomingArray[i % randomingArray.Length];
animals.Add(new ImageSource() { Source = repeatNumber, Number = i });
}
else if ((i % 9) == 0)
{
animals.Add(new ImageSource() { Source = repeatNumber, Number = i });
}
else
{
animals.Add(new ImageSource() { Source = randomingArray[i % rnd.Next(1,5)], Number = i });
}
ItemsControl1.ItemsSource = animals;
}
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("test");
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show((new ImageSource() { Source = repeatNumber })); <-- the name repeatNumber does not exist in current context
}
}
class ImageSource
{
public int Number { get; set; }
public string Source { get; set; }
}
You need to define repeatnumber as a class property
public partial class MainWindow : Window
{
private string repeatNumber;
public MainWindow()
{
...
Otherwise it is limited to the scope (<= you want to look that up) of the MainWindow constructor.

How do I get a list that stores different styles I.E Button , Drag&Drop etc

public partial class QuestionDragAndDropList : Form
{
private List<ListQuestions> Questions = new List<ListQuestions>();
private int i = 0;
public QuestionDragAndDropList()
{
InitializeComponent();
NextQuestion();
}
private void QuestionList()
{
Questions.Add(new ListQuestions("Question 1", new[] {//Answers}, 0));
Questions.Add(new ListQuestions("Question 2", new[] {//^}, 0));
Questions.Add(new ListQuestions("Question 3", new[] {//^}, 0));
Questions.Add(new ListQuestions("Question 4", new[] {//^}, 0));
}
private void NextQuestion()
{
if (i != 2)
{
lblQuestion.Text = Questions[i].GetQuestion();
string[] Ans = Questions[i].GetAns();
BtnA1.Text = Ans[0];
BtnA2.Text = Ans[1];
BtnA3.Text = Ans[2];
BtnA4.Text = Ans[3];
}
else
{
Questions[i].GetQuestion();
BitMap[] Ans = Questions[i].GetAnswers();
}
}
private void AnsCheck(int Answer)
{
if (Answer < Questions.Count)
{
WelcomeYear11.Userfiling.IncreaseS();
}
i++;
if (i != Questions.Count)
{
NextQuestion();
}
else
{
do something.
}
}
private void GrabLabel(object sender, MouseEventArgs e)
{
Label selectedLbl = (Label)sender;
selectedLbl.DoDragDrop(selectedLbl.Text, DragDropEffects.Copy);
}
private void AllowDragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void PBox1DragDrop(object sender, DragEventArgs e)
{
string result = e.Data.GetData(DataFormats.Text).ToString();
if (result == "9")
{
lblA1.Visible = false;
PboxA1.Visible = false;
}
}
private void PBox2DragDrop(object sender, DragEventArgs e)
{
string result = e.Data.GetData(DataFormats.Text).ToString();
if (result == "30")
{
LblA2.Visible = false;
PBoxA2.Visible = false;
}
}
private void PBox3DragDrop(object sender, DragEventArgs e)
{
string result = e.Data.GetData(DataFormats.Text).ToString();
if (result == "5")
{
LblA3.Visible = false;
PBoxA3.Visible = false;
}
}
private void PBox4DragDrop(object sender, DragEventArgs e)
{
string result = e.Data.GetData(DataFormats.Text).ToString();
if (result == "18")
{
LblA3.Visible = false;
PBoxA3.Visible = false;
}
}
}
So using this code, how am I able to not only have it set to generate forms with button questions but also with drag/drop, checkboxes, etc...
The ListClass used in the sample is:
class ListQuestions
{
private string Questions;
private Bitmap[] Answers; private string[] Ans; //First is for Picbox questions. Second is for button questions.
private int PosOfAns;
public ListQuestions(string questions, Bitmap[] answers, int posOfAns)
{
Questions = questions;
Answers = answers;
PosOfAns = posOfAns;
}
public ListQuestions(string questions, string[] ans, int posOfAns)
{
Questions = questions;
Ans = ans;
PosOfAns = posOfAns;
}
public string GetQuestion()
{
return Questions;
}
public string[] GetAns()
{
return Ans;
}
public Bitmap[] GetAnswers()
{
return Answers;
}
public int GetPosOfAns()
{
return PosOfAns;
}
}
Any help is much appreciated :).
I think you're after this
using System.Collections.Generic;
Bitmap[] bm = { new Bitmap(1, 1) };//just had to make one to use in the ListQuestions method overload
List<ListQuestions> thelist = new List<ListQuestions>();
thelist.Add(new ListQuestions("", bm, 1));
Basically, you create your class, then you make a list of your class
List<ListQuestions> thelist = new List<ListQuestions>();
Then you can manipulate your list
thelist.Add(new ListQuestions("", bm, 1));
But since your class properties are all private, none of them are accessible outside of your class. So after you have added items to your List<>, you can't really access any of them. For example, this won't work until your properties are public.
string question = thelist[0].Questions;
Here's a working sample
using System.Collections.Generic;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
List<myclass> mylist = new List<myclass>();
mylist = Fillmylist(mylist);
this.Controls.Add(mylist[0].chckbx_Recurse);
this.Controls.Add(mylist[0].txtbx_pwd);
this.Controls.Add(mylist[0].txtbx_usrname);
}
private List<myclass> Fillmylist(List<myclass> incominglist)
{
TextBox tb = new TextBox();
tb.Name = "txtbx_usr";
tb.SetBounds(20, 20, 50, 10);
TextBox tbp = new TextBox();
tbp.Name = "txtbx_pwd";
tbp.SetBounds(20,50, 50, 10);
CheckBox cb = new CheckBox();
cb.Name = "chkbx_recurse";
cb.SetBounds(20, 80, 20, 20);
incominglist.Add(new myclass { txtbx_usrname = tb, txtbx_pwd = tbp, chckbx_Recurse = cb });
return incominglist;
}
}
class myclass
{
public TextBox txtbx_usrname { get; set; }
public TextBox txtbx_pwd { get; set; }
public CheckBox chckbx_Recurse { get; set; }
}
}

Get Data from DatagridView from to Object Array then populate ComboBox

I want to get data from datagridview and then populate the combobox with data from an object array which contains a string
public class Departmentinfo
{
public string departmentname;
.
.
.
.
}
Departmentinfo dep[];
private void getdepartments()
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
college.department[i].departmentname = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
}
}
private void putdepinfo()
{
comboBox4.DataSource = dep[].departmentname;
}
please suggest !
I got the code working with this,
public class Departmentinfo
{
public string departmentname;
private void Departmentinfo(string s)
{
this.departmentname = s;
}
}
List<Departmentinfo> DepInfo = new List<Departmentinfo>();
private void getdepartments()
{
for (int i = 0; i < dataGridView1.RowCount; i++)
{
DepInfo.Add(new Departmentinfo(Convert.ToString(dataGridView1.Rows[i].Cells[0].Value)));
}
}
private void putdepartments()
{
foreach (Departmentinfo dep in DepInfo)
{
comboBox4.Items.Add(dep.departmentname);
}
}

Categories

Resources