473,396 Members | 1,865 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,396 software developers and data experts.

Class return Nothing

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
be*****@bouree.net
Nov 21 '05 #1
15 1524
"Bernard Bourée" <be*****@bouree.net> schrieb:
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?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #2
Bernard,
What is wrong ?


I think this
\\\
Return moVal
///

I hope this helps?

Cor
Nov 21 '05 #3

"Bernard Bourée" <be*****@bouree.net> wrote
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

Nov 21 '05 #4
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

--
Bernard Bourée
be*****@bouree.net
"Larry Serflaten" <se*******@usinternet.com> a écrit dans le message de
news:uu**************@TK2MSFTNGP15.phx.gbl...

"Bernard Bourée" <be*****@bouree.net> wrote
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

Nov 21 '05 #5
Thanks Herfried!!

I'm really stupid!
--
Bernard Bourée
be*****@bouree.net
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> a écrit dans le
message de news:%2***************@TK2MSFTNGP11.phx.gbl...
"Bernard Bourée" <be*****@bouree.net> schrieb:
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?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #6
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
Nov 21 '05 #7

"Bernard Bourée" <be*****@bouree.net> wrote
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

Nov 21 '05 #8
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 Bourée
be*****@bouree.net
"Larry Serflaten" <se*******@usinternet.com> a écrit dans le message de
news:us**************@TK2MSFTNGP10.phx.gbl...

"Bernard Bourée" <be*****@bouree.net> wrote
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

Nov 21 '05 #9
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

Nov 21 '05 #10
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 Bourée
be*****@bouree.net
"Cor Ligthert" <no************@planet.nl> a écrit dans le message de
news:ev**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #11
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

"Bernard Bourée" <be*****@bouree.net>
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 Bourée
be*****@bouree.net
"Cor Ligthert" <no************@planet.nl> a écrit dans le message de
news:ev**************@TK2MSFTNGP12.phx.gbl...
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


Nov 21 '05 #12
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 Bourée
be*****@bouree.net
"Cor Ligthert" <no************@planet.nl> a écrit dans le message de
news:ut**************@TK2MSFTNGP12.phx.gbl...
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

"Bernard Bourée" <be*****@bouree.net>
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 Bourée
be*****@bouree.net
"Cor Ligthert" <no************@planet.nl> a écrit dans le message de
news:ev**************@TK2MSFTNGP12.phx.gbl...
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



Nov 21 '05 #13
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

Nov 21 '05 #14

"Bernard Bourée" <be*****@bouree.net> wrote
I'm not sure to follow you completly but I'm maybe wrong!


Maybe this will help clear out the fog...

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

LFS
Nov 21 '05 #15
Thanks Larry for this helpful doc.

--
Bernard Bourée
be*****@bouree.net
"Larry Serflaten" <se*******@usinternet.com> a écrit dans le message de
news:%2****************@TK2MSFTNGP15.phx.gbl...

"Bernard Bourée" <be*****@bouree.net> wrote
I'm not sure to follow you completly but I'm maybe wrong!


Maybe this will help clear out the fog...

http://msdn.microsoft.com/msdnmag/is...asicInstincts/

LFS

Nov 21 '05 #16

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

Similar topics

3
by: jamie_m_ | last post by:
I have a custom collection ... clFile that INHERITS from NameObjectCollectionBase the problem is, when I try to create an xmlserializer instance i get an error You must implement a default...
10
by: Sunny | last post by:
Hi, I have an old problem which I couldn't solve so far. Now I have found a post in that group that gave me an idea, but I can not fully understand it. The problem is: I'm trying to use a...
0
by: Daniel Sélen Secches | last post by:
I found a good class to do a simple FTP. Very good.... I'm posting it with the message, i hope it helps someone ============================================================== Imports...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: Peter | last post by:
I want to create a multidemensional arraylist. Seeing as they don't exist I was wondering if there is a way to create a class that works like one. I basically want to use it like this Dim...
9
by: craig.overton | last post by:
All, I am currently developing an FTP class in VB.NET. It's kid tested, mother approved when trying to access an FTP Server on a Windows box meaning I can connect, run commands, upload and...
3
by: Bmack500 | last post by:
I have a sub, and a class. The sub is like this: Sub dothis() Dim aInfoListX As New infoWrapperClass aInfoListX.params = ReadConfig() Dim sqlCMD As New SqlCommand aInfoListX.sqlCmd = sqlCMD ...
1
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object."...
11
by: Jan | last post by:
Hi, I defined a function like this: Public Function myfunction(ByVal myvar As Object) As String ... Return xyz End Function in aspx file, i use it like this:
0
by: windandwaves | last post by:
Hi Folk What do you think about this database class? Do you have any feedback or ideas on how it could be improved? Thank you Nicolaas class dbClass {
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.