Solved

Problem using 'this' in static declaration

Posted on 2011-09-04
4
256 Views
Last Modified: 2012-06-27

This question relates to a related solution already submitted (please see below for link)
..............................................................................................................................

When I come to implement Neun123's solution into my code, I encounter compilation problems adapting my existing program, in particular with reference to the following line in the example code:
Form2Obj = gcnew Form2((CPPForms::IForm1^)this);

1) In my version, the equivalent of Form2Obj is declared as public:static, resulting in the compiler error:
" 'this' : can only be referenced inside non-static member functions".

2) The reason it is declared as static is because otherwise I get the following error:
"only static data members can be initialized inside a ref class or value type".

Hence, I get into a circular compiler issue.

I'm guessing the discrepancy between Neun123's solution and my implementation is a question of a compiler switch '/clr:pure'.  Is that the issue, or am I barking up the wrong tree?

How can I refer to 'this' without using the 'this' keyword?   Is there another way round this?
0
Comment
Question by:johnny_device
  • 2
4 Comments
 
LVL 9

Expert Comment

by:Orcbighter
ID: 36482805
When refering to a function within a static class you just reference that function using the class namespace.

example

static class Thinghy
{
   public bool IsOK() { return true; }

   public int DoSomething( int param1, int param2 )
   {
     int answer = 0;
     /// blah blah
    return answer;
}

Now, to access a member function

...
 if ( Thingy::IsOK() )
{
    int a = 5, b = 6;
   int ans = Thingy::DoSomething( a, b );

}
0
 

Author Comment

by:johnny_device
ID: 36491741

Hi Orcbighter, appreciate your speedy response.  However, I'm not totally clear how it relates to my particular problem.  It looks to me as though I'm not trying to access a function within the Class; what I need to do is to pass the Class itself as a parameter to a separate Form.  The syntax will not allow me to use 'this' to refer to "this Form Class", so how do I get around it?  I tried putting the class namespace itself as a parameter, but that didn't work either.

Can you tell me more literally what I would replace 'this' with in the following snippet:
Form2Obj = gcnew Form2((CPPForms::IForm1^)this);

Thanks, jd
0
 
LVL 3

Accepted Solution

by:
Neun123 earned 500 total points
ID: 36516188
Hi johnny,

the problem you are experiencing is because static initializers are called before the "this" variable could exist.

As described here : http://msdn.microsoft.com/en-us/library/aa645612%28v=vs.71%29.aspx the order is as follows:

1. static variable initializers
2. static constructors
3. base class variable initializers
4. base class constructor
5. variable initializers
6. constructor

[Reading what's written here: http://weblogs.asp.net/psteele/archive/2006/02/22/438840.aspx the other between 4 and 5 varies for VB.Net and C#, so it's hard to tell which applies to Managed C++.]

The "this" variable is set at 5. The code you show is run at 1, hence the issue. If you look back at my code, you'll see that the instanciation of Form2 is inside the constructor of Form1, which is run at 6, after "this" is set.
0
 

Author Closing Comment

by:johnny_device
ID: 36525458
Thank you again, Neun123.  I am beginning to see that the application of logic can make things seem more logical.

You are the worthy winner of the points.


Regards,



jd
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now