C
C#2y ago
bernie2024

❔ ✅ How to call a specific attribute from a getter class to another class

Hey there, I am struggling to call this attribute from another class. The two relevant code pieces are:
public List<CompletedAssignment> GetCompletedAssignments(int studentID, int assignmentID)
{
List<CompletedAssignment> completedAssignments = new List<CompletedAssignment>();

using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT StudentID, AssignmentID, EarnedPoints FROM CompletedAssignments WHERE StudentID = @StudentID AND AssignmentID = @AssignmentID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@StudentID", studentID);
command.Parameters.AddWithValue("@AssignmentID", assignmentID);

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
CompletedAssignment completedAssignment = new CompletedAssignment();

completedAssignment.StudentID = reader.GetInt32(0);
completedAssignment.AssignmentID = reader.GetInt32(1);
completedAssignment.EarnedPoints = reader.GetInt32(2);

completedAssignments.Add(completedAssignment);
}
}
}

return completedAssignments;
}
public List<CompletedAssignment> GetCompletedAssignments(int studentID, int assignmentID)
{
List<CompletedAssignment> completedAssignments = new List<CompletedAssignment>();

using (SqlConnection connection = new SqlConnection(connectionString))
{
string query = "SELECT StudentID, AssignmentID, EarnedPoints FROM CompletedAssignments WHERE StudentID = @StudentID AND AssignmentID = @AssignmentID";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@StudentID", studentID);
command.Parameters.AddWithValue("@AssignmentID", assignmentID);

connection.Open();

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
CompletedAssignment completedAssignment = new CompletedAssignment();

completedAssignment.StudentID = reader.GetInt32(0);
completedAssignment.AssignmentID = reader.GetInt32(1);
completedAssignment.EarnedPoints = reader.GetInt32(2);

completedAssignments.Add(completedAssignment);
}
}
}

return completedAssignments;
}
and
12 Replies
bernie2024
bernie2024OP2y ago
private void UpdateTextBoxes()
{
if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
{
return;
}

Student student = (Student)comboBox1.SelectedItem;
Assignment assignment = (Assignment)comboBox2.SelectedItem;

string completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID).ToString();

textBox1.Text = completedAssignment;
textBox2.Text = assignment.TotalPoints.ToString();
}
private void UpdateTextBoxes()
{
if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
{
return;
}

Student student = (Student)comboBox1.SelectedItem;
Assignment assignment = (Assignment)comboBox2.SelectedItem;

string completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID).ToString();

textBox1.Text = completedAssignment;
textBox2.Text = assignment.TotalPoints.ToString();
}
the issue is that I cant get the specific attribute I need into this textbox
ero
ero2y ago
What attribute?
bernie2024
bernie2024OP2y ago
sorry the EarnedPoints attribute what I want to do is this:
private void UpdateTextBoxes()
{
if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
{
return;
}

Student student = (Student)comboBox1.SelectedItem;
Assignment assignment = (Assignment)comboBox2.SelectedItem;


List<CompletedAssignment> completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID);


textBox1.Text = completedAssignment.EarnedPoints.ToString();

textBox2.Text = assignment.TotalPoints.ToString();
}

}
}
private void UpdateTextBoxes()
{
if (comboBox1.SelectedItem == null || comboBox2.SelectedItem == null)
{
return;
}

Student student = (Student)comboBox1.SelectedItem;
Assignment assignment = (Assignment)comboBox2.SelectedItem;


List<CompletedAssignment> completedAssignment = dbManager.GetCompletedAssignments(assignment.AssignmentID, student.StudentID);


textBox1.Text = completedAssignment.EarnedPoints.ToString();

textBox2.Text = assignment.TotalPoints.ToString();
}

}
}
`
bernie2024
bernie2024OP2y ago
but it throws the error
bernie2024
bernie2024OP2y ago
and I am not sure how to grab just that attribute from the list
ero
ero2y ago
So, those are called properties, not attributes Attributes are very specifically different things in C#
bernie2024
bernie2024OP2y ago
ok sorry I am just unsure of how to display that property
ero
ero2y ago
You have an entire list of CompletedAssignments You kinda need to choose just one to get its EarnedPoints How you choose which one to take... that'll have to be up to you
bernie2024
bernie2024OP2y ago
so like hmmm look I am feeding it a studentID and assignmentID right and its supposed to output the EarnedPoints value how do I choose in the list though?
ero
ero2y ago
I don't know It's your list By the way you call GetCompletedAssignments wrong You have student ID and assignment ID backwards in your call
bernie2024
bernie2024OP2y ago
thanks for the help! :) jk! !close
Accord
Accord2y ago
Closed! Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server