memory address of an object

In Java or possibly in C#, objectName.toString() gives the memory address of objectName.

How this is done in vb.net?

The reason behind my question is to find out when an object is being instantiated using different constructors in the following fashion in fact only one object is created or two?

Public Class Car
.
.
    Sub New(ByVal gaslevel As Single, ByVal MPG As Single)
        Me.mGasLevel = gaslevel
        Me.mMPG = MPG
        'MessageBox.Show("memory location by New(2 pram): " & Me.ToString())
    End Sub

    Sub New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single)
        Me.New(gaslevel, MPG)
        Me.mMileage = mileage
        MessageBox.Show("memory location by New(3 pram): " & Me.ToString(`)
    End Sub
.
.
End Class
-------------------------------
Case 1: When New(ByVal gaslevel As Single, ByVal MPG As Single) is called one object is created. No discussion on this.

Case 2: When New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single) is called, if we get:

a) memory location by New(2 pram): d100
    memory location by New(3 pram): d100

One object is created.

b) memory location by New(2 pram): d100
    memory location by New(3 pram): d200

Then, two objects are created. d100 is lost (no longer referenced by the object variable). But d200 is referenced by object variable.

Any thoughts on this?

Thank you.
LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
Various points.
I doubt the ToString gives the location of memory of the object - at best it gives the location of the string.
.Net has dynamic memory management.  That means you might actually get an adress and a few seconds later the object still exist but at a DIFFERENT memory adress.

Sub New(ByVal gaslevel As Single, ByVal MPG As Single)
        Me.mGasLevel = gaslevel
        Me.mMPG = MPG
        'MessageBox.Show("memory location by New(2 pram): " & Me.ToString())
    End Sub

That fills in one object.

    Sub New(ByVal gaslevel As Single, ByVal MPG As Single, ByVal mileage As Single)
        Me.New(gaslevel, MPG)
        Me.mMileage = mileage
        MessageBox.Show("memory location by New(3 pram): " & Me.ToString(`)
    End Sub
That as the first line CREATES a second object and fills in the existing object - hence two objects
0
CodeCruiserCommented:
ToString() is a method on the object class which gives the string representation of an object (its full type by default).

You can override it to return your own string representation. For example, DateTime structure overrides this method to allow you to get datetime is a specific format by supplying the format string.
0
käµfm³d 👽Commented:
I cannot find any explicit mention of this, but going by the information I found in the language specification regarding constructors (§9.3.1 Instance Constructors) I would say that there are not multiple objects created in memory. The reason I say this is that when you invoke a constructor, whether you explicitly add it or not, at some point a MyBase.New is called. Your memory allocation (I believe) is happening within a base constructor*; everything else is just field initialization. I would expect that under the hood, there is some bit of information carried along to the top-most constructor which alerts it as to how much memory to allocate. Remember that fields/properties, to a computer, are just offsets from some base pointer in memory. As long as the constructor receives back from the base constructor the starting address of the object from within memory, it can set the properties and variables accordingly because it just goes to offset X from that memory address received from the base constructor.

* When I say "memory allocation ... is happening within a base constructor" I am not referring to the immediate base; rather I am referring to (for lack of specific information) Object.New, since everything in .NET derives from Object. Per the spec, every constructor must invoke another constructor, but since Object is the super class of everything in .NET, I take that to be the last stop for constructors (though perhaps there is some underlying native call going on when you get to that level). It is my guess that the constructor for Object is the constructor which actually allocates memory--every other constructor only initializes members.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
wdosanjosCommented:
In C# this is done through Pointer Expressions, which are "unsafe code" and require a special compiler setting.  VB.NET does not support Pointer Expressions.

How to: Obtain the Address of a Variable (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/zcbcf4ta%28v=vs.100%29.aspx

Unsafe Code and Pointers (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx

I hope this helps.
0
Jacques Bourgeois (James Burger)PresidentCommented:
The only .NET language that completely supports unmanaged code (pointers) is C++.

As it has been said, but in other words, VB is managed code only, so it does not have access to pointers, as most modern programming languages, because pointers were one of the word causes of security problems in the past. Also in other words, C# can be compiled as unmanaged code, having access to pointers, but in a limited way, and that limits a lot what you can do in the application. I would be surprised that Java could do it, because I have always known Java as C++ without pointers.

You won't get 2 objects, but only one. That is the main reason why Me.New should be the first line in an alternate constructor. The job of the 2 methods in combined into only one allocation.

You can see that by using a tool such as the CLRProfiler, available somewhere on the Microsoft site.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.