Notifications
Clear all

[Closed] Struct member access requires instance

I know this has been covered and I’ve been reading through forum posts on the subject of structs, but im still struggling to understand them.

I have a dotnet dll that I am using in max, and I have tried to keep most of it in a struct to keep it neat. I sort of followed the layout of struct from another post on here but im having issues. the example bellow gives me an error when i click the button and the event calls the fn.
Am I getting the error becuase the event is calling the fn from out side of the struct, and it hasnt registered the variable?
the error is
– Runtime error: Struct member access requires instance: click <<

how ever if I was type: ctCamToolsUI.click = true that would work. The issue with that is if I call that function within the initializeControls function at the start it errors and says: – Unknown property: “click” in undefined

any help would be greatly apreciated, this is driving me mad!


(
 local dllDir = "c:\\Temp\\camToolsForm.dll"
 local assembly = dotNetClass "System.Reflection.Assembly"
 local r = assembly.loadfrom dllDir
 global ctFormObject = dotnetobject "camToolsForm.CamToolsForm"
 global ctCamToolsUI
 struct ctCamToolsStr
 (
  --vars
  keyPress = false,
  click = false,
  owner,
  on create do owner = this,
 
  fn cameraCreateSettingsFn =
  (
   print "here"
   click = true
  ),
  fn initializeForm =
  (
   sysPointer = DotNetObject "System.IntPtr" (Windows.GetMAXHWND())
   maxHwnd = DotNetObject "MaxCustomControls.Win32HandleWrapper" sysPointer
   -------------------
   ctFormObject.Show (maxHwnd)
  ),
  fn initializeControls =
  (
   dotnet.addEventHandler ctFormObject.btnCreateCam "Click" cameraCreateSettingsFn
 
   initializeForm()
  ),
  init_trigger = initializeControls(),
  EndOfStruct
 )
 ctCamToolsUI = ctCamToolsStr()
 --ctCamToolsUI.initializeForm()
)
 
2 Replies

you have to pass somehow the pointer to the struct instance to the eventCallback function.
usually people use the object’s tag property. my little changes in your snippet below demonstrate this technique:


(
 global ctCamToolsUI
 struct ctCamToolsStr
 (
  --vars
  keyPress = false,
  click = false,
  owner,
  on create do owner = this,
 
  fn cameraCreateSettingsFn s e =
  (
   print "here"
   s.tag.value.click = true
  ),
  form,
  bt = dotnetobject "Button",
  fn initializeForm =
  (
	form = dotnetobject "Form"
   sysPointer = DotNetObject "System.IntPtr" (Windows.GetMAXHWND())
   maxHwnd = DotNetObject "MaxCustomControls.Win32HandleWrapper" sysPointer
   -------------------
	   
	form.controls.add bt
   form.Show (maxHwnd)
  ),
  fn initializeControls =
  (
    bt.tag = dotnetmxsvalue this
   dotnet.addEventHandler bt "Click" cameraCreateSettingsFn
 
   initializeForm()
  ),
  init_trigger = initializeControls(),
  EndOfStruct
 )
 ctCamToolsUI = ctCamToolsStr()
 --ctCamToolsUI.initializeForm()
)

Thanks for the reply. What you’ve written makes sense, and I can get that to work. I wasnt aware of “dotnetmxsvalue”
Setting the tag of every part of the ui that has an event, would be painful as im not creating the parts in the script.

I think though that if I follow what you have written just apply “this” to the tag of the form and access it through that, it should work. It does in my small test anyway.

I thought about also accessing it through the senders parent but i cant guarantee that the parent will be the form itself. I think the following should work when i use it in anger.
thanks for your help.


(
 local dllDir = "c:\\Temp\\camToolsForm.dll"
 local assembly = dotNetClass "System.Reflection.Assembly"
 local r = assembly.loadfrom dllDir
 global ctFormObject = dotnetobject "camToolsForm.CamToolsForm"
 global ctCamToolsUI
 struct ctCamToolsStr
 (
  --vars
  keyPress = false,
  click = false,
 
  fn cameraCreateSettingsFn sender arg =
  (
   print "here"
   ctFormObject.tag.value.click = true
   print ctFormObject.tag.value.click
 
  ),
  fn initializeForm =
  (
   sysPointer = DotNetObject "System.IntPtr" (Windows.GetMAXHWND())
   maxHwnd = DotNetObject "MaxCustomControls.Win32HandleWrapper" sysPointer
   -------------------
   ctFormObject.Show (maxHwnd)
  ),
  fn initializeControls =
  (
   ctFormObject.tag = dotnetmxsvalue this
   dotnet.addEventHandler ctFormObject.btnCreateCam "Click" cameraCreateSettingsFn
 
   initializeForm()
 
  ),
  init_trigger = initializeControls(),
  EndOfStruct
 )
 ctCamToolsUI = ctCamToolsStr()
 --ctCamToolsUI.initializeForm()
)