C# variable access

liloe

It's my birthday today!
Joined
Jan 25, 2004
Messages
4,168
Okay, I've started programming again and I chose C# but there is one thing I'd like to know and I don't even know if it's possible without using some workarounds with arrays etc.

Basically I want to have a class MyClass with a x different fields and a SetField(var, value) method to set any of those fields.

Code:
class Myclass
{
  private int _x, _y, _z;
  public static int SetField(??? field, int value)
  {
    <--Code here---->
  }
}

Basically I want that people can use SetField(_x, 5) to set _x to 5. Maybe this example is a bit small and sucky, but the question remains: Is that sort of thing even possible?

EDIT: Yes, I know that MyClass._x = 5; would do the trick as well, if I set the fields to public, but I want to know about the other option ;)
 

JingleBells

FH is my second home
Joined
Mar 25, 2004
Messages
2,224
Unless you want to fuck about with reflection and shit you can't do this, you need to either write specific getter/setter methods for each variable, or use C#'s property syntax:
Code:
using System;

public class Customer
{
    private int m_id = -1;

    public int ID
    {
        get
        {
            return m_id;
        }
        set
        {
            m_id = value;
        }
    }

    private string m_name = string.Empty;

    public string Name
    {
        get
        {
            return m_name;
        }
        set
        {
            m_name = value;
        }
    }
}

and then refer to the object as you desribe:
Code:
public class CustomerManagerWithProperties
{
    public static void Main()
    {
        Customer cust = new Customer();

        cust.ID = 1;
        cust.Name = "Amelio Rosales";

	Console.WriteLine(
            "ID: {0}, Name: {1}",
            cust.ID,
            cust.Name);

        Console.ReadKey();
    }
}

Then to make variables read only you remove the set{} bit, and for write-only the get{} bit
 

ST^

Can't get enough of FH
Joined
Dec 22, 2003
Messages
2,351
Okay, I've started programming again and I chose C# but there is one thing I'd like to know and I don't even know if it's possible without using some workarounds with arrays etc.

Basically I want to have a class MyClass with a x different fields and a SetField(var, value) method to set any of those fields.

Code:
class Myclass
{
  private int _x, _y, _z;
  public static int SetField(??? field, int value)
  {
    <--Code here---->
  }
}

Basically I want that people can use SetField(_x, 5) to set _x to 5. Maybe this example is a bit small and sucky, but the question remains: Is that sort of thing even possible?

EDIT: Yes, I know that MyClass._x = 5; would do the trick as well, if I set the fields to public, but I want to know about the other option ;)

You can use get/set if you need to do some processing to the values first (though variables must be public), or make a separate function for each variable, or do if(field == '_x') { this._x = value; }

Or you can use an array or ArrayList instead of separate variables.

Why do you need to keep them private and yet need to change the values from outside?
 

liloe

It's my birthday today!
Joined
Jan 25, 2004
Messages
4,168
I knew about the get/set stuff, but I asked because of two reasons. First to know if it works at all and to see if it's possible to write "filler" methods which work for any input without having to use workarounds with arrays etc.

I will google that reflection stuff, though ;)

P.S.: The fields need to be private so that they can only be accessed by methods I allow ;)
 

Users who are viewing this thread

Top Bottom