开发者

Array of structures CLI

开发者 https://www.devze.com 2022-12-26 00:31 出处:网络
public value struct ListOfWindows { HWND hWindow; int winID; String^ capName; }; thats my structure now i have created an array of them:
public value struct ListOfWindows
{
 HWND hWindow;
 int winID;
 String^ capName;
};

thats my structure now i have created an array of them:

array<ListOfWindows ^> ^ MyArray =  gcnew array<ListOfWindows ^>(5);

now t开发者_JAVA技巧o test if that works i made a simple function:

void AddStruct( )
{
  HWND temp = ::FindWindow( NULL, "Test" );
  if( temp == NULL ) return;
  MyArray[0]->hWindow = temp; // debug time error..

  return;
}

ERROR: An unhandled exception of type 'System.NullReferenceException' occurred in Window.exe

Additional information: Object reference not set to an instance of an object.

dont know what to do.. kinda new to CLI so if you can help please do.. Thanks.


Well, you have an array of references to objects but I don't see any code that actually puts an object into any of them. Before accessing MyArray[0] you might want to put an object into the array at position 0.

I would change ListOfWindows to be a ref class - in your context it doesn't make much sense to use it as a value class - and then you can add an object to the array like this:

MyArray[0] = gcnew ListOfWindows;

(Untested but that's more or less how it should work). Once you've actually added that object you can then interact with it.


First you're creating an array of references not an array of values, so as @timo-geusch says you need to create those objects after creating your array of references.

However you could also create an array of values like this.

array<ListOfWindows>^ MyArray =  gcnew array<ListOfWindows>(5);

Then you'd access those values with the . operator instead of -> operator, like this.

void AddStruct( )
{
    HWND temp = ::FindWindow( NULL, "Test" );
    if( temp == NULL ) return;
    MyArray[0].hWindow = temp; // << here you access the value type, not the reference
    return;
}

Hope that helps

0

精彩评论

暂无评论...
验证码 换一张
取 消