Image as property

  • Thread starter Thread starter --== Alain ==--
  • Start date Start date
A

--== Alain ==--

Hi,

I'm creating my own .NET control and i would like to allow user to
allocate an image as property.

for that i used the following code :

#pragma region public Property : ImageSortAscendant
// Allow user to select the image which will be displayed for column
sorted ascendantly
[Description("Set image (8x8) to represent ascending sorting which will
appear onto sorted column header")]
[CategoryAttribute("Appearance")]
property Image^ ImageSortAscendant
{
Image^ get()
{
return m_ImageSortA;
}
void set(Image^ value)
{
if(value->Height == 8 && value->Width == 8)
{
m_ImageSortA = value;
}
}
}
#pragma endregion

However, i have a stupid runtime error.
"An unhandled exception of type 'System.NullReferenceException' occurred
in System.Drawing.dll
Additional information: Object reference not set to an instance of an
object."

so i do not have any instance of my object...but about which object ?
because this error occurs on line ==> if(value->Height == 8 &&
value->Width == 8)

therefore i suppose that it is relative to value object which is not
instanciated as Image ^value = gcnew Image();

however, i've never seen in any book instanciation for such thing at
this stage of component.

please, could you tell me what to do ?
thx.

Al.
 
Hi Alain,

--== Alain ==-- said:
Hi,

I'm creating my own .NET control and i would like to allow user to
allocate an image as property.

for that i used the following code :

#pragma region public Property : ImageSortAscendant
// Allow user to select the image which will be displayed for column
sorted ascendantly
[Description("Set image (8x8) to represent ascending sorting which will
appear onto sorted column header")]
[CategoryAttribute("Appearance")]
property Image^ ImageSortAscendant
{
Image^ get()
{
return m_ImageSortA;
}
void set(Image^ value)
{

Check if value is nullptr before using it.
if(value->Height == 8 && value->Width == 8)
{
m_ImageSortA = value;
}
}
}
#pragma endregion

However, i have a stupid runtime error.
"An unhandled exception of type 'System.NullReferenceException' occurred
in System.Drawing.dll
Additional information: Object reference not set to an instance of an
object."

so i do not have any instance of my object...but about which object ?
because this error occurs on line ==> if(value->Height == 8 &&
value->Width == 8)

Are you sure about where the exception occurs?
If yes, of what type is value?

Do you own the code which uses your control? How do you set the property?
 
Hi Sven,
Check if value is nullptr before using it.
Yes i set it to nullptr in component constructor to initialize it.
Are you sure about where the exception occurs?
If yes, of what type is value?
value is the same type as property. it means Image^ but as for value,
undefined..
I solved it by writting m_ImageSortA = value; before to make all tests
on it.

but if i do that, i can not have null value for this property once it
has been set :-(

Do you own the code which uses your control? How do you set the property?
yes, as i'm the component creator ;-)
 
Hi Alain,

--== Alain ==-- said:
Hi Sven,

Yes i set it to nullptr in component constructor to initialize it.

value is the same type as property. it means Image^ but as for value,
undefined..

I was wondering if some derived class was passed in which might produce
problems in your case.
I solved it by writting m_ImageSortA = value; before to make all tests on
it.

Hmm, sounds strange. Why should using value throw and using m_ImageSortA
which got value assigned not throw? Do you have other threads which might
try to access m_ImageSortA while it is still nullptr?
but if i do that, i can not have null value for this property once it has
been set :-(

Why not? Just check m_ImageSortA != nullptr before you use it to check its
properties. Did I missunderstand that?
yes, as i'm the component creator ;-)

I understood that you are the component creator but do you also write the
code which uses your component? What values are assigned to
ImageSortAscendant? Might the component user cause the exception by using
the component in an unexpected way?
 
Back
Top