-
Saving a variable as a reference in an object
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
-
Re: Saving a variable as a reference in an object
In C# there is no support for references to value types other than in constructor or method parameters. This means you cannot define
a variable capable of holding a reference to other variable. Store a reference to a delegate or interface that has a method that
modifies the variable instead.
--
Boris Karadjov
Brainbench MVP for Visual C++
http://www.brainbench.com
"Christopher" <devx@thinkware.co.uk> wrote in message news:3f095c34$1@tnews.web.devx.com...
>
> 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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks