var NationEligable = (from p in playerPeople orderby p.NationClubContracted where p.NationClubContracted!= -1 group p by new { p.NationalityName,p.NationClubContracted } into mygroup select new { mygroup.First().NationalityName ,NationCallUp = mygroup.First().NationClubContracted }).ToList();
i know this is a "anonymous type" but need to be able to access the list across classes/other forms in my projects.
I don't mind if it needs to get re-written from linq/lambda or whatever..
Basically need the list accessible across other classes/form in my project to populate comboboxes.
Any help would be appreciated.
VulpesPosted May 2, 2013, 12:49 PM
In the code behind my Form1 I have:
//
//
public System.Collections.Generic.List
//
//
//
Public Form1()
{
//
//
//playerPeople is created here
//
//
//
The code for Form2 will now be along these lines:
Jay SPosted May 4, 2013, 4:39 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace FMSaveEditor
{
public class FirstNames
{
public string Fname { get; set; }
public int FirstNameID { get; set; }
}
}
In form1 I've done:
List
join nm in list.Select((FName, z) => new { FName, z }) on fn.FirstNameID equals nm.z
group new { fn, nm } by new { fn.FirstNameID, nm.z }
into mygroup
select new FirstNames() { FirstNameID = mygroup.First().fn.FirstNameID, Fname = mygroup.First().nm.FName }).Distinct().ToList();
Which all seems to work..nothing errors and the list populates.
In form2 I do:
Form1 f1 = (Form1)Application.OpenForms["Form1"];
List
Names.Sort((FirstNames x, FirstNames y) => x.Fname.CompareTo(y.Fname));
It fails as the object comes back as null?
EDIT:
Sorted it out...forgot:
this.FirstNames = FirstNames;
Idiot!
Jay SPosted May 3, 2013, 7:51 PM
Now :/ Just need to do what you did last time and make the list avaliable to all classes :s
Jay SPosted May 3, 2013, 3:10 PM
I have another list issue (sorry if i should start a new thread...)
Currently creating a list like this:
its perfect and operates as i need.
I'm creating a second list (this can be changed if you thinks best...just mocked it up)
What it does is fine..though you may think theres a better way..
What I need to do now is join them..
I need to join FirstNameID in the FirstNameIds list to the index number? in the "list" list, "list" is just a 1 column list, but the item number in "list" matches the FirstNameID
So when I try to do a join in my linq I have nothing to join on..
Any suggestions?
VulpesPosted May 3, 2013, 7:53 AM
However, what you can do is to change your List
So, if you want to add a blank entry:
List
Nation blank = new Nation()
{
NationalityName = "", NationCallUp = 0 // say
};
nations.Insert(0, blank); // insert at first position in List
comboBox1.DataSource = nations;
Jay SPosted May 3, 2013, 7:05 AM
Would reflection be needed?!
Jay SPosted May 3, 2013, 5:07 AM
Instead of using this:
(changed some of the names)
I did as you suggested a couple of posts above:
And then just did this rather than looking back on the list:
VulpesPosted May 3, 2013, 5:01 AM
You can bind to any collection which implements the IList interface such as arrays, ArrayLists, generic Lists and DataSets.
When you do this, the Items property contains objects of the relevant type rather than strings. So, if you bind to a List
The DisplayMember property will then represent a property of the underlying Nation object which actually is displayed in the combobox.
The ValueMember property will represent another property of the Nation object which is not actually displayed but can be thought of as the 'value' of the object and which is therefore useful from a programmatic viewpoint.
In contrast, when in my earlier example, I was generating strings from the Nation objects and adding them to the Items collection, then the Items will actually be those strings.
Incidentally, they need not necessarily be strings (Items is an object collection) but, if they're something else, the ToString() method will be applied to them to display them in the combobox.
If you're not sure what your Items actually are, then you can easily discover their type with this line:
MessageBox.Show(comboBox1.SelectedItem.GetType().ToString());
Jay SPosted May 3, 2013, 2:00 AM
Wouldn't all my combo box be strings regardless?!
VulpesPosted May 2, 2013, 6:49 PM
However, if (as the error message suggests) you're populating the Items collection with strings, then it won't work and it will be more difficult to get back to the original Nation object.
Jay SPosted May 2, 2013, 3:33 PM
I've been able to bind it to the combobox with no problems..
What I'm not trying to do is to use the combobox selecteditem to search back on this list:
It fails on the first line with:
Unable to cast object of type 'System.String' to type 'Foo.Nation'
Any ideas?
VulpesPosted May 2, 2013, 2:02 PM
However, if you want to use databinding with DisplayMember and ValueMember properties, you can do this:
private void SomeMethod()
{
// get list of Nations from Form1
Form1 f1 = (Form1)Application.OpenForms["Form1"];
List
// show them in a combobox
comboBox1.DataSource = nations;
comboBox1.DisplayMember = "NationalityName";
comboBox1.ValueMember = "NationCallUp";
comboBox1.SelectedIndex = 0;
}
The following is a summary of what we've done:
1. Replaced the anonymous type with a named class, Nation, so that we can expose the List of Nations to all classes in the application.
2. The List is exposed as a public field, Nations, of Form1 which is populated in the constructor. As a constructor never returns a value we simply set the field to the result of the query.
3. Form2 can now access this field by first getting a reference to the current instance of Form1 (using Application.OpenForms), f1, and then using f1.Nations.
4. The combobox can now be filled either by databinding to the List or generating strings from the Nation properties and adding them to the Items collection.
Jay SPosted May 2, 2013, 1:16 PM
cboIntNation.Items.Add(String.Format("{0} - {1}", n.NationalityName, n.NationCallup):
I get a string in the combobox rather than a value and ID?
Jay SPosted May 2, 2013, 1:05 PM
Nations = NationEligable; -> this.Nations = NationEligable;
In form2 I did:
Got there in the end!!
Can you example (as best as you can and if you have time) what you/we did for this, just so I can try and understand it
Jay SPosted May 2, 2013, 12:27 PM
Created my "top level class":
In the code behind my Form1 I have:
//
//
public System.Collections.Generic.List
//
//
//
Public Form1()
{
//
//
//playerPeople is created here
//
//
//
This errors...The "Nation" is same above along with the return NationEligable saying : Since 'Foo.Form1.Form1()' returns void, a return keyword must not be followed by an object expression
VulpesPosted May 2, 2013, 12:09 PM
Jay SPosted May 2, 2013, 11:55 AM
My concern is : p in playerPeople
Is a list in my "Form1" code. No idea how to do a "top level" class and still have no idea how, for example, Form2 can call the Nation class to populate a combobox where the Nation class relies on a list in Form1
I'm completely lost at what to do.
VulpesPosted May 2, 2013, 11:48 AM
All that the other classes need to know is that they're getting a List of Nations. How this List was generated is not relevant to them.
Jay SPosted May 2, 2013, 11:44 AM
and
Nation()
I'm also concerned as in the linq it says: from p in playerPeople
playerPeople is a list itself..so if I call this new class from another class it will not know what from p in playerPeople is?!
VulpesPosted May 2, 2013, 11:36 AM
So, I'd create a named class instead:
and then change your query as follows:
You can now expose NationEligable as a public or internal field of the containing class or return it from a method.