Reference Accessor

    Date: 10/10/08 (C Sharp)    Keywords: no keywords

    Hello guys. Do any of you know how get a property accessor return a reference. I'm sure it can be done, because the framework does it.

    Here's an example :

    public class Upper {
    	public struct SInner {
    		private string m_str;
    		public string str {
    			get { return m_str; }
    			set { m_str = value; }
    		}
    	}
    
    	private SInner m_Inner = new Inner();
    	public SInner Inner {
    		get { return m_Inner; }
    	}
    
    	public void func() {
    		Inner.str = "value";
    	}
    }
    


    The assignment in func() causes a compile error. This is because i need a reference to m_Inner to be returned, not a copy of the value that simply resets the previous value.

    And, yes, there is a reason for it. I know the example seems unnecessarily complex, but it's being dumbed down.

    Thanks for any help.

    EDIT:

    Nevermind, I figured it out. Since Inner was a struct, it was a ValueType. Thus, Inner.str was a value (sort of.) I changed Inner to be a class, and that allowed forced it to be returned by reference.

    If that's confusing, just comment. I'll explain it better.

    Source: http://community.livejournal.com/csharp/98794.html

« Client-side validation || Vista/2k3 C# Packet Capturing »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home