locked
How to use class name in a function? RRS feed

  • Question

  • Hi all,

    I have a more classes like ... 

    public class Cars
    {
        public int id { get; set; }
        public string name { get; set; }
        ...
    }

    and datagrids.

    For all I want to creat one function but I donw know how to use a class name.

    That function looks like ..

    public void indexRefresh(DataGrid dtg, /*class name*/)
    {
        for (int i = 0; i < dtg.Items.Count; i++)
        {
            (dtg.Items[i] as /*class name*/).index = i;
        }
        dtg.Items.Refresh();
    }

    Thanks for help.

    Tuesday, June 23, 2020 10:30 AM

Answers

  • If you adore minimal changes, then try this code too:

    public void indexRefresh( DataGrid dtg )

    {

       for (int i = 0; i < dtg.Items.Count; i++)

       {

          dynamic d = dtg.Items[i];

          d.index = i;

       }

       dtg.Items.Refresh();

    }


    • Edited by Viorel_MVP Tuesday, June 23, 2020 4:37 PM
    • Marked as answer by TakeshiKitano Wednesday, June 24, 2020 6:20 AM
    Tuesday, June 23, 2020 4:36 PM

All replies

  • You can pass the class as a Generic argument:

    public void indexRefresh<T>(DataGrid dtg)
    {
        for (int i = 0; i < dtg.Items.Count; i++)
        {
            (T)(dtg.Items[i]).index = i;
        }
        dtg.Items.Refresh();
    }
    
    // call it as indexRefresh<yourClassName>(dtg)

    Tuesday, June 23, 2020 11:06 AM
  • It looks really good ... but in this case it doesnt know "index" in

    (T)(dg.Items[i]).index

    public class Cars
    {
        public int id { get; set; }
        public int index { get; set; } <<==
        public string name { get; set; }
        ...
    }
    ... or id, name ... whatever in class (for info ... every of my classes contain public int index )


    Tuesday, June 23, 2020 11:18 AM
  • Use Generics with a constraint:

    public interface IDataTransferObject
    {
        int ID { get; set; }
        int Index { get; set; }
        string Name { get; set; }
    }
    
    public class Cars : IDataTransferObject
    {
        public int ID { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public int Index { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
        public string Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
    }
    
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }
    
        public void indexRefresh<T>(DataGrid dtg) where T : IDataTransferObject
        {
            for (int i = 0; i < dtg.Items.Count; i++)
            {
                (T)(dtg.Items[i]).index = i;
            }
    
            dtg.Items.Refresh();
        }
    }
    

    Tuesday, June 23, 2020 1:40 PM
  • If you adore minimal changes, then try this code too:

    public void indexRefresh( DataGrid dtg )

    {

       for (int i = 0; i < dtg.Items.Count; i++)

       {

          dynamic d = dtg.Items[i];

          d.index = i;

       }

       dtg.Items.Refresh();

    }


    • Edited by Viorel_MVP Tuesday, June 23, 2020 4:37 PM
    • Marked as answer by TakeshiKitano Wednesday, June 24, 2020 6:20 AM
    Tuesday, June 23, 2020 4:36 PM