Question: So I am currently coding a project for a class and so far I have the code somewhat working. I just need help with coding
So I am currently coding a project for a class and so far I have the code somewhat working. I just need help with coding combo and listbox. Working in C#, wpf application for selecting a class for "registration" I am working in C#, Visual Studio 17.
The errors I have are:
Error CS0120 An object reference is required for the non-static field, method, or property 'ItemsControl.Items' Lines 84-89, 95 (ComboBox.Items.Add) and (ListBox.Items.Add)
Error CS0120 An object reference is required for the non-static field, method, or property 'Selector.SelectedItem' Line 94 (ComboBox.SelectedItem)
Here's my code:
CreateClassesObjs.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes;
namespace CreateClassesObjs { class Course
{ private string courseName; //REQ1 Private string field to hold name of course
public Course() //default constructor { courseName = "Default course name"; }
public Course(string initialCourseName) //overloaded constructor { courseName = initialCourseName; }
public void SetName(string a) //REQ2 set name field to string. Because of the book's suggestion, I changed this public function to be capitalized { courseName = a; }
public string GetName() //REQ3 Retrieve name field { return courseName; }
public override string ToString() //REQ4 Returns name field by calling GetName() { return GetName(); }
///
Course choice;
public MainWindow() { InitializeComponent(); }
private void InitializeComponent() { throw new NotImplementedException(); }
private void Window_Loaded(object sender, RoutedEventArgs e) { Course course1 = new Course(); Course course2 = new Course(); Course course3 = new Course(); Course course4 = new Course(); Course course5 = new Course(); Course course6 = new Course(); Course course7 = new Course();
course1.SetName("IT 145"); course2.SetName("IT 200"); course3.SetName("IT 201"); course4.SetName("IT 270"); course5.SetName("IT 315"); course6.SetName("IT 328"); course7.SetName("IT 330"); ComboBox.Items.Add(course2); ComboBox.Items.Add(course3); ComboBox.Items.Add(course4); ComboBox.Items.Add(course5); ComboBox.Items.Add(course6); ComboBox.Items.Add(course7); }
private void Button_Click(object sender, RoutedEventArgs e) { choice = (Course)(ComboBox.SelectedItem); ListBox.Items.Add(choice); }
}
} }
Here's what I have in the designer: MainWindow.xaml
Since listbox and combobox are case sensitive in C#, how do I fix these two errors? I have tried to quick add and refractor but it did not change anything. Help?
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
