473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

passing by ref for value type and storing this in a member...

Hi,

I would like to hear peoples thoughts on passing a value type by reference
and then trying to store this ref in a member so it can be updated
elsewhere, whereby I want the referenced value to be updated.

eg.

class A
{
bool refval;

A(ref bool val)
{
refval = val;
}

B()
{
refval = true;
}
}

main()
{
bool passbyref = false;

A a = new A(ref passbyref);

// value of passbyref this point is still false.

a.B();

// I want value of passbyref at this point to be true, however it is
still false as class A simply took a copy of ref.
}

In a nutshell I want the 'passbyref' reference to be updated in A.B()
method.

I'd like to do this without resorting to other methods such as passing a
struct (non value type), object etc.

Or is this prevented because it might be considered as bad design?

Any help would be appreciated.
Thanks,

Dave
Feb 22 '06 #1
1 1208
Essentially what you want is a pointer to the value type, but in the
case of a value type I'm not sure what that means. I'll explain. Before
I do, though, have you noticed that your code doesn't do what you'd
like it to?

class A
{
bool refval;

A(ref bool val)
{
refval = val;
}

B()
{
refval = true;
}
}

In this class, the line

reval = val;

just copies the value of the bool from "val" into "refval". It doesn't
_store a pointer_ to the value that was passed into A(). So, when you
call B(), all it does is update the "refval" bool to contain something
different. This is _not_ the same as the old C way of doing it:

int *refvalp;

void A(int *valp)
{
refvalp = valp;
}

void B(void)
{
*refvalp = true;
}

because in the C case you're holding a _pointer_ to an int, whereas in
the C# case you're holding a bool, not a pointer (or a reference), and
there's no way to declare a class member like this:

private bool ref refval;

....in other words, in C# there's no way to declare a pointer.

Think of what would happen if you could. Let's say that your class does
what you would like: it stores a "pointer" to an int and then allows
you to update it later. What if your class instance outlives the int?
Consider this:

public void CrashAndBurn()
{
A = GetA();
UseA(A);
}

public A GetA()
{
bool x;
return new A(ref x);
}

public void UseA(A anA)
{
int i;
anA.B();
}

The line anA.B() would be following the "pointer" it holds and updating
the bool "x", but "x" is a local variable of the method GetA(), which
has finished and released its stack space... stack space that is now
being used by UseA to hold its int, i. So, calling anA.B() would slam a
bool onto the stack where "x" _was_, and in so doing corrupt "i".
Instant stack corruption. Easy end run around all of the security that
..NET offers.

That's why C# doesn't allow pointers in safe code.

Now, that said, this doesn't mean that there aren't ways to hold
references to particular properties of particular instances allocated
on the heap. You can do that using Reflection. You cannot, however,
hold a reference to an arbitrary value type and then update it whenever
you like.

Feb 23 '06 #2

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

Similar topics

5
by: Paul | last post by:
I want to use sessions to cover myself in case the user switches off cookies so I am passing the session ID manually through a hidden input field. This is what I have so far. index.php page...
1
by: Paul | last post by:
Hmmm, didn't seem to work. I have set session.use_cookies = 1 and session.use_trans_sid = 1 in my php.ini file. Index.php contains:...
5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
16
by: Simon | last post by:
Hi all, I think I've seen someone passing an emumeration in code before. Can anyone tell me if thats possible and why i would want to. Many thanks Kindest Regards
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
8
by: nick02895 | last post by:
I am not a programmer, just putting bits and pieces I've found on the net. I am missing something here, can you help? The "publishcityonline" checkbox is not sending the required 0 or 1 to mysql...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
by: puzzlecracker | last post by:
How can I pass a reference to a method as constant? I tried the following: Function(const Foo f) or Function(readonly Foo f) Also, How to declare local variable to be constant const...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.