473,386 Members | 1,819 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.

Storing a reference to a variable within a class

Hi all,

I am trying to store a reference to a variable in my data
providerr class
but cannot save a reference to it. Can anyone help me?

I would like this code to allow me to store any field by
referenece so that
it's contents can be written to using the Value property.

Code sample:
============
public class MyField
{
private string strFieldName;
private object objValue;

public string FieldName {set{strFieldName=value;}get
{return(strFieldName);}}
public object Value {set{objValue=value;}get{return
(objValue);}}

public SqlField(string pFieldName,
ref object pObject) /* THIS LINE DOES NOT WORK AT
POINT REF */
{
strFieldName=pFieldName;
objValue=pObject;
}

Then in the program it is called using:
=======================================
int y=0;
MyField m = new MyField("id", ref y);
y=26;

Results wanted:
===============
m.Value is 0,
m.Value is 26 when y is set to 26

Please note that I don't always want to store an integer,
it maybe a string
or other object.

Thanks in advance
Chris

Jul 19 '05 #1
1 5954
Ignoring the fact that your constructor name doesn't match the classname (I
take it you changed it before pasting it into your post), you misunderstand
the nature of value/reference types and the operating of .net
boxing/unboxing. Some beginners' info at
http://www.codeproject.com/csharp/boxing.asp and the msdn meat and two veg
is at
http://msdn.microsoft.com/library/de...unboxingpg.asp .

Your code doesn't work for the same reason as...

int x = 5;
object y = x;
x = 6;

....not working as you'd hope. The variable 'y' will remain at 5. The reasons
for this is that 'x' is boxed (copied into an object, valuetype to reference
type); when 'x' is changed to 6, 'y' remains unchanged because they are not
related (and never were). If you really want to get down and dirty and use
proper pointers (why you'd want to do this is beyond me, the point of c# and
managed code is to escape that nonsense) by "pinning" variables in memory to
prevent the GC binning them while you manipulate them with pointers a la
c/c++ (see c# keywords "fixed", "unsafe" and "stackalloc" and the "*" and
"&" operators).

My advice to you is to read about boxing/unboxing/value & references types,
grab a coffee (or something stronger) and seriously rethink your program and
object model. If you need to have a single instance of a parameter being
referenced throughout your code, make a lightweight class for it, (not a
struct as they are valuetypes and will be subject to the same behaviour as
ints, bytes, strings etc). Objects, objects & objects.

Hope this helps,

- Oisin

"Christopher" <no******@thinkware.co.uk> wrote in message
news:09****************************@phx.gbl...
Hi all,

I am trying to store a reference to a variable in my data
providerr class
but cannot save a reference to it. Can anyone help me?

I would like this code to allow me to store any field by
referenece so that
it's contents can be written to using the Value property.

Code sample:
============
public class MyField
{
private string strFieldName;
private object objValue;

public string FieldName {set{strFieldName=value;}get
{return(strFieldName);}}
public object Value {set{objValue=value;}get{return
(objValue);}}

public SqlField(string pFieldName,
ref object pObject) /* THIS LINE DOES NOT WORK AT
POINT REF */
{
strFieldName=pFieldName;
objValue=pObject;
}

Then in the program it is called using:
=======================================
int y=0;
MyField m = new MyField("id", ref y);
y=26;

Results wanted:
===============
m.Value is 0,
m.Value is 26 when y is set to 26

Please note that I don't always want to store an integer,
it maybe a string
or other object.

Thanks in advance
Chris

Jul 19 '05 #2

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

Similar topics

11
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
1
by: Christopher | last post by:
Hi all, I am trying to store a reference to a variable in my data providerr class but cannot save a reference to it. Can anyone help me? I would like this code to allow me to store any field...
5
by: Javier Campos | last post by:
WARNING: This is an HTML post, for the sake of readability, if your client can see HTML posts, do it, it doesn't contain any script or virus :-) I can reformat a non-HTML post if you want me to (and...
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
2
by: newjazzharmony | last post by:
All, I'd appreciate it if someone could answer these questions for me. This scenario applies to an ASP dot net application that is deployed in a web farm. 1) Does the application live in a...
5
by: Bryan | last post by:
I have a class 'TagType' with an ilist member 'Props' that holds a collection of another class called 'Prop'. I let the user create TagTypes and save multiple properties (Props) in them. I am...
7
by: andy | last post by:
A question about about passing a class by reference: Say you have a class called car, and within that you have two objects called car01 and car02. Within the class I have an int variable...
3
by: Seth Gecko | last post by:
Hi I am working with generic lists of various objects and a control dealing with these lists. For instance: A parent form holds: dim Walls as List(Of wall) dim Segments as List(Of segment) ...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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.