On 07/12/2007, Peter Ritchie
<[EMAIL PROTECTED]> wrote:
> On Fri, 7 Dec 2007 00:50:06 +1030, Mark Hurd <[EMAIL PROTECTED]> wrote:
>
> >Following on from the thread "another generic query", what is the
> >simplest way to implement Nullable(Of String) when that IS what you
> >want.
> >
> >E.g. You have a String property and setting it to any value, including
> >Nothing, needs to be differentiated from its default value say.
> >
> >I decided to create
> >
> >Structure Box(Of T)
> > Public Value As T
> >End Structure
> >
> >Then I can use Nullable(Of Box(Of String))
> >
> >which isn't that convoluted...
> >
> >Does Box exist somewhere in the framework?
> >
> >Is there a better way?
> >
> >Regards,
> >Mark Hurd, B.Sc.(Ma.)(Hons.)
> >
> >PS The more complete solution I'm using is:
> >
> >Structure Box(Of T)
> > Public Sub New(ByVal Value As T)
> >  Me.Value = Value
> > End Sub
> > Public ReadOnly Value As T
> >End Structure
> >...
> >Private SomeField As Nullable(Of Box(Of String))) = Nothing
> >...
> >If SomeField.HasValue Then Use(SomeField.Value.Value)
> >
> >===================================
>
> null/Nothing is a String variable's default value...  If you're overriding
> what "default" means in your particular context, then you'll also have to
> override comparison against your default.

Understood. It's a 'three' valued logic Default, Nothing, AnyString.

> You cannot use a reference type (like String) with Nullable<T>, so there
> isn't away to do with Nullable<T>.   You'll have to write an new class to
> handle that.  You can also not box a String (i.e. boxing a String does
> nothing).

You do realise the code above works, and I just called the Box
Structure that because it is performing a similar function to value
boxing.

> But, as Barry points out, why?

When the default 'unmodified' value needs to be differentiated from Nothing.

An actual use of this construct with T=Object is when asynchronously
calling an overloaded pair of methods where it is not documented what
the default value is when an Object is not specified. (Specifically
InvokeAsync internal to a web service.)

Most of my processing is shared, but to tell the difference between my
callers specifying the Object or not, I can use this construct and
call the relevant overload as needed. (I'm actually Shadowing Invoke
and the two InvokeAsync methods to adjust the web service Url just
prior to the web service call.)

Obviously I can have a separate Boolean UserStateProvided, but that's
what a Nullable encapsulates :-(

Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to