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 1 1224
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 contains:
<?php
$_SESSION = "";
$_SESSION = "";
|
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:
----------------------------------------------------------------------------
<?php
ini_set("session.use_cookies", "off");
ini_set("session.use_trans_sid", "on");
session_start();
$_SESSION = "";
$_SESSION =...
|
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, 2, func);
|
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
|
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 free()?
#include <stdlib.h>
struct stype
{
int *foo;
};
| |
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 object is a reference type?
my code is not proving that. I have a web project i created from a web
service that is my object:
public class...
|
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 database. It shows up when the form requests the info, just not when I add or edit. I guess the 0 could be null if that helps.
If you need more info,...
|
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;
|
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 Foo foo or readonlyFoo f?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |