开发者

accessors problem

I was wondering if it was impossible to set up an accessor to allow you to access the accessor's variable.. Example of an error:

    public void Main()
    {
        Object.name = "test"; //Can't access the object's subproperties
    }

    Objec ob = new Objec();
    public Objec Object
    {
        get { return ob; }
        set { ob = value; }
    }
class Objec
{
    string name;
    string value;
}

Is there anyway to do the above (other than making accessors for every value)?

Thanks,

Max

EDIT: Here is a better example

    public void Main()
    {
        //Now I can't change the X or Y properties, this will display an error
        ThePoint.X = 10;
        //To change the x value, I need to do the following:
        ThePoint = new Point(10,0);
    }

    private Point Poi = new Point();
    public Point ThePoint
    {
        get { return Poi; }
        set { Poi = value; }
    }

Is there a way to make 'Th开发者_开发百科ePoint.X' work (without just publicly displaying 'Poi')?


I'm guessing you didn't post full code. I would imagine right now you can't access Objec.name because it is a private variable (and thus inaccessible from Main()).

Try:

class Program
{
    public static void Main(string args[])
    {
        MyClass instance = new MyClass();
        instance.Child.ChildValue = "something";
    }
}

public class MyClass
{
    // The following code declares Public Properties 
    // rather than private variables.

    public string Value { get; set; }
    public string Name { get; set; }
    public MyChild Child { get; set; }

    public MyClass()
    {
        this.Child = new MyChild();
    }
}

public class MyChild
{
    public string ChildValue { get; set; }
}


Ok, with your new example, it's clear what the problem is: you're acting as if Point were a class when it's actually a struct.

When you say ThePoint.X = 10;, all you're doing is changing the X for a copy of the original point. That's because a struct is always passed by value, not reference.

In short, the error is in your expectations.


You have to specify the public for every class member you want to expose in C#.

According to the C# specification:

Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private. The access level for class members and struct members, including nested classes and structs, is private by default.


You should make the name and value variables public to get your solution to compile. Or better yet, use automatic properties.


The answer to your actual question "Is there anyway to do the above (other than making accessors for every value)?" is NO.

Guessing a bit on your intentions, you seem to want to follow a design pattern known as Bridge (your class is an abstraction around the implementation of Object and/or Point). You will only expose to your class' audience the pieces of the original implementation that you want, in your question's case, every value.


You need to make the following changes:

public void Main()
{
    Object.name = "test"; //Can access the object's properties now...
}

Objec ob = new Objec();

public Objec Object
{
    get { return ob; }
    set { ob = value; }
}

public class Objec
{
    public string name {get; set;}
    public string value {get; set;}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