referencing objects

  • Thread starter Thread starter Jack Fox
  • Start date Start date
J

Jack Fox

How do I reference an object within a procedure? I tried to accomplish this
by boxing a "cell" object, but that doesn't work. My cells can each have a
subcell, and I need a way to initialize them down the chain.


object oTempCell = oCurHeaderCell; // I tried "boxing" the cell
MakeHeaderCell(ref oTempCell);

//at this point "oTempCell" has been updated, but not "oCurHeaderCell", so I
made a copy, not a reference

for (int q = 0; q < k; q++)
{
if (((Cell)oTempCell).SubCell == null)
((Cell)oTempCell).SubCell = new Cell();

oTempCell = ((Cell)oTempCell).SubCell;
//I never know how far down the chain I have to go

MakeHeaderCell(ref oTempCell);
}
 
"Boxing" is only done on value types. There is no such thing as boxing a
reference type. struct(ures) are value types. All the built in base
datatypes are also value types (int, double, bool, short, long, etc...).
Although "string" is the exception, it is a special reference type. Any
type defined with the "class" keyword will be a reference type, any defined
with "struct" are a value type. You can check helpfiles to see if a given
..NET type is a struct or class.

By "cell" is that a type in the framework such as a Data grid's cell? or a
cell in MS Excel? Or is that a type you defined. You don't mention the
datatype in your code example. I don't fully understand you object model
relating to cells. Show us a brief hierarchy, or tell us what .NET "cell"
type you are talking about.

What do you want to reference in the procedure? is it a global variable
(static member of a public class)? is it a variable passed into the
procedure?

Is a cell part of a spreadsheet/grid? Do you really need to instantiate
every cell in your "file" / "document"? Can you just create them as they
are needed? These are just things to think about. Without knowing what
your application does and what the object model is, I can't give you any
really constructive advice.
 
"Cell" is my own creation, defined with the "class" keyword, and I was very
surprised that my code is treating instantiations of Cell like a value type
instead of a reference type.

In my previous message I abbreviated the creation of a Cell instance, the
full code to create a Cell looks like this:

Cell oCurHeaderCell = new Cell((int)CellType.Empty, " ", new SortedList(),
new DateTime(1, 12, 31), 0, 0);

Here is the whole definition of the class "Cell":

using System;
using System.Collections;
using MyNamespace;

namespace MyNamespace
{
/// <summary>
/// Summary description for Cell.
/// </summary>
public class Cell
{
private int iCellType;
private DateTime dtDateTime;
private decimal dFloating;
private int iInteger;
private SortedList oFootnotes;
private DateTime dtStartDateTime;
private Cell oSubCell;
private string strText;

public Cell(int CellType, string Text, SortedList Footnotes, DateTime
DateTime, decimal Floating, int Integer)
{
strText = Text;
iCellType = CellType;
dtDateTime = DateTime;
dFloating = Floating;
oFootnotes = Footnotes;
iInteger = Integer;
}
internal int CellType
{
get
{
return iCellType;
}
}
internal DateTime DateTime
{
get
{
return dtDateTime;
}
set
{
dtDateTime = value;
}
}
internal decimal Floating
{
get
{
return dFloating;
}
set
{
dFloating = value;
}
}
internal SortedList Footnotes
{
get
{
return oFootnotes;
}
}
internal int Integer
{
get
{
return iInteger;
}
set
{
iInteger = value;
}
}
internal DateTime StartDateTime
{
get
{
return dtStartDateTime;
}
set
{
dtStartDateTime = value;
}
}
internal Cell SubCell
{
get
{
return oSubCell;
}
set
{
oSubCell = value;
}
}
internal string Text
{
get
{
return strText;
}
set
{
strText = value;
}
}
}
}
 
Is this a code library (DLL) that is meant to be used by another
application? if so, I guess the cell object can be referenced, but not
manipulated by the user since all the properties are internal instead of
public. Is that intentional? Also it seems cell does nothing except hold
information. Where is "MakeHeaderCell()"? If that is exclusively a Cell
function, shouldn't it be a method inside cell (maybe static)?

