Saving a variable as a reference in an object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Saving a variable as a reference in an object

  1. #1
    Christopher Guest

    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

  2. #2
    Boris Karadjov Guest

    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center