Class return Nothing

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
 
H

Herfried K. Wagner [MVP]

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?
 
L

Larry Serflaten

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
 
B

Bernard Bourée

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
 
B

Bernard Bourée

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?
 
C

Cor Ligthert

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
 
L

Larry Serflaten

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
 
B

Bernard Bourée

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
 
C

Cor Ligthert

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
 
B

Bernard Bourée

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.
 
C

Cor Ligthert

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
 
B

Bernard Bourée

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 !
 
C

Cor Ligthert

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top