Is the for loop in your original message the contents of MakeHeaderCell()?

What do you mean when you say, "I never know how far down the chain I have
to go"? Are you not creating these cell objects from reading a file, or by
request of the user? You don't know how far you should be going logically?
or don't know how to implement intialization to the correct level? Have you
tried using a recursive method?

What do you mean when you say "oTempCell was updated, but not oHeaderCell"?
Do you mean visually on the screen (ie. spreadsheet view)? or you see in
the debugger that oHeaderCell is "null"?

It would help if we knew what kind of application this is?

Michael Lang, MCSD
 
Is this a code library (DLL) that is meant to be used by another
application?
The Cell class is internal to a code library. The original code I posted
manipulates it.

Where is "MakeHeaderCell()"?
A void process that updates properties in the Cell instance passed to it.

If that is exclusively a Cell
function, shouldn't it be a method inside cell (maybe static)?
Good suggestion, but for my edification I would like to know how I managed
to make what I thought was a reference object act like a value object.

Is the for loop in your original message the contents of MakeHeaderCell()?
No, it is a separate void process that updates the properties in the Cell
instance passed to it.

What do you mean when you say, "I never know how far down the chain I have
to go"?
Every Cell potentially has a SubCell, even SubCells. The variable "k" in
the loop determines how far I chain down in constructing SubCells. By "I
never know.." I just mean I want simple generic code that can handle any
depth of chaining.

Are you not creating these cell objects from reading a file, or by
request of the user? You don't know how far you should be going logically?
or don't know how to implement intialization to the correct level?
See previous.

Have you
tried using a recursive method?
Possibly a better solution (you suggested a method above), but still
doesn't explain why I am copying and not referencing an instance of a Cell.

What do you mean when you say "oTempCell was updated, but not oHeaderCell"?
THIS IS THE CRUX OF MY QUESTION. Two lines of code actually tell the whole
story:

object oTempCell = oCurHeaderCell;
//"oCurHeaderCell" is an instance of Cell. It is not null.
//Cell oTempCell = oCurHeaderCell; has the same result, I tried "boxing"
it in desperation.

//Next I send oTempCell off to get initialized
MakeHeaderCell(ref oTempCell, oHeaderArray, i, iHeadStart, iHeadEnd, k,
iColumnCount);

Upon returning to the calling procedure (in the debugger), oTempCell has
updated properties, oCurHeaderCell does not. Therefore, oTempCell was
created as a copy of oCurHeaderCell, not as a reference to it.

how I managed to copy when I thought I was referencing.

thanks,
Jack
 
Is this a code library (DLL) that is meant to be used by another
application?
The Cell class is internal to a code library. The original code I posted
manipulates it WITHIN THE DLL.
 
Thanks for your help, Michael!

Your references got me to remember under certain circumstances within the
called procedure I was assinging the parameter Cell to another Cell instead
of copying over all of its properties, so it emerged from the procedure
referencing a different Cell. At the time I wrote it I didn't think through
that the original Cell would not be included in this reference. I simply had
to change the code to copy all of the properties. Mental myopia strikes
again.


Michael Lang said:
Ah. I just noticed you are using the ref keyword on a reference type.
Anytime you pass a reference type variable into a method, the original item
will be modified by that method if that arguement is modified in that
methods code. Use the ref parameter when passing a value type into a method
to have the variable passed in modified and not just copied in.

See help:
http://msdn.microsoft.com/library/d...s/csref/html/vclrfpassingmethodparameters.asp
see: "Passing Reference-Type Parameters"

Does your "MakeHeaderCell(ref Cell cllHeader)" method set the "cllHeader"
variable to a new instance of a cell? See the help...
http://msdn.microsoft.com/library/d...s/csref/html/vclrfpassingmethodparameters.asp
"Example 5: Passing Reference Types by Reference"

