473,411 Members | 2,154 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,411 software developers and data experts.

Looking for Better Way to Write Property

Greetings,

I often find myself writing public properties in my ASP.NET code and I need
the ability to robustly handle cases where the property has not been set.

So I often end up with code like this:

public Guid PresenterID
{
get
{
object obj = ViewState["PresenterID"];
if (obj == null)
return null;
return (Guid)obj;
}
set { ViewState["PresenterID"] = value; }
}

My get seem overly complex because if I typecast the result of ViewState, I
get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.

I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?

Thanks.

Jonathan

Aug 18 '08 #1
8 1137
return (ViewState["PresenterID"] as Guid);

On Aug 18, 10:08*am, "Jonathan Wood" <jw...@softcircuits.comwrote:
Greetings,

I often find myself writing public properties in my ASP.NET code and I need
the ability to robustly handle cases where the property has not been set.

So I often end up with code like this:

*public Guid PresenterID
*{
* get
* {
* *object obj = ViewState["PresenterID"];
* *if (obj == null)
* * return null;
* *return (Guid)obj;
* }
* set { ViewState["PresenterID"] = value; }
*}

My get seem overly complex because if I typecast the result of ViewState,I
get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.

I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?

Thanks.

Jonathan
Aug 18 '08 #2
Er... Well, actually, looks like that code wouldn't compile since I was
returning null and Guid isn't nullable. More of the same, unfortunately.

Jonathan

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eL**************@TK2MSFTNGP06.phx.gbl...
Greetings,

I often find myself writing public properties in my ASP.NET code and I
need the ability to robustly handle cases where the property has not been
set.

So I often end up with code like this:

public Guid PresenterID
{
get
{
object obj = ViewState["PresenterID"];
if (obj == null)
return null;
return (Guid)obj;
}
set { ViewState["PresenterID"] = value; }
}

My get seem overly complex because if I typecast the result of ViewState,
I get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.

I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?

Thanks.

Jonathan
Aug 18 '08 #3
Maybe you should store Nullable<Guidinto viewstate.

On Aug 18, 10:27*am, "Jonathan Wood" <jw...@softcircuits.comwrote:
Er... Well, actually, looks like that code wouldn't compile since I was
returning null and Guid isn't nullable. More of the same, unfortunately.

Jonathan

"Jonathan Wood" <jw...@softcircuits.comwrote in message

news:eL**************@TK2MSFTNGP06.phx.gbl...
Greetings,
I often find myself writing public properties in my ASP.NET code and I
need the ability to robustly handle cases where the property has not been
set.
So I often end up with code like this:
public Guid PresenterID
{
*get
*{
* object obj = ViewState["PresenterID"];
* if (obj == null)
* *return null;
* return (Guid)obj;
*}
*set { ViewState["PresenterID"] = value; }
}
My get seem overly complex because if I typecast the result of ViewState,
I get an error if it's null. I think I can use the as keyword to return
nullable datatypes whether they are null or not. But for non-nullable data
types, it seems like extra code is needed to workaround C#'s seemingly
arbitrary limitations.
I know it's not a lot of code, but I have to do something like this so
often. Can anyone offer a more efficient way to write this code?
Thanks.
Jonathan
Aug 18 '08 #4
Kalpesh,
return (ViewState["PresenterID"] as Guid);
That would be a nice, simple fix wouldn't it? But it won't compile (Guid is
not nullable).
Maybe you should store Nullable<Guidinto viewstate.
Maybe. I'm not familiar with that. I'll poke around.

Thanks.

Jonathan

Aug 18 '08 #5
On Aug 18, 9:53*pm, "Jonathan Wood" <jw...@softcircuits.comwrote:
Kalpesh,
return (ViewState["PresenterID"] as Guid);

That would be a nice, simple fix wouldn't it? But it won't compile (Guid is
not nullable).
Regardless of whether the value was nullable or not nullable when you
boxed it, you can always unbox it into a nullabe value, and check for
null. So:

ViewState["PresentedID"] = Guid.NewGuid(); // not nullable
...
Guid? maybeGuid = ViewState["PresentedID"] as Guid?; // still works,
null unboxes to null
if (maybeGuid != null)
{
Guid guid = (Guid)maybeGuid;
}
Aug 19 '08 #6
On Tue, 19 Aug 2008 00:05:05 -0700, Pavel Minaev <in****@gmail.comwrote:
Regardless of whether the value was nullable or not nullable when you
boxed it, you can always unbox it into a nullabe value, and check for
null. So:

