Class return Nothing

  • Thread starter Thread starter Bernard Bourée
  • Start date Start date
B

Bernard Bourée

I have the following code:

Dim mVar As New Variable()

mVar.NomVal = "Test" ==> this line return a NomVal ="Test"

mVar.oVal = 10D ====> but this one return a oVal= NOTHING !!!!

What is wrong ?



Bernard

----------------------------------
Public Class Variable

Private mNomVal As String

Private moVal As Object

Public Property NomVal()

Get

Return mNomVal

End Get

Set(ByVal Value)

mNomVal = Value

End Set

End Property

Public Property oVal() As Object

Get

Return oVal

End Get

Set(ByVal Value As Object)

If TypeOf Value Is Integer Then

moVal = CType(Value, Integer)

ElseIf TypeOf Value Is Short Then

moVal = CType(Value, Short)

ElseIf TypeOf Value Is Long Then

moVal = CType(Value, Long)

ElseIf TypeOf Value Is Decimal Then

moVal = CType(Value, Decimal)

ElseIf TypeOf Value Is Double Then

moVal = CType(Value, Double)

ElseIf TypeOf Value Is Boolean Then

moVal = CType(Value, Boolean)

End If

End Set

End Property
 
Bernard Bourée said:
Dim mVar As New Variable()

mVar.NomVal = "Test" ==> this line return a NomVal ="Test"

mVar.oVal = 10D ====> but this one return a oVal= NOTHING !!!!

What is wrong ?
[...]
Private mNomVal As String

Private moVal As Object
[...]
Public Property oVal() As Object

Get

Return oVal

.... should read 'Return moVal'.
End Get

Set(ByVal Value As Object)

If TypeOf Value Is Integer Then

moVal = CType(Value, Integer)

ElseIf TypeOf Value Is Short Then

moVal = CType(Value, Short)

ElseIf TypeOf Value Is Long Then

moVal = CType(Value, Long)

ElseIf TypeOf Value Is Decimal Then

moVal = CType(Value, Decimal)

ElseIf TypeOf Value Is Double Then

moVal = CType(Value, Double)

ElseIf TypeOf Value Is Boolean Then

moVal = CType(Value, Boolean)

End If

End Set

What's the purpose of the code above?
 
Bernard Bourée said:
Private moVal As Object

If TypeOf Value Is Integer Then

moVal = CType(Value, Integer)

ElseIf TypeOf Value Is Short Then '...
End If


moVal is declared as Object. No matter what type
you cast it to, it will always be an Object, so why
bother trying to cast it?

LFS
 
Larry

The reason is that oVal can effectivly contain different kind of values
(short, int, dbl, etc) and that I need to be able to make the treatment will
all these type
 
Thanks Herfried!!

I'm really stupid!


--
Bernard Bourée
(e-mail address removed)
Herfried K. Wagner said:
Bernard Bourée said:
Dim mVar As New Variable()

mVar.NomVal = "Test" ==> this line return a NomVal ="Test"

mVar.oVal = 10D ====> but this one return a oVal= NOTHING !!!!

What is wrong ?
[...]
Private mNomVal As String

Private moVal As Object
[...]
Public Property oVal() As Object

Get

Return oVal

... should read 'Return moVal'.
End Get

Set(ByVal Value As Object)

If TypeOf Value Is Integer Then

moVal = CType(Value, Integer)

ElseIf TypeOf Value Is Short Then

moVal = CType(Value, Short)

ElseIf TypeOf Value Is Long Then

moVal = CType(Value, Long)

ElseIf TypeOf Value Is Decimal Then

moVal = CType(Value, Decimal)

ElseIf TypeOf Value Is Double Then

moVal = CType(Value, Double)

ElseIf TypeOf Value Is Boolean Then

moVal = CType(Value, Boolean)

End If

End Set

What's the purpose of the code above?
 
Bernard,

I hope you tried my solution because that is the error, however Larry is in
my opinion right, the mainpart of the code has not much sense, returned will
always be an object.

Cor
 
Bernard Bourée said:
The reason is that oVal can effectivly contain different kind of values
(short, int, dbl, etc) and that I need to be able to make the treatment will
all these type

You may need to adjust your view. oVal cannot contain different kinds
of values. It can only contain a reference to an object because it is
declared "As Object". Just like a variable declared "As Integer" will
only contain values that are (accepted as) integers, it cannot contain
anything else.

When you try to stuff different kinds of values into an object variable,
they will all get boxed and moved on to the heap where the object variable
will contain a pointer to that object on the heap.

LFS
 
Larry
I'm not sure to follow you completly but I'm maybe wrong!
Look at the following code.
The function GetTypeVal returns a different answer if in an object I have
placed a short or a decimal.

Maybe there is a much shorter way to achieve that or there is a Net function
but I could not find it!

Public Function GetTypeVal(ByVal oVal As Object) As String

Dim i As Integer

Dim s As Short

Dim b As Boolean

Dim l As Long

Dim dc As Decimal

Dim db As Double

i = oVal : s = oVal : b = oVal : l = oVal : dc = oVal : db = oVal

If i.Equals(oVal) Then

Return "Integer"

ElseIf s.Equals(oVal) Then

Return "Short"

ElseIf b.Equals(oVal) Then

Return "Boolean"

ElseIf l.Equals(oVal) Then

Return "Long"

ElseIf dc.Equals(oVal) Then

Return "Decimal"

ElseIf db.Equals(oVal) Then

Return "Double"

End If

End Function
 
Bernard,

What you do is that you unbox a value from an object and box it again in a
new object.

This
object = object
will in this case give the same result as
object = CDec(object) 'Or with CType(Object,decimal)

I hope this gives an idea where we are talking about

Cor
 
Cor

I thought that an Object could contain anything, that is any "legal" class
defined.
Do you mean that the sign "=" does not affect the content of the object and
does not "transform" it in a decimal in the line
object=CDec(object) ?

But when I look at the variables windows I can see that the value is changed
to a Decimal type value.
So I don't understand.
 
Bernard,

I use forever the Quick watch in the debugger for that.
That shows it very fine.

What you do in my opinion is
dim obj as object = 10D
dim obj1 as object = CDec(obj)
were the last is the same as
dim obj1 as object = 10D
or
dim obj1 as object = obj

You do not do
dim obj1 as decimal = CDec(obj) ' where you are probably after

I hope this makes things clearer, however just try it with the debugger.

Cor
 
Cor

I have run the following code:
Dim ii As Integer = 10

Dim dd As Decimal = 100

Dim ob As Object

ob = CInt(dd) ===> here ob is an integer (dd is decimal)

ob = CDec(ii)===> and here ob is a decimal(ii is int)

So it seams that there is a transformation !
 
Bernard,
Dim ii As Integer = 10
Dim dd As Decimal = 100
Dim ob As Object
ob = CInt(dd) ===> here ob is an integer (dd is decimal)
ob = CDec(ii)===> and here ob is a decimal(ii is int)
So it seams that there is a transformation !

Of course however placed boxed in the object again

But your original code is:

If TypeOf Value Is Integer Then
moVal = CType(Value, Integer)

There is probably a transformation, however it has no sense because it
transforms to the same value in the object again.

That is what we try to tell you.

Cor
 
Back
Top