Which Is the Object Here?

R

rn5a

Consider the following code:

-------------------------------------------
Class MyInt
Public MyValue As Integer
End Class

Sub Page_Load(..........)
Dim x As New MyInt
Dim y As New MyInt

x.MyValue = 15
y = x

y.MyValue = 30
Response.Write(x.MyValue)
End Sub
-------------------------------------------

Please note the line

x.MyValue = 15

This line assigns a value to the object but which is the object here -
MyValue, MyInt or x or x.MyValue?

Thanks,

Ron
 
R

rn5a

Consider the following code:

-------------------------------------------
Class MyInt
Public MyValue As Integer
End Class

Sub Page_Load(..........)
Dim x As New MyInt
Dim y As New MyInt

x.MyValue = 15
y = x

y.MyValue = 30
Response.Write(x.MyValue)
End Sub
-------------------------------------------

Please note the line

x.MyValue = 15

This line assigns a value to the object but which is the object here -
MyValue, MyInt or x or x.MyValue?

Thanks,

Ron

ADDENDUM:
==========

Also please note the line

Dim x As New MyInt

If I am not wrong, the above line creates space for "x" (allocates a
memory location to "x") & returns a pointer. To whom does it return
the pointer?
 
R

raju

ADDENDUM:
==========

Also please note the line

Dim x As New MyInt

If I am not wrong, the above line creates space for "x" (allocates a
memory location to "x") & returns a pointer. To whom does it return
the pointer?- Hide quoted text -

- Show quoted text -

Hai Ron

According to my knowledge, for your first question, I think
x.myvalue is an object.

Because in .NET everything is an object.

For the second question you are using "New" keyword, so it
creates an instance of the myInt class and returns the reference to x.

If it is wrong, then please explain.

Regards,
Raj
 
P

Patrice

MyInt is a class, x is an object, x.MyValue is an integer.

Basically an object is a pointer. More precisely the New keyword allocates
space for an object instance of the MyInt class. The location of this space
is then returned to the x variable. From a programmer's point of view x is
the object (see this as a "symbol" that represents the object), technically
x is a pointer that points to the object.

http://msdn2.microsoft.com/en-us/library/b86b82w0(VS.80).aspx could help
(Object-Oriented Programming in Visual Basic).
 
R

raju

MyInt is a class, x is an object, x.MyValue is an integer.

Basically an object is a pointer. More precisely the New keyword allocates
space for an object instance of the MyInt class. The location of this space
is then returned to the x variable. From a programmer's point of view x is
the object (see this as a "symbol" that represents the object), technically
x is a pointer that points to the object.

http://msdn2.microsoft.com/en-us/library/b86b82w0(VS.80).aspxcould help
(Object-Oriented Programming in Visual Basic).

--
Patrice

<[email protected]> a écrit dans le message de (e-mail address removed)...










- Show quoted text -

Hai Ron,

Thank you!!!
 
R

rn5a

MyInt is a class, x is an object, x.MyValue is an integer.

Basically an object is a pointer. More precisely the New keyword allocates
space for an object instance of the MyInt class. The location of this space
is then returned to the x variable. From a programmer's point of view x is
the object (see this as a "symbol" that represents the object), technically
x is a pointer that points to the object.

http://msdn2.microsoft.com/en-us/library/b86b82w0(VS.80).aspxcould help
(Object-Oriented Programming in Visual Basic).

--
Patrice

<[email protected]> a écrit dans le message de (e-mail address removed)...










- Show quoted text -

Patrice, you have said that "More precisely the New keyword allocates
space for an object instance of the MyInt class" Do you mean to say
that the New keyword allocates space for the object "x"?

You have also said that "the location of this space is then returned
to the x variable". You have also said that "x" is the object. Now
since the location of this space is returned to the variable "x", is
"x" a pointer or is it the object or is it both?

Getting confused....

Thanks,

Ron
 
B

bruce barker

its easiier to see if broken up

dim x as MyInt 'declare and allocatate a MyInt object pointer (on the stack)

x = new MyInt 'allocate a new MyInt object in the heap and assign
location to pointer

x.MyValue = 15 'copy an integer value to integer value


the trick here is integers are valuetypes not objects. instead of a
pointer to an integer, MyValue as space allocated in the objects memory
to store the integer.

now when passing an integer to a method, it be be passed by value, the
parameter is the actual value, or passed by ref, the parameter is
pointer to integers memory location.

-- bruce (sqlwork.com)
 
B

bruce barker

..net has two major classes of memory objects (there are more but lets
keep it simple).

the first is a ValueType. ValueTypes are primitive datatypes like ints,
floats, datetime, enum, etc. ValueType have their storage directly
allocated because the size is know. also they can not be set to null.
ValueTypes can be copied, and passed as parameters to a method.

the second is objects, actually class instances. these are allocated in
the heap and managed by the GC. They can not be copied. Memory for a
class instance is create by the new operator.

variables are always valuestypes. if the variable is defined as a
valuetype, then it represents the value. if the variable is defined as
an object, then its a valuetype of object pointer.

if an object variable points to a valuetype, the valuetype is boxed (an
object wrapper is automatically created) and the variable points to the
object wrapper instance.

some examples:

myClass y = new myClass();
myClass x = y;

y points to new myClass instance allocated by the new operator. x
point's to the same class instance because it got a copy of y.

int y = 1;
int x = y;

y is valuetype of int, and integer value 1 is copied to it. the value of
y is copied to x.



-- bruce (sqlwork.com)
 

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

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top