C
Chris Ashley
I'm getting a little confused at the behaviour of C# with one of my
class constructors. A code example will probably help:
public class RawBitmap
{
internal BITMAPFILEHEADER fileHdr;
internal BITMAPINFOHEADER infoHdr;
private Byte[] bytImgData;
public RawBitmap(BITMAPFILEHEADER theHeader, BITMAPINFOHEADER
theInfo)
{
fileHdr = theHeader;
infoHdr = theInfo;
}
public void SetWidth(int intWidth)
{
infoHdr.biWidth = intWidth;
}
}
public class RawBitmapManager
{
private BITMAPFILEHEADER fileHdr;
private BITMAPINFOHEADER infoHdr;
public RawBitmap[] theBitmaps;
public void SetByteData()
{
// Generate standard length strips
for (int i = 0; i < intTotalStrips; i++)
{
theBitmaps = new RawBitmap(fileHdr, infoHdr);
theBitmaps.SetHeight(256);
theBitmaps.BytImgData = ReadLines(ioBinaryStream,
STRIP_HEIGHT);
}
if (intLastStripHeight > 0)
{
intTotalStrips++;
int intLastBound = theBitmaps.GetUpperBound(0);
theBitmaps[intLastBound] = new RawBitmap(fileHdr, infoHdr);
theBitmaps[intLastBound].SetHeight(11);
theBitmaps[intLastBound].BytImgData =
ReadLines(ioBinaryStream, intLastStripHeight);
}
}
RawBitmapManager holds an Array of RawBitmap objects. When I call the
'SetByteData' method I would expect the array of RawBitmaps to be
populated with copies of infoHdr, but instead it looks like it is being
passed by reference because the biHeight property of every item in the
array is set to 11 at the end of the method. If I comment out the
intLastStripHeight bit, every item int he array is set to 256.
How can I force the constructor parameters to be copied rather than
passed by reference?
Thanks,
Chris
class constructors. A code example will probably help:
public class RawBitmap
{
internal BITMAPFILEHEADER fileHdr;
internal BITMAPINFOHEADER infoHdr;
private Byte[] bytImgData;
public RawBitmap(BITMAPFILEHEADER theHeader, BITMAPINFOHEADER
theInfo)
{
fileHdr = theHeader;
infoHdr = theInfo;
}
public void SetWidth(int intWidth)
{
infoHdr.biWidth = intWidth;
}
}
public class RawBitmapManager
{
private BITMAPFILEHEADER fileHdr;
private BITMAPINFOHEADER infoHdr;
public RawBitmap[] theBitmaps;
public void SetByteData()
{
// Generate standard length strips
for (int i = 0; i < intTotalStrips; i++)
{
theBitmaps = new RawBitmap(fileHdr, infoHdr);
theBitmaps.SetHeight(256);
theBitmaps.BytImgData = ReadLines(ioBinaryStream,
STRIP_HEIGHT);
}
if (intLastStripHeight > 0)
{
intTotalStrips++;
int intLastBound = theBitmaps.GetUpperBound(0);
theBitmaps[intLastBound] = new RawBitmap(fileHdr, infoHdr);
theBitmaps[intLastBound].SetHeight(11);
theBitmaps[intLastBound].BytImgData =
ReadLines(ioBinaryStream, intLastStripHeight);
}
}
RawBitmapManager holds an Array of RawBitmap objects. When I call the
'SetByteData' method I would expect the array of RawBitmaps to be
populated with copies of infoHdr, but instead it looks like it is being
passed by reference because the biHeight property of every item in the
array is set to 11 at the end of the method. If I comment out the
intLastStripHeight bit, every item int he array is set to 256.
How can I force the constructor parameters to be copied rather than
passed by reference?
Thanks,
Chris