473,473 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing constructor parameter by value?

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[i] = new RawBitmap(fileHdr, infoHdr);
theBitmaps[i].SetHeight(256);
theBitmaps[i].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

Sep 8 '06 #1
5 2398
Well, what are BITMAPFILEHEADER and BITMAPINFOHEADER? Classes? Structs? If
they are classes, then the reference will be passed by value, so yes: each
RawBitmap will be looking at the same instance of fileHdr etc.

If appropriate (and it almost always isn't) you could make these structs. I
doubt this would be a good idea. A better option may be to simply provide a
Clone() method on these objects, and explicitely clone them before passing
them to the ctor.

Marc
Sep 8 '06 #2
Chris Ashley wrote:
I'm getting a little confused at the behaviour of C# with one of my
class constructors.
<snip>
How can I force the constructor parameters to be copied rather than
passed by reference?
The parameter *is* being passed by value - but arrays are reference
types, so the parameters are references themselves. You'll need to
create a copy of the array in the constructor.

See http://www.pobox.com/~skeet/csharp/parameters.html for details of
parameter passing. (There's nothing special about constructors in this
regard.)

Jon

Sep 8 '06 #3
Well, what are BITMAPFILEHEADER and BITMAPINFOHEADER? Classes? Structs? If
they are classes, then the reference will be passed by value, so yes: each
RawBitmap will be looking at the same instance of fileHdr etc.

If appropriate (and it almost always isn't) you could make these structs. I
doubt this would be a good idea. A better option may be to simply provide a
Clone() method on these objects, and explicitely clone them before passing
them to the ctor.

Marc
Or clone then *inside* the constructor

Hans Kesting
Sep 8 '06 #4
Can you use the ref keyword to pass by reference?
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
>Well, what are BITMAPFILEHEADER and BITMAPINFOHEADER? Classes? Structs?
If they are classes, then the reference will be passed by value, so yes:
each RawBitmap will be looking at the same instance of fileHdr etc.

If appropriate (and it almost always isn't) you could make these structs.
I doubt this would be a good idea. A better option may be to simply
provide a Clone() method on these objects, and explicitely clone them
before passing them to the ctor.

Marc

Or clone then *inside* the constructor

Hans Kesting


Sep 8 '06 #5
Can you use the ref keyword to pass by reference?
>
I don't know if that is possible for a constructor, but it would just
pass the reference to the object by reference. It would NOT "clone" the
object or otherwise make an independant copy.

Hans Kesting
"Hans Kesting" <ne***********@spamgourmet.comwrote in message
news:mn***********************@spamgourmet.com...
>>Well, what are BITMAPFILEHEADER and BITMAPINFOHEADER? Classes? Structs? If
they are classes, then the reference will be passed by value, so yes: each
RawBitmap will be looking at the same instance of fileHdr etc.

If appropriate (and it almost always isn't) you could make these structs.
I doubt this would be a good idea. A better option may be to simply
provide a Clone() method on these objects, and explicitely clone them
before passing them to the ctor.

Marc

Or clone then *inside* the constructor

Hans Kesting


Sep 8 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
4
by: Sahil Malik [MVP] | last post by:
Okay so now I understand (surprised though) - that WebServices can indeed pass ByRef/ref parameters. All I have to do is mark an integer parameter of a WebMethod as "ref". Funnily enough, this is...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
3
by: Ross McLean | last post by:
Hi all, I've been teaching myself C# for a new project at work. I have a bit of a background in c++ and java but never been what you could call a guru. I'm having some strange things happening...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
2
by: PSN | last post by:
Hi all .. can any one please explain the output of the following code .. class A { public: int a1; int a2; A(int a1, int a2) { cout << "i am in AAA" << endl;
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.