Note in this example even if the arguement "cllHeader" is set to a new
instance, the original variable is modified. This is not the case without
the ref keyword. In other words is is redirected to point to the new object
reference. This means it is no longer the same object that "oCurHeaderCell"
points to.

Also see:
http://msdn.microsoft.com/library/d...sref/html/vclrfpassingarraysusingrefoutpg.asp
summary:
"the array can be assigned the null value or can be initialized to a
different array. For example:
public static void MyMethod(ref int[] arr)
{
arr = new int[10]; // arr initialized to a different array
}"

If you did this then the original "oCurHeaderCell" would not be modified.
Actually "oTempCell" would not be "modified" either. It would just be
redirected to point to the newly created Cell object, just as "arr" is in
the example above.

These are my best guesses given the code I have seen so far.

Michael Lang, MCSD

Jack Fox said:
Is this a code library (DLL) that is meant to be used by another
application?
posted
manipulates it.

Where is "MakeHeaderCell()"?
it.

If that is exclusively a Cell
function, shouldn't it be a method inside cell (maybe static)?
managed
to make what I thought was a reference object act like a value object.

Is the for loop in your original message the contents of MakeHeaderCell()?
Cell
instance passed to it.

What do you mean when you say, "I never know how far down the chain I have
to go"?
in
the loop determines how far I chain down in constructing SubCells. By "I
never know.." I just mean I want simple generic code that can handle any
depth of chaining.

Are you not creating these cell objects from reading a file, or by
request of the user? You don't know how far you should be going logically?
or don't know how to implement intialization to the correct level?


Have you
tried using a recursive method?

doesn't explain why I am copying and not referencing an instance of a Cell.

What do you mean when you say "oTempCell was updated, but not oHeaderCell"? whole
story:

object oTempCell = oCurHeaderCell;
//"oCurHeaderCell" is an instance of Cell. It is not null.
//Cell oTempCell = oCurHeaderCell; has the same result, I tried "boxing"
it in desperation.

//Next I send oTempCell off to get initialized
MakeHeaderCell(ref oTempCell, oHeaderArray, i, iHeadStart, iHeadEnd, k,
iColumnCount);

Upon returning to the calling procedure (in the debugger), oTempCell has
updated properties, oCurHeaderCell does not. Therefore, oTempCell was
created as a copy of oCurHeaderCell, not as a reference to it.

know
how I managed to copy when I thought I was referencing.

thanks,
Jack


or
by Have
you instance,
the type.
Any cell? mention
the knowing
what you
any "boxing"
the
 
Ah. I just noticed you are using the ref keyword on a reference type.
Anytime you pass a reference type variable into a method, the original item
will be modified by that method if that arguement is modified in that
methods code. Use the ref parameter when passing a value type into a method
to have the variable passed in modified and not just copied in.

See help:
http://msdn.microsoft.com/library/d...s/csref/html/vclrfpassingmethodparameters.asp
see: "Passing Reference-Type Parameters"

Does your "MakeHeaderCell(ref Cell cllHeader)" method set the "cllHeader"
variable to a new instance of a cell? See the help...
http://msdn.microsoft.com/library/d...s/csref/html/vclrfpassingmethodparameters.asp
"Example 5: Passing Reference Types by Reference"

Note in this example even if the arguement "cllHeader" is set to a new
instance, the original variable is modified. This is not the case without
the ref keyword. In other words is is redirected to point to the new object
reference. This means it is no longer the same object that "oCurHeaderCell"
points to.

Also see:
http://msdn.microsoft.com/library/d...sref/html/vclrfpassingarraysusingrefoutpg.asp
summary:
"the array can be assigned the null value or can be initialized to a
different array. For example:
public static void MyMethod(ref int[] arr)
{
arr = new int[10]; // arr initialized to a different array
}"

If you did this then the original "oCurHeaderCell" would not be modified.
Actually "oTempCell" would not be "modified" either. It would just be
redirected to point to the newly created Cell object, just as "arr" is in
the example above.

These are my best guesses given the code I have seen so far.

Michael Lang, MCSD
 
Back
Top