user1259366
user1259366

Reputation: 65

Get data in class

I have class student :

public class Student
    {
        public string Name { get; set; }
        public string Age { get; set; }
        public Student()
        {
        }
        public List<Student> getData()
        {
            List<Student> st = new List<Student> 
            {
                new Student{Name="Pham Nguyen",Age = "22"},
                new Student{Name="Phi Diep",Age = "22"},
                new Student{Name="Khang Tran",Age = "28"},
                new Student{Name="Trong Khoa",Age = "28"},
                new Student{Name="Quan Huy",Age = "28"},
                new Student{Name="Huy Chau",Age = "28"},
                new Student{Name="Hien Nguyen",Age = "28"},
                new Student{Name="Minh Sang",Age = "28"},
            };
            return st;
        }        
    }

How can I take data in this class? ( I mean - example : I want take Name="Minh Sang",Age = "28" to show).

sorry about this question. but i don't know where to find it.

thanks all

Upvotes: 0

Views: 6456

Answers (5)

BizApps
BizApps

Reputation: 6130

You can use linq:

Student st = new Student();

var getStudent = from a in st.getData()
                      where a.Age == "28" & a.Name == "Minh Sang"
                      select a;

MessageBox.Show(getStudent.First().Age);
MessageBox.Show(getStudent.First().Name);

Upvotes: 2

Oscar Jara
Oscar Jara

Reputation: 14187

EDIT 1: Add these methods to your class:

public Student getStudent(int age, string name)
{
    return this.getData().Find(s => Convert.ToInt32(s.Age) == age && s.Name.Equals(name));
}

public Student getByIndex(int index)
{
    Student s = null;
    // maxIndex will be: 7
    // your array goes from 0 to 7
    int maxIndex = this.getData().Count() - 1;
    // If your index does not exceed the elements of the array:
    if (index <= maxIndex)
        s  = this.getData()[index];
    return s;
}
  • I convert the age to int if you need to evaluate in future > or <.

EDIT 2: Then invoke the methods like this:

    Student st = new Student();
    // s1 and s2 will return null if no result found.
    Student s1 = st.getStudent(28, "Minh Sang");
    Student s2 = st.getByIndex(7);

    if (s1 != null)
        Console.WriteLine(s1.Age);
        Console.WriteLine(s1.Name);
    if (s2 != null)
        Console.WriteLine(s2.Age);
        Console.WriteLine(s2.Name);

Upvotes: 0

Alexei Levenkov
Alexei Levenkov

Reputation: 100547

Maybe you are looking for DebuggerDisplay attribute to show it in debugger?

[DebuggerDisplay("Name = {name}, Age={age}")]
public class Student {....}

So when you hover over an item of type Student it will be shown the way you want...

Upvotes: 0

Kay
Kay

Reputation: 70

Call getData() to get a list of the students.

Iterate through the list with a foreach loop.

Inside the loop, print out the name and age of the Student.

Upvotes: 0

digitalmarks
digitalmarks

Reputation: 429

Look at the List.Find Method:

http://msdn.microsoft.com/en-us/library/x0b5b5bc.aspx

Next try implementing a new method:

public Student GetStudent(string name, int age)

Upvotes: 0

Related Questions