473,387 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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.StretchIma ge;

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
Nov 13 '08 #1
6 8425
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.StretchIma ge;

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
Nov 13 '08 #2
>On Nov 13, 8:53*am, Hans Kesting <news.han...@spamgourmet.comwrote:
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
Nov 13 '08 #3
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
Nov 13 '08 #4
Marc Gravell wrote:
>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};
This should be clearer:

(... as Nullable<{type}>).GetValueOrDefault(...);
>
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

Nov 13 '08 #5
>On Nov 13, 9:44*am, Marc Gravell <marc.grav...@gmail.comwrote:
Ooh, that is evil...
sorry...want's trying to be "evil", just confused.
thanks for all your help.
Nov 13 '08 #6
"GiJeet" <gi****@yahoo.comwrote in message
news:88**********************************@o4g2000p ra.googlegroups.com...
>On Nov 13, 9:44 am, Marc Gravell <marc.grav...@gmail.comwrote:
>Ooh, that is evil...
sorry...want's trying to be "evil", just confused.
thanks for all your help.
No, he meant the ? ?? syntax was evil.
Nov 13 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Roger Leigh | last post by:
I've written a simple container template class to contain a single value. This emits a signal when the value is changed (it's used as a notifier of changes), and listeners can connect to its...
5
by: xuatla | last post by:
Hi, I encountered the following compile error of c++ and hope to get your help. test2.cpp: In member function `CTest CTest::operator+=(CTest&)': test2.cpp:79: error: no match for 'operator='...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
47
by: Roger Lakner | last post by:
I often see operator implemented something like this: class Foo { ... }; class FooList { public: const Foo& operator (unsigned index) const {return array;}; Foo& operator (unsigned index) ...
13
by: Protoman | last post by:
Here's a non intrusive reference counting smart pointer class I'm working on; I keep getting a "22 C:\Dev-Cpp\SmrtPtr.hpp ISO C++ forbids declaration of `SmrtPtrDB' with no type" error. Code: ...
6
by: Bill foust | last post by:
I'm running into a situation there I think an operator overload would solve the issue, but I'm unable to make it work for some reason. If anyone can help here I would appreciate it. I have a...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
2
by: hcaptech | last post by:
This is my Test.can you help me ? 1.Which of the following statement about C# varialble is incorrect ? A.A variable is a computer memory location identified by a unique name B.A variable's name...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.