C
C#13mo ago
Calen

WinForms Course Registration/Validation Homework

I just started my C# class for college and I've been having a real tough time trying to get my WinForms Course registration project to work. With this assignment, the only code I needed to write was the code under the courseComboBox_SelectedIndexChanged method and the code under the private void completeRegistrationButton_Click(object sender, EventArgs e) method. I included the validation in the first method but I'm not sure if that needs to be its own file or not. I've inserted the code needed for the courseComboBox_SelectedIndexChanged so it can handle the selection of the courses in the comboBox by validating the selection and adding the course number to the list of registered courses in the ListBox. I've also written the code for the button's event handler method so that when the user clicks the button to submit the completed registration, the program writes the list of registered courses to the output file. Visual Studio says I have no issues but when I run my code, it throws an issue as shown in the last picture. Also when I run the program, the form does not store the classes that I select and I am not able to progress so I'm not sure if that's the courseComboBox_SelectedIndexChanged method acting up or where I'm messing up. For reference, I am referencing the courses project in my WinForms project. This is my first time really submitting anything programming related anywhere so I apologize if I miss some key details I'm trying to insert everything I can think of. I am also really fresh in the programming world so I'm still in the basics. I'd appreciate any help with this, I don't want someone to do it for me, but rather help me find the solution so I can learn what I did wrong and what I'm missing. Thank you.
No description
No description
No description
No description
No description
No description
No description
117 Replies
Mayor McCheese
Mayor McCheese13mo ago
I'm not very familiar with winforms, but try to change your courses to a BindingList instead of a List, and add courses to that instead
Calen
CalenOP13mo ago
I changed the list to a BindingList but it did not fix my errors. I still have the exception user-unhandled error and am not able to save my selections on the form. Would you have any suggestions on what videos or sites are good resources for WinForms?
Mayor McCheese
Mayor McCheese13mo ago
Can you paste your code, not photos? $paste
MODiX
MODiX13mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Mayor McCheese
Mayor McCheese13mo ago
$code
MODiX
MODiX13mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Calen
CalenOP13mo ago
$codegif
Calen
CalenOP13mo ago
namespace Courses
{
public class Course
{
// Parameterized constructor sets values for all properties
public Course(string courseNumber, string courseTitle, int credits, int weeks)
{
CourseNumber = courseNumber;
CourseTitle = courseTitle;
Credits = credits;
LengthInWeeks = weeks;
// Initialize IsRegistered to false
IsRegistered = false;
}

// These properties are auto-implemented and read only (init allows setting via constructor).
public string CourseNumber { get; init; }
public string CourseTitle { get; init; }
public int Credits { get; init; }
public int LengthInWeeks { get; init; }
// IsRegistered is read-write
public bool IsRegistered { get; set; }

}
}
namespace Courses
{
public class Course
{
// Parameterized constructor sets values for all properties
public Course(string courseNumber, string courseTitle, int credits, int weeks)
{
CourseNumber = courseNumber;
CourseTitle = courseTitle;
Credits = credits;
LengthInWeeks = weeks;
// Initialize IsRegistered to false
IsRegistered = false;
}

// These properties are auto-implemented and read only (init allows setting via constructor).
public string CourseNumber { get; init; }
public string CourseTitle { get; init; }
public int Credits { get; init; }
public int LengthInWeeks { get; init; }
// IsRegistered is read-write
public bool IsRegistered { get; set; }

}
}
Calen
CalenOP13mo ago
BlazeBin - chjgjygibeie
A tool for sharing your source code with the world!
Calen
CalenOP13mo ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace u04a1_Registration_WinForm
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace u04a1_Registration_WinForm
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Mayor McCheese
Mayor McCheese13mo ago
Apologies looking on phone for the moment What is the data source courseComboBox? Is it courses?
Calen
CalenOP13mo ago
so that is supposed to be the object on the form that the user will use to make their selections from my understanding.
No description
Calen
CalenOP13mo ago
That drop down box
Mayor McCheese
Mayor McCheese13mo ago
So the error is complaining that you can't change that items collection right?
Calen
CalenOP13mo ago
From what I'm understanding, yes that's correct. the form won't store the course when the user selects it and continue the loop
Mayor McCheese
Mayor McCheese13mo ago
So let's look at a couple methods here
private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//courseComboBox.SelectedIndex = -1;

Course selectedCourse = courseComboBox.SelectedItem as Course;

if (selectedCourse == null)
{
statusMessageText.Text = "Please select a course: ";
return;
}

if (selectedCourse.IsRegistered)
{
statusMessageText.Text = "You are already registered for this course: ";
return;
}

//Register for the Course
selectedCourse.IsRegistered = true;

//update the ComboBox displaying the registered courses
courseComboBox.Items.Add(selectedCourse.CourseTitle);

//update total credits
totalCredits += selectedCourse.Credits;

//Display a success message
statusMessageText.Text = $"Registered for {selectedCourse.CourseTitle}";
}
private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//courseComboBox.SelectedIndex = -1;

Course selectedCourse = courseComboBox.SelectedItem as Course;

if (selectedCourse == null)
{
statusMessageText.Text = "Please select a course: ";
return;
}

if (selectedCourse.IsRegistered)
{
statusMessageText.Text = "You are already registered for this course: ";
return;
}

//Register for the Course
selectedCourse.IsRegistered = true;

//update the ComboBox displaying the registered courses
courseComboBox.Items.Add(selectedCourse.CourseTitle);

//update total credits
totalCredits += selectedCourse.Credits;

//Display a success message
statusMessageText.Text = $"Registered for {selectedCourse.CourseTitle}";
}
This method fires because the the "Selected Index Changed", I'm making a somewhat safe assumption that this is when courseComboBox has a selected index change event raised. I say somewhat safe because despite what the name of method is, it's actually not correlated to control couseComboBox so on line 81: courseComboBox.Items.Add(selectedCourse.CourseTitle); what do you expect this line to do?
Calen
CalenOP13mo ago
I expected that line of code to add the course by its course title to the already registered box
Mayor McCheese
Mayor McCheese13mo ago
What is the already registered box?
Calen
CalenOP13mo ago
IsRegistered* I apologize
Mayor McCheese
Mayor McCheese13mo ago
kk let's backwalk this a bit Firstly we have private BindingList<Course> courses = new BindingList<Course>(); BindingList has some black magic in the background that should help the control stay current against it's databound collection so in Form1_Load We're reading data here
using (StreamReader sr = new StreamReader(@"Data\course.data.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// Parse data and add Course objects to courses List as they are created
string[] data = line.Split(',');
// Need to convert credits and weeks to integers
courses.Add(new Course(data[0], data[1], Convert.ToInt16(data[2]),
Convert.ToInt16(data[3])));
}
}
using (StreamReader sr = new StreamReader(@"Data\course.data.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
// Parse data and add Course objects to courses List as they are created
string[] data = line.Split(',');
// Need to convert credits and weeks to integers
courses.Add(new Course(data[0], data[1], Convert.ToInt16(data[2]),
Convert.ToInt16(data[3])));
}
}
Which fills that same collection, courses, via courses.Add
Calen
CalenOP13mo ago
I will ask, the only code I wrote was the code under the two methods i mentioned above. the rest of it was already written by the professor. So would it be safe to change the List to a BindingList if he had wrote it originally as a List?
Mayor McCheese
Mayor McCheese13mo ago
you can safely change it back I thought we were solving a different problem when I mentioned that see the aforementioned, I'm helping on my phone now I'm not helping on my phone 🙂
Calen
CalenOP13mo ago
No worries, I should've clarified that's on me
Mayor McCheese
Mayor McCheese13mo ago
so, back to Form_Load nah you're fine, I just want to make sure we're on the same page, and that the rest of the code makes some sense so here, courseComboBox.DataSource = courses; we're telling courseComboBox that it's DataSource is that list which is a List<Course>; for the moment, the easiest way to think of List<Course> is that's a collection of courses and it can ONLY contain courses. so if you try to add widgets they're the wrong shape and they won't fit so back to our datasource in form_load you can see....
courseComboBox.DataSource = courses;
courseComboBox.DisplayMember = "CourseNumber";
courseComboBox.ValueMember = "CourseNumber";
courseComboBox.DataSource = courses;
courseComboBox.DisplayMember = "CourseNumber";
courseComboBox.ValueMember = "CourseNumber";
Here we're telling courseComboBox that the thing to show in the Box is the "member" CourseNumber and same for the value the underlying data, is still a Course, but, to show it you'll use the CourseNumber again this is all setup you didn't do, but I'm trying explain what was done @Calen in this method private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e) is your code right?
Calen
CalenOP13mo ago
private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e)
and
private void completeRegistrationButton_Click(object sender, EventArgs e)
private void completeRegistrationButton_Click(object sender, EventArgs e)
is the code I've written. Everything else, was already pre-written by the professor Also, I greatly appreciate the explanations you've provided, it's helped clarify this program
Mayor McCheese
Mayor McCheese13mo ago
so inside private void courseComboBox_SelectedIndexChanged(object sender, EventArgs e), you've written courseComboBox.Items.Add(selectedCourse.CourseTitle); you're trying to edit the courseComboBox Items collection and failing here right?
Calen
CalenOP13mo ago
Correct
Mayor McCheese
Mayor McCheese13mo ago
okay, so let's talk about the error first it's saying "Hey, you can't use the Items collection because I have a different idea of how I want my data to be changed" so when you set the DataSource it's setting a "flag" in the background that basically says, you can't assign to the property because I'm being managed a different way BUT also the datasource we're using is a list of Course so when we try to add via Items collections to the the combobox is saying "I can't work that way anymore because I have an external data source and even if we could add items that way a string is a different type of object than a Course, so it doesn't fit. So what do you expect to have happen with line 81? You said something earlier
I expected that line of code to add the course by its course title to the already registered box
IsRegistered* I apologize
I expected that line of code to add the course by its course title to the already registered box
IsRegistered* I apologize
and I'm not certain how that correlates
Calen
CalenOP13mo ago
I had a friend try and help me out with this prior to me coming to the group and that's how he had explained it to me. I'm still trying to get my mind around validation and WinForms
Mayor McCheese
Mayor McCheese13mo ago
kk so line 81 I suspect is just wrong, and we should remove it so let's decompose the method
Course selectedCourse = courseComboBox.SelectedItem as Course;

if (selectedCourse == null)
{
statusMessageText.Text = "Please select a course: ";
return;
}

if (selectedCourse.IsRegistered)
{
statusMessageText.Text = "You are already registered for this course: ";
return;
}

//Register for the Course
selectedCourse.IsRegistered = true;

//update the ComboBox displaying the registered courses
courseComboBox.Items.Add(selectedCourse.CourseTitle);

//update total credits
totalCredits += selectedCourse.Credits;

//Display a success message
statusMessageText.Text = $"Registered for {selectedCourse.CourseTitle}";
Course selectedCourse = courseComboBox.SelectedItem as Course;

if (selectedCourse == null)
{
statusMessageText.Text = "Please select a course: ";
return;
}

if (selectedCourse.IsRegistered)
{
statusMessageText.Text = "You are already registered for this course: ";
return;
}

//Register for the Course
selectedCourse.IsRegistered = true;

//update the ComboBox displaying the registered courses
courseComboBox.Items.Add(selectedCourse.CourseTitle);

//update total credits
totalCredits += selectedCourse.Credits;

//Display a success message
statusMessageText.Text = $"Registered for {selectedCourse.CourseTitle}";
Course selectedCourse = courseComboBox.SelectedItem as Course;
This is called a safe cast, we're trying to cast "SelctedItem" as a "Course"; if SelectedItem was a Cat, then it is NOT a Course, and the result is null
if (selectedCourse == null)
So, if it's not a Course, we're going to do what's in the if and here
if (selectedCourse.IsRegistered)
If you've already registered for the course, then you we enter into that if does that part make sense?
Calen
CalenOP13mo ago
Yes, that makes sense
Mayor McCheese
Mayor McCheese13mo ago
okay, so back to the top that method: Course selectedCourse = courseComboBox.SelectedItem as Course; here we're getting a "reference" to the selected course, and we're dealing with the object... so consider the following:
Course a = new Course { CourseNumber = "1234" },
Course b = new Course { CourseNumber = "5678" },
Course c = new Course { CourseNumber = "9101" }

