lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Excellent response guys, thanks for the speedy help.

Philip's code sample works perfectly in isolating each part of the syntax into a lua table. This is nice and flexible and should allow me to do what I need to do

That code snippet wins a prize for brilliance but sure is tricky to follow. I get the gist of it now, but will study it some more to let it digest and filter in

The comments Jerome made about using Color.Yellow instead of "yellow" I am not really too worried about, I like keeping the Lua side terse.
Its only a couple of C# lines to get the color from a string
          KnownColor colorKnown = (KnownColor)Enum.Parse(typeof(KnownColor), (string)propValue, true);
          Color color = Color.FromKnownColor(colorKnown);

Wasnt too sure what you meant with 
"Intuitively I would expect your C#/.NET binding to let you have such a syntax already."

Digressing slightly, I did originally try using LuaInterface to implement some of the simpler syntax command bindings. Here is an interesting example that worked fine without me needing to call my own c# function  (or so I initially thought)

dgv1.Columns["Age"].Width = 400

Great I thought, until I tried

dgv1.Columns["Age"].Width = 400

allocating a width after the form Show, crashed with a Net exception. The reason for the crash is that, executing the width  change after the Show() triggers a cross threading error as I am running Lua from a separate thread to the GUI. Therefore trying to access the dataGridView.width property from a thread that didnt create the control goes bang. Surprisingly, it seems to tolerate that access though if you do it before the Show().  

Hence the need for me to process the Lua syntax such that I can call my own binding function to execute the command.  My own C# function alllows me to Invoke() so that I force it to access the Windows control from the owning GUI thread. 


From: <> on behalf of Jerome Vuarand <>
Sent: 14 February 2016 14:58
To: Lua mailing list
Subject: Re: Question about metamethods

2016-02-14 12:41 GMT+00:00 Geoff Smith <>:
> I am wondering if  __index & __newindex metamethods can be used to solve
> this slightly complex specific problem
> [...]
> My preferred lua syntax mimics the C# line, so I would like to trigger a
> call to that function with something like
>   dgv1.Rows[1].Cells[4].Style.BackColor = "Yellow";
> I suspect this probably isnt possible, can anyone prove me wrong ?

Just in case the other answers to your post are as confusing to you as
they are to me, the simple answer is: yes, it is possible. The above
is equivalent to this:

dgv1["Rows"][1]["Cells"][4]["Style"]["BackColor"] = "Yellow"

So you need dgv1 to have an __index that accepts a string "Rows" as
argument and returns another object. Repeat that for keys 1, "Cells",
4 and "Style". That last Style object should have a __newindex that
accepts the string "BackColor" as key and the string "Yellow" as

> Or
> alternatively suggest another variation on how to solve it ?

You can keep the original syntax for easier porting:

dgv1.Rows[1].Cells[4].Style.BackColor = Color.Yellow

The advantage is you can keep a minimal namespace for the name
"Yellow", instead of having a global association of that name to a
color object.

Intuitively I would expect your C#/.NET binding to let you have such a
syntax already. So before you dismiss it and roll your own, I'd
suggess looking really hard for it.