Object behavior for simple data types? 
Author Message
 Object behavior for simple data types?

Hi all,

In .NET all data types (Integer, Long, String, ...) are
objects. But in my opinion, these data types don't really
behave like normal objects. I noticed the following
behaviour:

Dim l As Long

Msgbox(l = Nothing)
'Results in True

Msgbox(l = 0)
'Results in True

Even stranger behaviour:

Dim l As Long

l = 0

Msgbox(l = Nothing)
'Results in True

I hoped that in .NET there would be a way to determine if
a variable has been set or not. For example you have a
class with a ID property (long), you would implement it
like this:
Public Class Test
    Private _id As Long
    Public Property ID As Long
        Get
            Return _id
        End Get
        Set(Value As Long)
            _id = Value
        End Set
    End Property
End Class
Suppose you would like to have functionality in this
class, to save the data. In de Save sub, you would like to
check if the ID property was set or not. I hoped you could
do it like this:
Public Sub Save
    If _id = Nothing Then
        'Raise error to tell the ID property was not set.
    Else
        'Save data
    End If
End Save

Was it wrong of me to expect such behaviour, or am I wrong
somewhere?

Thanks,
Jan



Tue, 26 Apr 2005 19:25:58 GMT  
 Object behavior for simple data types?

Quote:
> Hi all,

> In .NET all data types (Integer, Long, String, ...) are
> objects. But in my opinion, these data types don't really
> behave like normal objects. I noticed the following
> behaviour:

> Dim l As Long

> Msgbox(l = Nothing)
> 'Results in True

> Msgbox(l = 0)
> 'Results in True

> Even stranger behaviour:

> Dim l As Long

> l = 0

> Msgbox(l = Nothing)
> 'Results in True

> I hoped that in .NET there would be a way to determine if
> a variable has been set or not. For example you have a
> class with a ID property (long), you would implement it
> like this:
> Public Class Test
>     Private _id As Long
>     Public Property ID As Long
>         Get
>             Return _id
>         End Get
>         Set(Value As Long)
>             _id = Value
>         End Set
>     End Property
> End Class
> Suppose you would like to have functionality in this
> class, to save the data. In de Save sub, you would like to
> check if the ID property was set or not. I hoped you could
> do it like this:
> Public Sub Save
>     If _id = Nothing Then
>         'Raise error to tell the ID property was not set.
>     Else
>         'Save data
>     End If
> End Save

> Was it wrong of me to expect such behaviour, or am I wrong
> somewhere?

In VB.Net, Nothing used with value types, acts like the default value of the
value type, i.e. 0 for numeric values.  Setting a value type to Nothing
means setting it to it's default value, because a value type can never
contain Nothing. "Nothing" means "no reference" but a value type does not
contain a reference.

