简体   繁体   中英

Create an object collection and query it

A stupid question. I have three classes. Class DataTableManager is a kind of global class.

public  class DataTableManager
{
    public DataTable dt1 { get; set; }
    public List<object> SchoolInfo { get; set; }
    public string ApplicationName {get;set;}
    public int number;
}

In public class DataCreate, I want to add diferent type of variables to the object then we can get them in another public class Summary. My question is I am not familar with object collection for different type variables.

For example, in

public class DataCreate 
{

   public string FirstName { get; set; }
   public string LastName { get; set; }
   public int ID { get; set; }
   public GradeLevel Year;
}

I want to add them to SchoolInfo. Suppose they are from textboxs. Not sure how to do it?

And how to retrieve it in my third class Summary?

Thanks.

UPDATE:

DataTableManager object is a global that hold all form's information. In each form we want to set the object and held by DataTableManager object. Then finally I want to get them in the last form. What I want is each form has an object to hold all variables although they have different type.

To create a new DataCreate object and add it to a DataTableManager object's SchoolInfo list, you would need to do something like this:

DataTableManager manager = new DataTableManager();
DataCreate data = new DataCreate();
data.LastName = TextBox1.Text;
data.ID = TextBox2.Text;
manager.SchoolInfo.Add(data);

However, I should point out that it's bad practice to declare SchoolInfo as a List<Object> type of property. It would be far better to declare it as a List<DataCreate> .

To add the DataCreate object to the list from within the DataCreate object itself, you would need to do something like this:

manager.SchoolInfo.Add(this);

DataTableManager is not an object, it's a type. You have to instantiate it to create an object (like I did when I called new DataTableManager ). Alternatively, you could make it a static class so that there is no object that needs to be instantiated and you can access it's members directly through its type name (eg DataTableManager.SchoolInfo.Add(...) ), but that's not how you currently have it defined.

However, I would not recommend doing things the way you are doing them at all. It's never a good idea to have global variables. Rather, if the form needs to have access to a DataTableManager object, it should ask for it (either by passing one in to a parameter of a constructor on the form, or by setting a public property on the form, or by calling some kind of public initialization method on the form). You must have some code somewhere that is showing these DataCreate forms. It is that class, where that code resides, that should be instantiating the manager and giving it to the forms.

However, typically you wouldn't want to store references to all the forms. Typically you'd just want to store the data entered. To do that, you'd want to create a new class which has no functionality, just public properties for the data. Then have the form return one of those objects and then have the central class is displaying the form take the returned data and add it to the manager.

For instance:

public class DataManager
{
    public List<StudentData> Students { get; set; }
    // etc.
}

public class StudentData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    // etc.
}

public class StudentEntryDialog : Form
{
    public StudentData StudentData { get; set; }

    // ... code to call storeData method when user chooses to do so ...

    private void storeData()
    {
        StudentData = new StudentData();
        StudentData.FirstName = TextBox1.Text;
        StudentData.LastName = TextBox2.Text;
        // etc.
    }
}

public class CentralCodeOfSomeSort
{
    private DataManager manager = new DataManager();

    private showStudentEntry()
    {
        StudentEntryDialog dialog = new StudentEntryDialog();
        dialog.ShowDialog();
        if (dialog.StudentData != null)
            manager.Students.Add(dialog.StudentData);
    }
}

I am not sure what you want. Just guessing. Create a class for the students

public class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int ID { get; set; }
    ...
}

and define your list as

public List<Student> SchoolInfo { get; set; }

Now you can add students to the list

 var manager = new DataTableManager(); 
 var student = new Student {
     FirstName = firstNameTextBox.Text;
     LastName = lastNameTextBox.Text;
     ID = Int32.Parse(IdTextBox.Text);
 };
 manager.SchoolInfo.Add(student);  

You can the query the list with (as an example)

var query = manager.SchoolInfo
    .Where(s => s.LastName.StartsWith("A")
    .OrderBy(s => s.LastName)
    .ThenBy(s => s.FirstName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM