X
X
Xveeder2020-09-22 17:39:41
C++ / C#
Xveeder, 2020-09-22 17:39:41

How to access the properties of an object through a field class?

Good day, gentlemen.
Recently, I began to study C # tightly and the following question arose.

Let's say we have some class structure. The main class (more precisely, its object) contains data and resources that are used by other classes.

For example:

class BaseClient
    {
        static readonly HttpClient client = new HttpClient();
        public string token { get; private set; }

        public BaseClient(string token)
        {
            this.token = token;
        }

        public SecondClient SecondClient;
    }


Methods of the SecondClient class need to have access to the fields and properties of an object of the BaseClient class. How to make it so that methods from the SecondClient class can access the data of an object of the BaseClient class. It should look like this:

BaseClient BaseClient = new BaseClient(Token);
BaseClient.SecondClient.SomeMethod("89544511588");


And SomeMethod must get the Token from the BaseClient object.
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Q
Qualiant, 2020-09-22
@Qualiant

Either the BaseClient fields must be static, or the SecondClient needs to somehow know about the existence of a particular BaseClient instance. For example, by passing a reference to it through the constructor.
And also naming instances in the same way as a class is a bad form.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question