I don't like this behavior. IMO Nothing should only be used with reference
types, i.e. the compiler should complain (like in C#), or there should be an
option in the future that prohibits using Nothing with value types.

If you want to distinguish between Nothing and a certain numeric value, you
must declare the variable As Object. If you don't like this because the
variable could contain any type of object (not only Nothing or a numeric
value), write a wrapper class containing the value (optimizations on should
even create inline access to the underlying value, so it should not be a big
performance issue). Declare the variable as the type of the wrapper class.

Armin



Tue, 26 Apr 2005 20:20:27 GMT  
 Object behavior for simple data types?
So, can I say that value types (like String, Long, ...)
are no 'real' objects? So I have to deal with them not
like a normal object.

Thanks,
Jan

Quote:
>-----Original Message-----

>> Hi all,

>> In .NET all data types (Integer, Long, String, ...) are
>> objects. But in my opinion, these data types don't
really
>> behave like normal objects. I noticed the following
>> behaviour:

>> Dim l As Long

>> Msgbox(l = Nothing)
>> 'Results in True

>> Msgbox(l = 0)
>> 'Results in True

>> Even stranger behaviour:

>> Dim l As Long

>> l = 0

>> Msgbox(l = Nothing)
>> 'Results in True

>> I hoped that in .NET there would be a way to determine
if
>> a variable has been set or not. For example you have a
>> class with a ID property (long), you would implement it
>> like this:
>> Public Class Test
>>     Private _id As Long
>>     Public Property ID As Long
>>         Get
>>             Return _id
>>         End Get
>>         Set(Value As Long)
>>             _id = Value
>>         End Set
>>     End Property
>> End Class
>> Suppose you would like to have functionality in this
>> class, to save the data. In de Save sub, you would like
to
>> check if the ID property was set or not. I hoped you
could
>> do it like this:
>> Public Sub Save
>>     If _id = Nothing Then
>>         'Raise error to tell the ID property was not
set.
>>     Else
>>         'Save data
>>     End If
>> End Save

>> Was it wrong of me to expect such behaviour, or am I
wrong
>> somewhere?

>In VB.Net, Nothing used with value types, acts like the

default value of the

- Show quoted text -

Quote:
>value type, i.e. 0 for numeric values.  Setting a value
type to Nothing
>means setting it to it's default value, because a value
type can never
>contain Nothing. "Nothing" means "no reference" but a
value type does not
>contain a reference.

>I don't like this behavior. IMO Nothing should only be
used with reference
>types, i.e. the compiler should complain (like in C#), or
there should be an
>option in the future that prohibits using Nothing with
value types.

>If you want to distinguish between Nothing and a certain
numeric value, you
>must declare the variable As Object. If you don't like
this because the
>variable could contain any type of object (not only

Nothing or a numeric
Quote:
>value), write a wrapper class containing the value

(optimizations on should

- Show quoted text -

Quote:
>even create inline access to the underlying value, so it
should not be a big
>performance issue). Declare the variable as the type of
the wrapper class.

>Armin

>.



Tue, 26 Apr 2005 21:11:48 GMT  
 Object behavior for simple data types?

Quote:
> So, can I say that value types (like String, Long, ...)
> are no 'real' objects? So I have to deal with them not
> like a normal object.

Value types *are* objects. An object is a value type or a reference type.
String is not a value type, it's a reference type.

What do you mean with "normal" objects?

The difference between a value type and a reference type is the way the
object is stored in memory.

A variable is a name for a block of memory. If you declare a Byte variable,
the size of the block of memory is 1 byte (surprise). If you take Integer,
it's 4 and it's 8 bytes for Long. And so on. If you define a structure, it's
the same: The block of memory contains all the variables declared within the
structure. If the structure contains two longs, the block of memory occupied
by a variable is 16 bytes in size. => the variable contains the object.

If the type of the variable is a reference type, the size of the block of
memory is always 4 bytes (at least under win32). This is true for String,
Form, Textbox and all other reference types. These 4 bytes (32-Bit-Interger
(or IntPtr)) contain the address of the object. If a variable contains the
value 1234567, the object is stored at the memory location 123467. If the
variable contains the value 0, it does not reference an object. We say that
the variable Is Nothing. => the variable contains a reference to the object.

As you can see, you can not store "Nothing" in a value type because we do
not need a reference to a value type. A byte can have a value between 0 and
255 but nothing else that can tell us that it is Nothing.

In addition, we have boxed value types:

Dim o as object
o = 17L        'Long (8 bytes)
o = new Form1()

What now? What is the size of "o"? o can contain a value type or a reference
type. A value type requires the variable to have the size that the value
type needs. It is 8 bytes for long, but 4 bytes (the reference) for a Form
object. So, how can both objects be stored in the same variable? This is
done by boxing: If you declare a variable "As Object", it's size is always 4
bytes and it contains a reference to the object (or no reference, i.e. it Is
Nothing). After assigning a Long value, o contains a reference to a block of
memory that contains the Long value. After assigning the Form object, it
contains a refernce to a block of memory containing all fields of a Form
object.

Boxing can be used for distinguishing between Nothing and a value type. You
can set o to Nothing or to one of the numeric values you mentioned in your
first posting. As I wrote, the disadvantage is the lack of type safety. This
means, you can store everything in o, not only Nothing or Long.

You can avoid this by writing a wrapper class:
class LongWrapper
    public Value as Long
    Public Sub New(byval VAlue as long)
        me.value = value
    end sub
end class

Now you can declare
    dim wrappedlong as LongWrapper
and assign Nothing or, for example "New LongWrapper(17)"

Just a suggestions. There are (at least) two other solutions:
Write a property (type: object) that does a runtime-check if the passed
value is Nothing or Long. If not, throw an exception. Drawback: No compile
time check.

Second: Write a property (type: Long) and two additional methods:
SetPropertyNothing and IsPropertyNothing.

Both approaches have the same disadavantage: Within the class, you still can
set the field to any type. That's why a wrapper class can be useful.

Armin



Tue, 26 Apr 2005 23:15:41 GMT  
 Object behavior for simple data types?
Armin,

Thank you for your explanation. Now it makes sense to me!
I didn't know that there were value types. I thought only
reference types existed in .NET.

Once again, thanks!

Jan

Quote:
>-----Original Message-----

>> So, can I say that value types (like String, Long, ...)
>> are no 'real' objects? So I have to deal with them not
>> like a normal object.

>Value types *are* objects. An object is a value type or a
reference type.
>String is not a value type, it's a reference type.

>What do you mean with "normal" objects?

>The difference between a value type and a reference type
is the way the
>object is stored in memory.

>A variable is a name for a block of memory. If you

declare a Byte variable,
Quote:
>the size of the block of memory is 1 byte (surprise). If
you take Integer,
>it's 4 and it's 8 bytes for Long. And so on. If you

define a structure, it's
Quote:
>the same: The block of memory contains all the variables
declared within the
>structure. If the structure contains two longs, the block
of memory occupied
>by a variable is 16 bytes in size. => the variable

contains the object.
Quote:

>If the type of the variable is a reference type, the size
of the block of
>memory is always 4 bytes (at least under win32). This is
true for String,
>Form, Textbox and all other reference types. These 4

bytes (32-Bit-Interger
Quote:
>(or IntPtr)) contain the address of the object. If a

variable contains the
Quote:
>value 1234567, the object is stored at the memory

location 123467. If the
Quote:
>variable contains the value 0, it does not reference an
object. We say that
>the variable Is Nothing. => the variable contains a

reference to the object.

- Show quoted text -

Quote:

>As you can see, you can not store "Nothing" in a value
type because we do
>not need a reference to a value type. A byte can have a
value between 0 and
>255 but nothing else that can tell us that it is Nothing.

>In addition, we have boxed value types:

>Dim o as object
>o = 17L        'Long (8 bytes)
>o = new Form1()

>What now? What is the size of "o"? o can contain a value
type or a reference
>type. A value type requires the variable to have the size
that the value
>type needs. It is 8 bytes for long, but 4 bytes (the

reference) for a Form
Quote:
>object. So, how can both objects be stored in the same
variable? This is
>done by boxing: If you declare a variable "As Object",

it's size is always 4
Quote:
>bytes and it contains a reference to the object (or no

reference, i.e. it Is
Quote:
>Nothing). After assigning a Long value, o contains a

reference to a block of
Quote:
>memory that contains the Long value. After assigning the
Form object, it
>contains a refernce to a block of memory containing all
fields of a Form
>object.

>Boxing can be used for distinguishing between Nothing and
a value type. You
>can set o to Nothing or to one of the numeric values you
mentioned in your
>first posting. As I wrote, the disadvantage is the lack

of type safety. This

- Show quoted text -

Quote:
>means, you can store everything in o, not only Nothing or
Long.

>You can avoid this by writing a wrapper class:
>class LongWrapper
>    public Value as Long
>    Public Sub New(byval VAlue as long)
>        me.value = value
>    end sub
>end class

>Now you can declare
>    dim wrappedlong as LongWrapper
>and assign Nothing or, for example "New LongWrapper(17)"

>Just a suggestions. There are (at least) two other
solutions:
>Write a property (type: object) that does a runtime-check
if the passed
>value is Nothing or Long. If not, throw an exception.

Drawback: No compile
Quote:
>time check.

>Second: Write a property (type: Long) and two additional
methods:
>SetPropertyNothing and IsPropertyNothing.

>Both approaches have the same disadavantage: Within the

class, you still can

- Show quoted text -

Quote:
>set the field to any type. That's why a wrapper class can
be useful.

>Armin

>.



Wed, 27 Apr 2005 05:30:24 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Data Types using the Data Object

2. Simple question: Date data types

3. A very simple question -- Data Type Mismatch

4. Very simple question about data types

5. Get error: Disallowed implicit conversion from data type varchar to data type money

6. Data type conversion error when saving numeric data types

7. Q: variant data type with user defined data type

8. C data type vs. VB Data type

9. ADO data types and vbasic data types mapping

10. Data Type Problem accessing Btrieve (MBF) data type

11. Data Type Problem accessing Btrieve (MBF) data type

12. Data Type Problem accessing Btrieve (MBF) data type

 

 
Powered by phpBB® Forum Software