Dan Hall
Dan Hall

Reputation: 1534

Passing List of Class Objects to another class

i am trying to pass a List of class objects to another class, in this case I'm initilising a form class and need to send two values:

Class Object:

public class catData
{
  public string catName;
  public string modGUID;
  public string versionLocal;
  public string versionServer;
  public bool onServer;
}

In Class1 i have the list and need to send to Class2 upon initilisation.

Class1:

String catExportTXT = "txt";
List<catData> mainCatSet = new List<catData>();

using (var catForm = new Class2(catExportTXT, mainCatSet))
{
  var result = catForm.ShowDialog();
  if (result == System.Windows.Forms.DialogResult.OK)
  {
    localvar = catForm.selectedCats;
  }
  else
  {
    Environment.Exit(0);
  }
}

The problem here is Class2 has no definition for the class Object (catData) so i cannot send it directly as a parameter.

Can anyone advise on how i might go about defining the class object in Class2 so i can send the values?

Many Thanks

Upvotes: 0

Views: 2570

Answers (2)

James
James

Reputation: 2911

OK how about this:

1) Create an interface to provide a displayable name.

/// <summary>
/// Provides an interface for human-readable names in objects.
/// </summary>
public interface IDisplayName
{
    /// <summary>
    /// Gets a name to display in the application to end-users.
    /// </summary>
    string DisplayName { get; }
}

2) Create an interface for selectable objects.

/// <summary>
/// Provides an interface used to allow selection of an object in the user interface.
/// </summary>
public interface ISelectable : IDisplayName
{
}

3) Assign the interface and create the property on the catData object.

public class catData : ISelectable
{
    public string catName;
    public string modGUID;
    public string versionLocal;
    public string versionServer;
    public bool onServer;

    /// <summary>
    /// Gets a name to display in the application to end-users.
    /// </summary>
    public string DisplayName {  get { return catName; } }

    public override string ToString()
    {
        return this.DisplayName;
    }
}

4) The Class2 form (with an example but missing core selection actions obviously). If you are using a generic list control consider overriding ToString() in catData as above.

public partial class Class2<T> : Form where T : ISelectable
{
    private IEnumerable<ISelectable> selectableItems;

    public Class2()
    {
        InitializeComponent();
    }

    public Class2(string exportText, IEnumerable<T> list) : this()
    {
        this.selectableItems = list;
    }

    public T { get; set; }
    public List<T> SelectedItems { get; set; }

    private void Class2_Load(object sender, EventArgs e)
    {
        foreach (ISelectable item in selectableItems)
        {
            //System.Diagnostics.Debug.Print(item.DisplayName);
            System.Diagnostics.Debug.Print(item.ToString());
        }
    }
}

5) Utilize in your main app like this:

String catExportTXT = "txt";
List<catData> mainCatSet = new List<catData>();

using (var selectForm = new Class2<catData>(catExportTXT, mainCatSet))
{
    var result = selectForm.ShowDialog();
    if (result == System.Windows.Forms.DialogResult.OK)
    {
        var localvar = selectForm.SelectedItem;
    }
    else
    {
        Environment.Exit(0);
    }
}

Hope this makes sense?

Upvotes: 1

Daniel Manta
Daniel Manta

Reputation: 6683

I'm not sure why you need to do that but one option is using an array of dynamics.

class Program
{
    static void Main(string[] args)
    {
        var c1 = new Class1();
        c1.SendCats();
    }
}
class Class1
{
    public void SendCats()
    {
        var catData = new[]
        {
            new { name = "cat1", version = 1 },
            new { name = "cat2", version = 2 }
        };
        var c2 = new Class2(catData);
    }
}

class Class2
{
    public Class2(dynamic[] catData)
    {
        foreach (var cat in catData)
        {
            Console.WriteLine("{0} {1}", cat.name, cat.version);
        }
    }
}

The output is

cat1 1
cat2 2

Also, if needed you can convert a list of Cat object into a dynamic array

var catList = new List<Cat>();
catList.Add(new Cat { Name = "cat1", Version = 1 });
catList.Add(new Cat { Name = "cat2", Version = 2 });
var catData = catList.Select(c => new { name = c.Name, version = c.Version }).ToArray();
var c2 = new Class2(catData);

Upvotes: 0

Related Questions