List<Course> course = new List<Course>(new [] { a, b, c });

// a bunch of stuff

// now we select the SECOND one in the combobox labelled as 5678
Course selectedCourse = courseComboBox.SelectedItem as Course;
Course a = new Course { CourseNumber = "1234" },
Course b = new Course { CourseNumber = "5678" },
Course c = new Course { CourseNumber = "9101" }

List<Course> course = new List<Course>(new [] { a, b, c });

// a bunch of stuff

// now we select the SECOND one in the combobox labelled as 5678
Course selectedCourse = courseComboBox.SelectedItem as Course;
at this point selectedCourse is a "reference" to new Course { CourseNumber = "5678" } because it's the one we picked in the comboBox let me change one thing so now selectedCourse, becase we picked the second one, is Course b and here
selectedCourse.IsRegistered = true;
we set Course b IsRegistered = true through our reference of selectedCourse That bit is really important, becasue we have a reference to b, anything we change on our reference is really just actually b.
Calen
CalenOP13mo ago
With this assignment, he wants us to reference Course project onto the forms project. which is why I added the reference in there.
Mayor McCheese
Mayor McCheese13mo ago
two different types of references so example time
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
int a = 0;
int b = 1;
Console.WriteLine((a, b));
a = b;
b = 2;
int a = 0;
int b = 1;
Console.WriteLine((a, b));
a = b;
b = 2;
Console Output
(0, 1)
(0, 1)
Compile: 507.229ms | Execution: 63.568ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
In the above example: what is a at the end? it's okay to guess
Calen
CalenOP13mo ago
A would be b because that's the last thing that was assigned to a
Mayor McCheese
Mayor McCheese13mo ago
so, a would be 1, becasue we assigned it to b, which was one. because a and b are "integers" they are "value types" so they aren't "by reference"
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
int a = 0;
int b = 1;
Console.WriteLine((a, b));
a = b;
b = 2;
Console.WriteLine((a, b));
int a = 0;
int b = 1;
Console.WriteLine((a, b));
a = b;
b = 2;
Console.WriteLine((a, b));
Console Output
(0, 1)
(1, 2)
(0, 1)
(1, 2)
Compile: 586.508ms | Execution: 64.063ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
now we can get a bit more advanced
Calen
CalenOP13mo ago
So are we seeing what a is in this example as well?
Mayor McCheese
Mayor McCheese13mo ago
well in the above, we can see that a and b are set as values, and they don't change together, but separately
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
class Course {
public int Value {get; set;}
}

Course a = new Course { Value = 0 };
Course b = new Course { Value = 1 };

Console.WriteLine((a.Value, b.Value));
class Course {
public int Value {get; set;}
}

Course a = new Course { Value = 0 };
Course b = new Course { Value = 1 };

Console.WriteLine((a.Value, b.Value));
Console Output
(0, 1)
(0, 1)
Compile: 600.851ms | Execution: 89.127ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
now we have a similar example but we're using Courses, which are not value types and we see that a is 0, and b is 1
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
class Course {
public int Value {get; set;}
}

Course a = new Course { Value = 0 };
Course b = new Course { Value = 1 };

Console.WriteLine((a.Value, b.Value));
a = b;
a.Value = 10;
Console.WriteLine((a.Value, b.Value));
class Course {
public int Value {get; set;}
}

Course a = new Course { Value = 0 };
Course b = new Course { Value = 1 };

