tags:

views:

92

answers:

6

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 'ThePoint.X' work (without just publicly displaying 'Poi')?

+1  A: 

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

Michael Hedgpeth
Uh, no. The whole point of properties is to keep the syntax of public fields while offering a way to keep those fields private.
Steven Sudit
Steven, I understand that, but I'm answering his question on how to get it to work, not how to make it the best from a design perspective.
Michael Hedgpeth
If you're recommending something that you know is a bad idea, please say so.
Steven Sudit
OK, I edited it; thanks.
Michael Hedgpeth
+2  A: 

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; }
}
Justin Niessner
A: 

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;}
}
code4life
This won't compile, although to be fair, that's because you copied the errors from the original.
Steven Sudit
@Steven, thanks, I made the corrections. All I wanted to do was illustrate the auto-implemented properties, got careless...!
code4life
It still doesn't work at all. Again, this is mostly due to the source.
Steven Sudit
+1  A: 

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.

Dexter
+2  A: 

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.

Steven Sudit
A: 

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.

Jesse C. Slicer