ViewState["PresentedID"] = Guid.NewGuid(); // not nullable
...
Guid? maybeGuid = ViewState["PresentedID"] as Guid?; // still works,
null unboxes to null
if (maybeGuid != null)
{
Guid guid = (Guid)maybeGuid;
}
All due respect, I don't think the original problem was with regards to
_retrieving_ the value from "ViewState". It was with how to define the
property. It's true that the "as Guid?" can automatically convert the
returned object to a value type. But we're still left with the original
quandary.

Unless the property itself is Nullable<Guid(or some sort of nullable
type, anyway), it can't return null. In the above code, there's still the
question of what to return if "maybeGuid != null" fails to be true. Which
is what the original question was about in the first place.

The answers so far have been reasonable: one way to do it is declare the
property as Guid? (i.e. Nullable<T>) and let the caller deal with it.
Another way would be to return the default value for Guid ("return new
Guid()") instead. Again, the caller would need to check the value and
treat the default value the same as null.

Pete
Aug 19 '08 #7
Pavel,
Regardless of whether the value was nullable or not nullable when you
boxed it, you can always unbox it into a nullabe value, and check for
null. So:

ViewState["PresentedID"] = Guid.NewGuid(); // not nullable
...
Guid? maybeGuid = ViewState["PresentedID"] as Guid?; // still works,
null unboxes to null
if (maybeGuid != null)
{
Guid guid = (Guid)maybeGuid;
}
Thanks, but my problem is really that I need this all the time and I'm
trying to *reduce* the amount of code and make it as simple as possible.

Based on Kalpesh's input, I came up with the following:

protected Guid? PresenterID
{
get { return ViewState["PresenterID"] as Guid?; }
set { ViewState["PresenterID"] = value; }
}

I haven't been using this long, but it appears to work so far. And it's very
short and simple. And I can compare the property to null and determine if
it's set.

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

Aug 19 '08 #8
Peter,
All due respect, I don't think the original problem was with regards to
_retrieving_ the value from "ViewState". It was with how to define the
property. It's true that the "as Guid?" can automatically convert the
returned object to a value type. But we're still left with the original
quandary.

Unless the property itself is Nullable<Guid(or some sort of nullable
type, anyway), it can't return null. In the above code, there's still the
question of what to return if "maybeGuid != null" fails to be true. Which
is what the original question was about in the first place.
Correct. I had done something similar before with and int property and
settled on returning -1 if there was no value, but that wasn't as straight
forward with Guid. Making the property of type Guid? seems to solve the
issue quite nicely, I thought and I will take the same approach with int
properties that may not be set in the future.
The answers so far have been reasonable: one way to do it is declare the
property as Guid? (i.e. Nullable<T>) and let the caller deal with it.
Another way would be to return the default value for Guid ("return new
Guid()") instead. Again, the caller would need to check the value and
treat the default value the same as null.
Right. The first approach and having the caller test against null suited me
best.

Thanks.

Jonathan

Aug 19 '08 #9

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

Similar topics

5
by: Disiac | last post by:
Hi, MS Class Library Guidelines clearly discourages use of Write-Only properties yet they're continuously availed in .NET after VB 6. Personally, I really don't use them anywhere. Tend to find...
0
by: Irena | last post by:
Hi all there, Form time to time, I go back to this project of mine that drives me nuts, now. Sometimes ago, I have been suggested to use DAO insted of ADO for getting the "ROWSOURCE" property...
2
by: Jim Heavey | last post by:
I want to write a routine which will list all of the propetries and all property names for a particular object, say a DataColumn. I there a way for me to do this without manually looking up each...
3
by: Hani Atassi | last post by:
The definition of a read/write property in an interface is similar to the following: Public Interface EiDuty Property Key() As String End Interface If I would like to generate a TLB out of...
1
by: Carl Gilbert | last post by:
Hi Is there any way to enable/disable an item in a property grid at runtime? I have two properties that I am showin in a property gris and based on the validity of the first property, I want...
0
by: jwtulp | last post by:
Hello, I have a question about XmlSerialization. I have a class with a private field called createDate of the type DateTime. In the constructor of my class I use DateTime. Now to create a...
2
by: Ben Voigt | last post by:
The C# Language Specification says: A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write...
25
by: mereba | last post by:
Hello My country Ghana is changing its currency. I want to write a small programme in C++ that can covert from the old currency into the new one. I would like this programme to run behind a simple...
2
by: Michael Schipp | last post by:
Hi all, Does anybody know of a standard event that is call when an application starts? Something like filesystemwatcher that has an executed event? Thanks
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.