Console.WriteLine((a.Value, b.Value));
a = b;
a.Value = 10;
Console.WriteLine((a.Value, b.Value));
Console Output
(0, 1)
(10, 10)
(0, 1)
(10, 10)
Compile: 570.673ms | Execution: 85.227ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
so references work differently when we assign a to b, we have to references pointing to b, and we've lost the original a
Calen
CalenOP13mo ago
So for this one, you just set two references with their own values and had them print out?
Mayor McCheese
Mayor McCheese13mo ago
yes, but I wanted you to see that a and b are pointng to the same thing
Calen
CalenOP13mo ago
I just wanted to double check, thank you
Mayor McCheese
Mayor McCheese13mo ago
kk so now Course selectedCourse = courseComboBox.SelectedItem as Course; selectedCourse is a reference to SelectedItem so, if we make a change to the details of selectedCourse, we'll be changing SelectedItem so what this means is... when you do this.. selectedCourse.IsRegistered = true; and later try to do it again, if (selectedCourse.IsRegistered) for the same item that's why that validation works make sense?
Calen
CalenOP13mo ago
I'm starting to grasp the concept, its making sense
Mayor McCheese
Mayor McCheese13mo ago
So next we have
//update the ComboBox displaying the registered courses courseComboBox.Items.Add(selectedCourse.CourseTitle);
We don't need to do this at all tell me why
Calen
CalenOP13mo ago
is it because we already referenced our SelectedItem and selectedCourse?
Mayor McCheese
Mayor McCheese13mo ago
yepper! we've already done the update I had a message ready to go... hint: we already have the refence to the Course so basically if we remove that line, we have fixed the problem you're having, because we shouldn't need it at all
Calen
CalenOP13mo ago
So I had repeated the same thing over twice but incorrectly with the courseComboBox.Items.Add(selectedCourse.CourseTitle);
Mayor McCheese
Mayor McCheese13mo ago
sort of yes it's better to say probably that you already accomplished your intention before adding courseComboBox.Items.Add(selectedCourse.CourseTitle); the line courseComboBox.Items.Add(selectedCourse.CourseTitle); isn't valid at this time, and doesn't really accomplish what the comment says it is
Calen
CalenOP13mo ago
I removed that line and began to ran the code and I am getting the messages that the courses are registered, but the courses are not populating on the form and not stopping me after reaching the max amount of credits
No description
Mayor McCheese
Mayor McCheese13mo ago
now, for a number of reasons totalCredits += selectedCourse.Credits; is also wrong, but it's going to work for now let's tackle that next
Mayor McCheese
Mayor McCheese13mo ago
What is the name of that control?
No description
Calen
CalenOP13mo ago
I double clicked the box in the Form1.cs designer and it populated this code in the Form1.cs
private void registeredCourseList_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}
private void registeredCourseList_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}
Mayor McCheese
Mayor McCheese13mo ago
so remember line 81? that we deleted?
Calen
CalenOP13mo ago
yes
Mayor McCheese
Mayor McCheese13mo ago
let's add back, BUT change to registeredCourseList.Items.Add(selectedCourse.CourseTitle) and run again
Calen
CalenOP13mo ago
That worked and it allowed me to insert the courses in the box but it did not write out how many credits all of that is total and stop at the max credit hours allowed
No description
Mayor McCheese
Mayor McCheese13mo ago
sure so, now winforms databinding is utter trash
Mayor McCheese
Mayor McCheese13mo ago
Likely this is two controls
No description
Mayor McCheese
Mayor McCheese13mo ago
with a label on the left and a label on the right the one where the Text property is 0 is going to have a name okay, well both will have names but the one on the right is the one we care about
Calen
CalenOP13mo ago
Complete the code for the event handler method for the ComboBox so that it handles the selection of course in the ComboBox by validating the selection and adding the course number to the list of registered courses in the ListBox. Complete the code for the button's event handler method so that when the user clicks the button to submit the completed registration, the program writes the list of registered courses to the output file. Make sure your code is: Is a syntactically correct, well documented C# console application that compiles and runs without warnings or errors. Includes the Course class as a reusable library class. Implements the event handler method for item selection in the ComboBox control correctly. Implements the event handler method for the submit button correctly. Writes valid registrations to the registered.courses.txt file with appropriate exception handling. So these were the requirements for the assignment and I am trying to verify that there needs to be a requirement for limiting the amount of courses and credit hours taken
Mayor McCheese
Mayor McCheese13mo ago
regardless, the label that is set at 0 You have a the line statusMessageText.Text = "Error reading data: " + ex.Message; much the same way we set the value of that lable.Text with the total credits it's a bit more difficult, because totalCredits is an int and we can't automatically assign an int to a string, how might we fix that?
Calen
CalenOP13mo ago
Would it be by parsing it? This process sounds familiar
Mayor McCheese
Mayor McCheese13mo ago
parsing the int would convert from a string to an int
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
int.Parse("1")
int.Parse("1")
Result: int
1
1
Compile: 410.622ms | Execution: 33.347ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Failure
int.Parse("yes")
int.Parse("yes")
Exception: FormatException
- The input string 'yes' was not in a correct format.
- The input string 'yes' was not in a correct format.
Compile: 403.165ms | Execution: 25.361ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
that's not quite what we want
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Failure
string s = 1;
string s = 1;
Exception: CompilationErrorException
- Cannot implicitly convert type 'int' to 'string'
- Cannot implicitly convert type 'int' to 'string'
Compile: 371.960ms | Execution: 0.000ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
in c# there is a couple ways to handle this problem I'm going to cheat and say look around line 104
Calen
CalenOP13mo ago
The $"?
Mayor McCheese
Mayor McCheese13mo ago
yes could we leverage that?
Calen
CalenOP13mo ago
I was thinking that, but I thought it was parsing, but its encapsulation correct?
Mayor McCheese
Mayor McCheese13mo ago
not really parsing or encapsulating more like converting
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
string s = $"{1}"; s
string s = $"{1}"; s
Result: string
1
1
Compile: 495.667ms | Execution: 32.751ms | React with ❌ to remove this embed.
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
int totalCredits = 0;
totalCredits += 10;
$"You wanted {totalCredits}"
int totalCredits = 0;
totalCredits += 10;
$"You wanted {totalCredits}"
Result: string
You wanted 10
You wanted 10
Compile: 466.595ms | Execution: 40.229ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
so knowing the label name you should be able to set the value of the Tex property
Calen
CalenOP13mo ago
So we use $" anytime we need to convert an integer into a string to avoid any conflicting issues?
Mayor McCheese
Mayor McCheese13mo ago
sort of, it's a way to convert with a string it doesn't have to be an int
Calen
CalenOP13mo ago
With this, there's also an included unit test, would that also be a helpful way to find out issues in the code and pinpoint where problems come from?
MODiX
MODiX13mo ago
Mayor McCheese
REPL Result: Success
1.ToString()
1.ToString()
Result: string
1
1
Compile: 391.380ms | Execution: 22.242ms | React with ❌ to remove this embed.
Mayor McCheese
Mayor McCheese13mo ago
If you run the unit test, it might cover if the code works I don't know what the unit test does
Calen
CalenOP13mo ago
using Capella.IT4747;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace TestValidator
{
[TestClass]
public class ValidatorTest
{
private List<Course> courses;
// Set up List of Course objects to be used for each test
[TestInitialize]
public void Setup()
{
courses = new List<Course>();
courses.Add(new Course("IT2230", "Introduction to Databases", 3, 5));
courses[0].IsRegisteredFor = true;
courses.Add(new Course("IT2240", "Introduction to Programming - C", 3, 5));
courses[1].IsRegisteredFor = false;
courses.Add(new Course("IT2249", "Introduction to Java", 6, 10));
courses[2].IsRegisteredFor = false;
}
[TestMethod]
public void TestValidateRegistration_Duplicate()
{
const int expectedResult = -2;
int result = Validator.ValidateRegistration(0, courses);
Assert.AreEqual(expectedResult, result);
}
[TestMethod]
public void TestValidateRegistration_OverCreditLimit()
{
// Now registered for 6 credits
courses[1].IsRegisteredFor = true;
// Try to register for 6-credit course
int result = Validator.ValidateRegistration(2, courses);
const int expectedResult = -3;
Assert.AreEqual(expectedResult, result);
}
[TestMethod]
public void TestValidateRegistration_ValidRequest()
{
const int expectedResult = 0;
int result = Validator.ValidateRegistration(1, courses);
Assert.AreEqual(expectedResult, result);
}
}
}
using Capella.IT4747;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
namespace TestValidator
{
[TestClass]
public class ValidatorTest
{
private List<Course> courses;
// Set up List of Course objects to be used for each test
[TestInitialize]
public void Setup()
{
courses = new List<Course>();
courses.Add(new Course("IT2230", "Introduction to Databases", 3, 5));
courses[0].IsRegisteredFor = true;
courses.Add(new Course("IT2240", "Introduction to Programming - C", 3, 5));
courses[1].IsRegisteredFor = false;
courses.Add(new Course("IT2249", "Introduction to Java", 6, 10));
courses[2].IsRegisteredFor = false;
}
[TestMethod]
public void TestValidateRegistration_Duplicate()
{
const int expectedResult = -2;
int result = Validator.ValidateRegistration(0, courses);
Assert.AreEqual(expectedResult, result);
}
[TestMethod]
public void TestValidateRegistration_OverCreditLimit()
{
// Now registered for 6 credits
courses[1].IsRegisteredFor = true;
// Try to register for 6-credit course
int result = Validator.ValidateRegistration(2, courses);
const int expectedResult = -3;
Assert.AreEqual(expectedResult, result);
}
[TestMethod]
public void TestValidateRegistration_ValidRequest()
{
const int expectedResult = 0;
int result = Validator.ValidateRegistration(1, courses);
Assert.AreEqual(expectedResult, result);
}
}
}
This was the unit test but it is not currently in my project
Mayor McCheese
Mayor McCheese13mo ago
This won't really help you too much in validating your code
Calen
CalenOP13mo ago
So it's not really necessary then?
Mayor McCheese
Mayor McCheese13mo ago
I don't see the need for it, but I mean it's not my assignment 🙂
Calen
CalenOP13mo ago
I get that haha
Mayor McCheese
Mayor McCheese13mo ago
so what still doesn't work?
Calen
CalenOP13mo ago
lemme rebuild
Mayor McCheese
Mayor McCheese13mo ago
I only ask, as it's nearly midnight my time
Calen
CalenOP13mo ago
I understand, I appreciate you taking time out of your night to help me out with this. thank you I have one question, in regards to the label, were you referring to line 92 with the credits?
Mayor McCheese
Mayor McCheese13mo ago
nah, this is after my line 84 totalCredits += selectedCourse.Credits; after we set totalCredits, we can assign totalCredits to the label
Calen
CalenOP13mo ago
so what are the modifications that I need to do with the totalCredits? I'm looking for the line with the ints we need to change to a string
Mayor McCheese
Mayor McCheese13mo ago
well we leave everything as is we're adding more code so, we have some label that the text value is 0 what is the name of that label?
Calen
CalenOP13mo ago
Are you asking me to come up with a name or what the label is called? cause I see totalCredits = 0 up top
Mayor McCheese
Mayor McCheese13mo ago
No description
Mayor McCheese
Mayor McCheese13mo ago
that is probably a label on your form
Calen
CalenOP13mo ago
I'm not finding that label
Mayor McCheese
Mayor McCheese13mo ago
hmmmm so if you right click the zero on the form and select properties
Calen
CalenOP13mo ago
No description
Mayor McCheese
Mayor McCheese13mo ago
so your label is totalCreditValueLabel so after we incement total credits we can set totalCredits to the the "Text" property of totalCreditValueLable
Calen
CalenOP13mo ago
How would that be, I tried
totalCredits += totalCreditValueLabel.Text;
totalCredits += totalCreditValueLabel.Text;
but got an error
Mayor McCheese
Mayor McCheese13mo ago
more like totalCredits += selectedCourse.Credits; totalCreditValueLabel.Text = $"{totalCredits}";
Calen
CalenOP13mo ago
Worked like a charm. So, totalCredits += selectedCourse.Credits; reads the credit amount for a class, and then the second valueLabel.Text actually prints out the values of the courses?
Mayor McCheese
Mayor McCheese13mo ago
yepper
Calen
CalenOP13mo ago
I have to get in a whole different mindset with programming that everything has to be written out. I really appreciate all the time you've spent in helping me out with this, this works for assignment and I really learned a lot and it made sense what you walked me through with. I noticed you have experience in WPF and that is also something I will be working on as well as ADO.NET with SQL. I don't want to take up much more of your time but if you're available in the future, I'd appreciate getting your .02 on some of that as well if it's needed
Mayor McCheese
Mayor McCheese13mo ago
sure I'm more of a desktop hobbyist, I don't write desktop software professionally so keep that in mind 🙂
Calen
CalenOP13mo ago
I totally understand. Again, thank you for the help with this!
Mayor McCheese
Mayor McCheese13mo ago
np I'm headed to bed though
Calen
CalenOP13mo ago
sounds good!
Want results from more Discord servers?
Add your server