Help: The as operator must be used with a reference type ??

  • Thread starter Thread starter GiJeet
  • Start date Start date
G

GiJeet

Problem using AS operator to cast Tag property

Hello, I’m using the Tag property of a menu item to hold an enum value
of a PictureBoxSizeMode.

Eg: this.menuImageStretch.Tag =
System.Windows.Forms.PictureBoxSizeMode.StretchImage;

now in the ProcessImageClick method I check to make sure the Tag
propety is not null and cast from type Object to PictureBoxSizeMode
enum. The code below works as long as the value in Tag is
PictureBoxSizeMode enum value but when not...error.

private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
if (item.Tag != null)
{
pbxPhoto.SizeMode = (System.Windows.Forms.PictureBoxSizeMode)
item.Tag;
}
}

However, since the Tag property is of type object it can hold ANY
value so I want to prevent a runtime error and us the AS operator to
cast the tag property value to type
System.Windows.Forms.PictureBoxSizeMode but it throws an error: Error
1 The as operator must be used with a reference type
('System.Windows.Forms.PictureBoxSizeMode' is a value type)

so if I use: item.Tag as System.Windows.Forms.PictureBoxSizeMode //
throws error above

How to cast beforehand to prevent runtime error?

Thx
G
 
It happens that GiJeet formulated :
Problem using AS operator to cast Tag property

Hello, I’m using the Tag property of a menu item to hold an enum value
of a PictureBoxSizeMode.

Eg: this.menuImageStretch.Tag =
System.Windows.Forms.PictureBoxSizeMode.StretchImage;

now in the ProcessImageClick method I check to make sure the Tag
propety is not null and cast from type Object to PictureBoxSizeMode
enum. The code below works as long as the value in Tag is
PictureBoxSizeMode enum value but when not...error.

private void ProcessImageClick(ToolStripItemClickedEventArgs e)
{
ToolStripItem item = e.ClickedItem;
if (item.Tag != null)
{
pbxPhoto.SizeMode = (System.Windows.Forms.PictureBoxSizeMode)
item.Tag;
}
}

However, since the Tag property is of type object it can hold ANY
value so I want to prevent a runtime error and us the AS operator to
cast the tag property value to type
System.Windows.Forms.PictureBoxSizeMode but it throws an error: Error
1 The as operator must be used with a reference type
('System.Windows.Forms.PictureBoxSizeMode' is a value type)

so if I use: item.Tag as System.Windows.Forms.PictureBoxSizeMode //
throws error above

How to cast beforehand to prevent runtime error?

Thx
G

You could test with the "is" operator whether that Tag really holds a
PictureBoxSizeMode.

An other option could be (C#2 and on)

pbxPhoto.SizeMode = item.Tag as PictureBoxSizeMode? ??
PictureBoxSizeMode.Normal;

That is:
* use "as" to cast to a Nullable<PictureBoxSizeMode>,
* then use the "??" operator to convert a possible null value to a
default value (I have chosen "Normal")

Hans Kesting
 
That is:
* use "as" to cast to a Nullable<PictureBoxSizeMode>,
* then use the "??" operator to convert a possible null value to a  
default value (I have chosen "Normal")

Hans, thanks for responding. I'm a little confused. I see 3 question
marks...please explain.
Thanks again!
G
 
I'm a little confused. I see 3 question marks...please explain.

Ooh, that is evil...

PictureBoxSizeMode? means Nullable<PictureBoxSizeMode> - i.e. it can be
either a PictureBoxSizeMode or null (but nothing else).

?? is the null-coalescing operator; the first non-null value is used as
the result.

To be honest, I probably would *not* recommend the use of the suggested
syntax:

as {type}? ?? {default};

While terse (and quite clever), IMO this adds confusion. Which is never
a good idea. I would simply use the longer:

PictureBoxSizeMode result;
if(val != null && val is PictureBoxSizeMode) {
result = (PictureBoxSizeMode) val;
} else {
result = PictureBoxSizeMode.Normal; // etc
}

YMMV

Marc
 
Marc said:
Ooh, that is evil...

PictureBoxSizeMode? means Nullable<PictureBoxSizeMode> - i.e. it can
be either a PictureBoxSizeMode or null (but nothing else).

?? is the null-coalescing operator; the first non-null value is used
as the result.

To be honest, I probably would *not* recommend the use of the
suggested syntax:

as {type}? ?? {default};

This should be clearer:
 
Back
Top