-6

reading this How to loop through a multidimensional list i do not really find a way for my specific wanted thing.

Let me describe in words and with example code hoping it illustrate it as expected.

Description or goal is: a function that is able to create a control and set/configure itself with provided attributes as you will find below ?

TableLayoutPanel _tlp = new TableLayoutPanel();
_tlp.Name = "_tlp";
_tlp.TabStop = false;
_tlp.Dock = DockStyle.Fill;

_mytlp = (TableLayoutPanel)_func(TableLayoutPanel,new List<string,object>(){{TabStop,false},{Dock,fill}});

private _func(object _ctrl, List<string,object> _attribs) {
   _ctrl.getType() _tmpobj = new _ctrl.getType();
   foreach (KeyValuePair<string,object> _elem in _attribts ) {
       _tmpobj.(_elem.Key) = _elem.Value;
   }
   return _tmpobj;
}

if this is not enough illustration I do not know how to describe it better than this. I know this is not exact or correct c# code but it only should help to understand what my goal is. so first part of example code is standard creation of that specific control. next to it i would like to call the function that helps to create specified object by providing its type and wanted attributes that should be set for it.

thinking of attribute part another thing is unclear:

is following possible ?

List<string> _mL = new List<string>(){"TabStop|false"};
foreach ( string item in _mL ) {
   tmp = item.split('|');
   _tlp.tmp[0] = tmp[1];
   if ( tmp[0].equals("TabStop")) _tlp.TabStop = tmp[1];
}
beg-inner
  • 5
  • 3
  • 1
    And *why* are you trying to do this? What problem does being able to do this solve? – Sweeper Oct 01 '22 at 19:31
  • i can create a class that check for the _ctrl.getType().Name bind to switch to find out what control I want to create and so on. I thought there is a more elegant way to do it like here If not let me know – beg-inner Oct 01 '22 at 19:35
  • What you call attributes are called `Properites` in C# and cannot be accessed through via a string name, unless you use reflection, here 'anything' is possible, but you loose the type safety of C#. – Poul Bak Oct 01 '22 at 19:36
  • 1
    And why do you want to do *that*? Why can't you just create controls normally? What I'm suggesting is, that this is an [XY problem](https://xyproblem.info). – Sweeper Oct 01 '22 at 19:39
  • A better approach would be to use generics. A generic method could return an instance of your control, but you can only set common properties. – Poul Bak Oct 01 '22 at 19:39
  • @Poul: can you make a short example ? So it means when trying to reach my goal I lose type safety.. hmm okay thank your for this hint – beg-inner Oct 01 '22 at 19:46
  • You only loose type safety with reflection, not with generics. – Poul Bak Oct 01 '22 at 19:59
  • Do you know that you can simplify construction, like this `TableLayoutPanel _tlp = new TableLayoutPanel { Name = "_tlp", TabStop = false, Dock = DockStyle.Fill };` That is IMHO just as easy as calling some function with parameters. – Poul Bak Oct 01 '22 at 20:04
  • @Sweeper: why not posting a book link which contains input I need to solve this problem while reading / studying it ? – beg-inner Oct 01 '22 at 20:08
  • @Poul: thx for showing simplified way – beg-inner Oct 02 '22 at 07:58

1 Answers1

0

Probably this is what you are trying to say:

void Main()
{
    var _mytlp = new TableLayoutPanel();
    var t = new TextBox();
    SetProperties(_mytlp,
    new Dictionary<string, object>() {
            { "TabStop", false },
            { "Dock", DockStyle.Fill },
            { "BackColor", Color.Yellow},
    });
    Dictionary<string, object> values = new Dictionary<string, object> {
        { "Left", 20 },
        {"Top", 50},
        {"Text", "Sample"},
    };

    SetProperties(t, values);

    var f = new Form();
    _mytlp.Controls.Add(t);
    f.Controls.Add(_mytlp);
    f.Show();
}

private void SetProperties(Control _ctrl, Dictionary<string, object> _attribs)
{
    foreach (var _elem in _attribs)
    {
        _ctrl.GetType().GetProperty(_elem.Key).SetValue(_ctrl, _elem.Value);
    }
}
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
  • if _ctrl is by ref than it looks what I was half looking for. May I have to ask, is it possible to provide only the type of a control and the depending properties as you demonstrates so i can create it within the function and return the complete control ? This List cL = new List(){Panel,TextBox}; is causing error... So how can I simply list different object/control types ? – beg-inner Oct 01 '22 at 20:38
  • @beg-inner, I am afraid you don't know what you are asking. Maybe you should learn the basics of the language before trying to create controls? _ctrl is of course by ref. It is a Control, no? What is Panel, TextBox? Maybe you meant List cL = new List(){new Panel(), new TextBox()}; Why would it be half what you are looking for? Looking at syntax you use, it is more than wjhat you ask for IMHO. – Cetin Basoz Oct 01 '22 at 23:02
  • I guess this is not possible in c# a control type based object without knowing a referenced/bind object on stack - understood I need objects on stack before doing other stuff with them or someone else can tell me how to do it. – beg-inner Oct 02 '22 at 07:34
  • @beg-inner, sorry I don't really understand what you understood and what you are trying to say. As someone said in comments you have an XY problem. – Cetin Basoz Oct 02 '22 at 09:53
  • with new the object is created on stack and I wanted only the type reference ( e.G. TextBox ). guess compilers do not know what to do with TextBox only instead of knowing what to do with new TextBox(). That is what I understood – beg-inner Oct 05 '22 at 14:14
  • @beg-inner, wish I could understand what you are saying. – Cetin Basoz Oct 05 '22 at 14:16
  • if you tell a robot createCar( with { Tyres, Seats, ... } ) it does not know if it should use 14" or 20" tyres and which type of seats because this specific information is missing. While using new Tyres() if its defnied it contains now pre defined values for needed attributes so it could be 18" tyres that is defined as standard. Hope that helps explaining the thing i call type based / reference or just only the type without a specific value based reference as the example above with createCar – beg-inner Oct 06 '22 at 14:26
  • @beg-inner, sorry I don't get your point. var car = new Tyres(); and CreateCar(car) it surely knows what to create. Here car is a known type, just Tyres is not a variable assigned to something then it wouldn't naturally do anything but moreover it wouldn't compile at all. What you ask have solution above. You are trying it to be more complex. – Cetin Basoz Oct 06 '22 at 16:32
  • sorry for my complexity than – beg-inner Oct 07 '22 at 14:32