Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tied/linked objects/classes in C# like LINQ

Maybe it's a newbie question, but could anyone explain me how the tied/linked classes (I don't know their true names) are made? The example can be LINQ TO XML.

When I have the beneath code:

XDocument doc = XDocument.Load("...");
XElement element = doc.Element("root");
element.SetAttribute("NewAttribute", "BlahBlah");
doc.Save("...");

I change only element variable (I don't need to update it in doc because its referenced). How to create such classes?

[edit]
I tried @animaonline's code and it works

A a = new A();
B b = a.B(0);
b.Name = "asd";
Console.WriteLine(a.Bs[0].Name); // output "asd"

But tell what's the difference with the code above and below?

List<string> list = new List<string>();
list.Add("test1");
list.Add("test2");
var test = list.FirstOrDefault();
test = "asdasda";
Console.WriteLine(list[0]); // output "test1" - why not "asdasda" if the above example works???
like image 911
Nickon Avatar asked Aug 01 '13 11:08

Nickon


2 Answers

doc.Element is a method, it returns a reference to the first (in document order) child element with the specified XName.

Consider this example:

public class A
{
    public A()
    {
        this.Bs = new List<B>();

        this.Bs.Add(new B { Name = "a", Value = "aaa" });
        this.Bs.Add(new B { Name = "b", Value = "bbb" });
        this.Bs.Add(new B { Name = "c", Value = "ccc" });
    }

    public List<B> Bs { get; set; }

    public B B(int index)
    {
        if (this.Bs != null && this.Bs[index] != null)
            return this.Bs[index];

        return null;
    }
}

public class B
{
    public string Name { get; set; }
    public string Value { get; set; }
}

Usage:

A a = new A();
var refToA = a.B(0);
refToA.Value = "Some New Value";

foreach (var bs in a.Bs)
    System.Console.WriteLine(bs.Value);

Explanation:

As you can see the B() method returns a reference to a list item in the A class, updating it will change the value in the A.bs list as well, because it's the very same object.

like image 167
animaonline Avatar answered Nov 17 '22 22:11

animaonline


I think I understand what you're asking. The validity of my response is based entirely on this premise. :D


Class members do not have to be primitive types - they can be other classes too.

Simple example

Given two classes SimpleXDocument and SimpleXElement, note that SimpleXDocument has a member / exposed property of type SimpleXElement:

public Class SimpleXDocument
{
    private SimpleXElement _element;
    public SimpleXDocument(){ this._element = null;}
    public SimpleXDocument(SimpleXElement element){this._element = element;}
    public SimpleXElement Element
    {
        get{return this._element;}
        set{this._element = value;}
    }
}

Public Class SimpleXElement
{
    private String _value;
    public SimpleXElement(){this._value = String.Empty;}
    public SimpleXElement(String value){this._value = value;}
    public String Value
    {
        get{return this._value;}
        set{this._value = value;}
    }
}

SimpleXDocument references its own member of type SimpleXElement. You can do things like:

SimpleXDocument doc = new SimpleXDocument();
doc.Element = new SimpleXElement("Test 1");
SimpleXElement element = new SimpleXElement();
element.Value = "Test 2";
doc = New SimpleXDocument(element);
SimpleXElement anotherElement = new SimpleXElement("another test");
doc.Element = anotherElement;
doc.Element.Value = "yet another test";
anotherElement.Value = "final test"; //N.B. this will update doc.Element.Value too!
like image 36
Tom Tom Avatar answered Nov 17 '22 22:11

Tom Tom