Object parameters

  • Thread starter Thread starter Michael Gorbach
  • Start date Start date
M

Michael Gorbach

Hi,
I'm writing a simulation program and i have main form which controls
the simulation ... it has a private field which is the simulation
object (type simulation). I am using two subforms for different views
of the simulation, each of which is inside the main form. The
simulation is passed into these subforms view constructor from the main
form like so:
Tableform tableform = new Tableform(simulation).

What is being passed here?
It is not set to be a reference parameter, but it feels like an object
reference is being passed because which the mainform executes
simulation.run(100), the simulations in the two subforms run and fire
their events.
 
Michael,

It's hard to tell what is being passed there without seeing the
constructor for the Tableform class. My guess though is that you are
passing a reference to an object into the constructor. The parameter itself
is not a reference parameter (meaning the parameter is passed by value), but
the type that the parameter is a reference type, which means that you are
passing the reference by-value.

Hope this helps.
 
If simulation is a class, it'a reference type and it's reference is passed
to your subform. If it's declared as a struct, it's a value type and the
behaviour is different since the object is instanciated on the stack. The
behaviour you describe let me think you declared it as a reference type.
Thus, you only have one instance of your simulation but several reference to
it...
 
the simulation is a class
the constructor of my subform looks like so:
internal ViewForm(Simulation simulation)

I'm kinda confused ... aren't parameters supposed to be passed by value
by the default? so why is the object reference being passed?
 
Michael,

There is a big difference between a reference to an object, and passing
parameters by reference. When you are accessing reference types, you hold a
reference to the instance, you don't hold the actual instance yourself.
That's why when you do something like this:

// Create an instance of a class.
object o1 = new object();
object o2 = o1;

o1 and o2 actually point to the same object. Now, if you pass o1 to a
method normally, the ^reference^ that is o1 can not be changed, but when you
access o1, you are accessing the instance that the reference points to. So
if you do this:

public void DoSomething(object o)
{
// Change o to something else.
o = new object();
}

And call it like this:

DoSomething(o1);

The reference to o1 is passed by value, and can not be changed. Because
of this, when you exit DoSomething, o1 still points to the original value
that it had before the call to DoSomething. It does not point to a new
object. Now if the signature was this:

public void DoSomething(ref object o)

Then when you called DoSomething, o1 would point to a new object.
 
Nicholas (and others), thank you for the great explanations ... and the
super fast speed! I come from c++ and didnt realize, as andry
explained, that all objects created with new in csharp are pointers
essentially.

thanks again for the help!
 
Michael Gorbach said:
What is being passed here?
It is not set to be a reference parameter, but it feels like an object
reference is being passed because which the mainform executes
simulation.run(100), the simulations in the two subforms run and fire
their events.

A reference is being passed by value. You need to distinguish between
"reference type"/"value type" and "by reference"/"by value".

See http://www.pobox.com/~skeet/csharp/parameters.html for more
information.
 
Back
Top