-4

I got a variable "StudentInfo" assigned with below value in a curly bracket:

var StudentId = StudentBll.GetStudents(false).Select(t => new { ID = t.ID, Name = t.Name }).ToList();


            comboStudent.DataSource = StudentId;
            comboStudent.DisplayMember = "Name";
            comboStudent.ValueMember = "ID";

var StudentInfo = comboStudent.SelectedItem;

The value of var StudentInfo returns as:

{ ID: 1, Name: "John" }
    ID: 1
    Name: "John"

How can I reference the Name only from the StudentInfo? For example, lblName.Text = StudentInfo.Name ? lblName.Text = StudentInfo(0).Name ? Both doesn't work...

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • StudentInfo is just a var variable and it's not a class – ColourShadow Nov 16 '18 at 02:21
  • 3
    Do you mean `var StudentInfo = new { ID = 1, Name = "John" }`? If so, `StudentInfo.Name` should, and does work. – ProgrammingLlama Nov 16 '18 at 02:21
  • 4
    Please post your _actual_ code so we can see exactly what the problem is. Since what you posted isn't complete C#, we're having to guess at what the missing pieces are. – D Stanley Nov 16 '18 at 02:22
  • 3
    It would be awesome if you could provide a [mcve] – jazb Nov 16 '18 at 02:26
  • var StudentId = StudentBll.GetStudents(false).Select(t => new { ID = t.ID, Name = t.Name }).ToList(); comboStudent.DataSource = StudentId; comboStudent.DisplayMember = "Name"; comboStudent.ValueMember = "ID"; var StudentInfo = comboStudent.SelectedItem; That's how the var StudentInfo got the { ID: 1, Name: "John" } value – ColourShadow Nov 16 '18 at 02:34
  • 1
    Please provide it as an [edit to your question](https://stackoverflow.com/posts/53330537/edit) and not as a comment, and also add a tag indicating which UI framework you're using (winforms, wpf, webforms, etc.) as it appears to be relevant to your question. – ProgrammingLlama Nov 16 '18 at 02:35
  • Edited, it's WinForms – ColourShadow Nov 16 '18 at 02:37

1 Answers1

1

ComboBox's SelectedItem property returns object. Only at runtime do we know what value this holds. That's why you can't use StudentInfo.Name because from the compiler's perspective, you're doing something like var StudentInfo = new object(); Console.WriteLine(StudentInfo.Name);: object doesn't contain a property Name.

Your next issue is that you're using an anonymous type (new { ID = t.ID, Name = t.Name }) so you can't just cast SelectedItem back to the type.

Some options are:

1. Define a class

public class BasicStudent
{
    public int ID {get;set;}
    public string Name {get;set;}
}

change your select to .Select(t => new BasicStudent { ID = t.ID, Name = t.Name }) and then afterwards cast .SelectedItem: Console.WriteLine(((BasicStudent)comboStudent.SelectedItem));

2. Use the original class

Stop selecting an anonymous type and just use the object as-is. Then simply cast back from .SelectedItem: Console.WriteLine(((Student)comboStudent.SelectedItem));

3. Use dynamic

dynamic student = comboStudent.SelectedItem;
Console.WriteLine(student.Name.ToString());

4. Use a hacky trick to cast comboStudent.SelectedItem back to the anonymous type

See the answer here.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86