__property oddity
-
Actually, I copied the property into my code and it compiles fine sans the content. It's the content of the property that's the problem. I don't know if James has solved it or not. Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
Tom Archer wrote: I don't know if James has solved it or not. He hasnt. Not yet. I think he's going to post the code snippets now. Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
Tom Archer wrote: I don't know if James has solved it or not. He hasnt. Not yet. I think he's going to post the code snippets now. Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
Thanks, Nish. I saw that he had posted responses to other posts after I answered you. Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
-
Here is the offending code
// ScreenSaverUtils.h
#pragma once
#pragma unmanaged
#include#pragma managed
#using // For System.Diagnostics.Debug class
using namespace System;
using namespace System::Drawing;namespace ScreenSaverUtils
{
public __gc class OptimizedBitmap : public IDisposable
{
private:
OptimizedBitmap(void)
{
}public: OptimizedBitmap(Graphics \*g, Bitmap \*bmpToCopy ) : \_disposed(false) { // Copy bitmap } ~OptimizedBitmap(void) { Dispose(); } void Dispose() { // Free resources } \_\_property Graphics\* get\_Graphics() { System::Diagnostics::Debug::Assert(!\_disposed); return Graphics::FromImage(bitmap); } \_\_property Bitmap\* get\_Bitmap() { System::Diagnostics::Debug::Assert(!\_disposed); return dynamic\_cast(bitmap->Clone()); } private: bool \_disposed; HDC hdc; HBITMAP hbitmap; Graphics \*graphics; Bitmap \*bitmap; };
}
Thanks for everyone's help :) [Edit: I'm no longer interested in using those two as properties because the original intent has changed; but I still want to know why its failing to compile :)] James Simplicity Rules!
Actually, depending on how you arrange this code, it comes back with varying results. If you move the variable declarations to the top of the class, most of the problems disappear. This doesn’t give me a whole lot of faith in MC++. There seems to be a conflict here though, but nothing is jumping out at me as anything being wrong with the class structure. Note: Variable names should not start with an underscore in C/C++; those are reserved for compiler vendors. The following compiles.
public __gc class OptimizedBitmap : public IDisposable
{
private:
bool disposed;
HDC hdc;
HBITMAP hbitmap;
Graphics *graphics;
Bitmap *bitmap;OptimizedBitmap() { }
public:
OptimizedBitmap(Graphics *g, Bitmap *bmpToCopy ) :
disposed(false)
{
// Copy bitmap
}
~OptimizedBitmap(void)
{
Dispose();
}
void Dispose() {
// Free resources
}
__property Graphics* get_Graphics()
{
return Graphics::FromImage(bitmap);
}
__property Bitmap* get_Bitmap()
{
#if 0
return dynamic_castSystem::Drawing::Bitmap\*(bitmap->Clone());
#else // or
return new System::Drawing::Bitmap(bitmap);
#endif
}
}; -
Actually, depending on how you arrange this code, it comes back with varying results. If you move the variable declarations to the top of the class, most of the problems disappear. This doesn’t give me a whole lot of faith in MC++. There seems to be a conflict here though, but nothing is jumping out at me as anything being wrong with the class structure. Note: Variable names should not start with an underscore in C/C++; those are reserved for compiler vendors. The following compiles.
public __gc class OptimizedBitmap : public IDisposable
{
private:
bool disposed;
HDC hdc;
HBITMAP hbitmap;
Graphics *graphics;
Bitmap *bitmap;OptimizedBitmap() { }
public:
OptimizedBitmap(Graphics *g, Bitmap *bmpToCopy ) :
disposed(false)
{
// Copy bitmap
}
~OptimizedBitmap(void)
{
Dispose();
}
void Dispose() {
// Free resources
}
__property Graphics* get_Graphics()
{
return Graphics::FromImage(bitmap);
}
__property Bitmap* get_Bitmap()
{
#if 0
return dynamic_castSystem::Drawing::Bitmap\*(bitmap->Clone());
#else // or
return new System::Drawing::Bitmap(bitmap);
#endif
}
};Thanks Neil, i'll put this through its run later, but now I have some anime to watch :) James Simplicity Rules!
-
Thanks Neil, i'll put this through its run later, but now I have some anime to watch :) James Simplicity Rules!
This is interesting, I just created a new project and found this... This compiles...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
};
This does not...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;
};I would bet there is a bug report on this, or I am missing something very fundamental that came in on the heals of MC++.
-
This is interesting, I just created a new project and found this... This compiles...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
};
This does not...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;
};I would bet there is a bug report on this, or I am missing something very fundamental that came in on the heals of MC++.
That would explain why it worked on my machine then. Very strange... Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
-
This is interesting, I just created a new project and found this... This compiles...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
};
This does not...
namespace VNK {
using namespace System;
using namespace System::Drawing;public __gc class Foo
{
public:
Foo();
~Foo();\_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; }
private:
Graphics *m_pGraphics;
Bitmap *m_pBitmap;
};I would bet there is a bug report on this, or I am missing something very fundamental that came in on the heals of MC++.
I don't think it's a bug. To me it is looks quite obvious that the scope resolution should fail in the second case as it tries to resolve the token Graphics in the local scope and it turns out to be a property name where property name is not expected. Solve this using
using namespace System; using namespace System::Drawing; public \_\_gc class Foo { public: Foo(); ~Foo(); \_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; } private: System::Drawing::Graphics \*m\_pGraphics; System::Drawing::Bitmap \*m\_pBitmap; };
And for all the C# haters, C# compiler is much smarter in this regard.
-
That would explain why it worked on my machine then. Very strange... Cheers, Tom Archer Author, Inside C# A total abstainer is one who abstains from everything but abstention, and especially from inactivity in the affairs of others.
Tom Archer wrote: Very strange... It is indeed, before Christian tore me away from my research, I didn't see anything that would indicate variable declarations need to go at the top of the file. Unfortunately all the __property examples I've seen use the same one slightly modified:
__property int get_Size() { return 0; } __property void set_Size() { }
X| Once I get my mind back in shape I'll take a further look :) James Simplicity Rules! -
I don't think it's a bug. To me it is looks quite obvious that the scope resolution should fail in the second case as it tries to resolve the token Graphics in the local scope and it turns out to be a property name where property name is not expected. Solve this using
using namespace System; using namespace System::Drawing; public \_\_gc class Foo { public: Foo(); ~Foo(); \_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; } private: System::Drawing::Graphics \*m\_pGraphics; System::Drawing::Bitmap \*m\_pBitmap; };
And for all the C# haters, C# compiler is much smarter in this regard.
Rama Krishna wrote: private: System::Drawing::Graphics *m_pGraphics; System::Drawing::Bitmap *m_pBitmap; :omg: :omg: Thats it, I quit! ;P James Simplicity Rules!
-
I don't think it's a bug. To me it is looks quite obvious that the scope resolution should fail in the second case as it tries to resolve the token Graphics in the local scope and it turns out to be a property name where property name is not expected. Solve this using
using namespace System; using namespace System::Drawing; public \_\_gc class Foo { public: Foo(); ~Foo(); \_\_property Graphics\* get\_Graphics() { return m\_pGraphics; } \_\_property Bitmap\* get\_Bitmap() { return m\_pBitmap; } private: System::Drawing::Graphics \*m\_pGraphics; System::Drawing::Bitmap \*m\_pBitmap; };
And for all the C# haters, C# compiler is much smarter in this regard.
Actually, I think you may be correct, that’s what I get for looking at these things at 3:30 in the morning...