Chris
Chris

Reputation: 51

C# refer to parent

sorry for my stupidity but I'm looking for a way to construct an object from a class that connects to a service and also contains another class with methods for the service.

The problem I'm facing is trying to figure out a way to only have to connect to the service once. I refuse to use global variables and the likes.

I'm finding it difficult understanding the object concepts in C# as my programming background mainly comes from JavaScript.

Any help is most appreciated.

namespace Tests.Classes
{
    public class L
    {
        dynamic uri;
        dynamic service;
        dynamic credentials;
        dynamic proxy;

        public L L()
        {
            this.uri = new Uri("bladie bladie blah");
            this.credentials = new ClientCredentials();
            this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
            this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
            this.service = (IOrganizationService)this.proxy;
            return this;
        }

        public class OL
        {
            public OL OL(dynamic a)
            {
                this.service = parent.service; // <- doesn't compile
                return this;
            }
        }
    }
}

To make it clear how it's called:

var l = new L();
l.OL("haha");

Maybe my code isn't clear enough. This will keep the categorization fanatics at bay :P.

namespace Tests.Classes
{
    public class L
    {
        Uri uri;
        IOrganizationService service;
        ClientCredentials credentials;
        OrganizationServiceProxy proxy;

        public L L()
        {
            this.uri = new Uri("hBladie Bladie Blah");
            this.credentials = new ClientCredentials();
            this.credentials.Windows.ClientCredential = (NetworkCredential)CredentialCache.DefaultCredentials;
            this.proxy = new OrganizationServiceProxy(this.uri, null, this.credentials, null);
            this.service = (IOrganizationService)this.proxy;

            return this;
        }

        public class OL
        {
            Entity entity = new Entity();
            IOrganizationService service = null;

            public OL OL(dynamic a)
            {
                if (a is Entity)
                {
                    this.entity = a;
                }
                if (a is string)
                {
                    this.entity = new Entity(a);
                }

                return this;
            }

            public OL attr(dynamic key, dynamic value)
            {
                this.entity[key] = value;
                return this;
            }

            public Boolean save()
            {
                this.parent.service.create(this.entity); // parent does not exist
            }
        }
    }
}

I hate messy programming, I love jQuery style.

Here's how the code has to be used:

new L().OL("haha").attr("Hello", "world").save();

or

var l = new L();
l.OL("HAHA").attr("foo", "bar").save();
l.OL("pff").attr("boppa", "deebop").save();

Upvotes: 0

Views: 233

Answers (3)

Saeb Amini
Saeb Amini

Reputation: 24390

If you want a single service to exist for all instances of your classes, declare it as static. e.g.:

static IOrganizationService service;

Then in your nested class, you can refer to it by L.service.

Upvotes: 0

zmbq
zmbq

Reputation: 39013

That would have worked in Java. In C#, you need to pass L to OL's constructor:

public OL(dynamic O, L parent)
{
   this.service = parent.service;
}

By the way, your constructor won't compile, you have two OLs there, and a return.

By the way 2, why are you using so many dynamics?

Upvotes: 1

Tigran
Tigran

Reputation: 62246

Do somethign like this:

public class OL
{
    dynamic olService = nnull;
    public OL OL(dynamic a)
    {
          this.olService = parent.service; // <- doesn't compile
          //return this; //remove this !!!!
     }
}

EDIT

Just note: try to not use dynamic untill you really need that. Use strong types in C# language as much as it possible.

Upvotes: 0

Related Questions