nighttrain
nighttrain

Reputation: 1

Access to protected member and properties case

I have one base class called Cocpit for Server and Protocl derived classes then I have Heartbeatsupervisorclass which would take Server and protocol and do some work anyway I got some problem with access to protected member inside Heartbeat - I know because I am trying to access through Server object and not over heartbeat but how to solve that issue according to my whole code?

CHANGED

public static bool IsConnectedToInternet()
        {

         Ping pingSender = new Ping();
        PingOptions options = new PingOptions();
        options.DontFragment = true;
        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        try
        {
          PingReply reply = pingSender.Send(HOST, timeout, buffer, options);
          if (reply.Status == IPStatus.Success)
          {            
            //Ping was successful
            return true
          }
          else 

Upvotes: 0

Views: 81

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503469

and after all when want to use inside Heartbeate I cant access protocol.Name inside constructor - how to access that?

You can't, while it's protected. protected is designed to give access to protected members of objects of type Foo (and derived classes) within Foo.

From section 3.5.3 of the C# 5 specification:

When a protected instance member is accessed outside the program text of the class in which it is declared, and when a protected internal instance member is accessed outside the program text of the program in which it is declared, the access must take place within a class declaration that derives from the class in which it is declared. Furthermore, the access is required to take place through an instance of that derived class type or a class type constructed from it. This restriction prevents one derived class from accessing protected members of other derived classes, even when the members are inherited from the same base class.

It sounds like you should probably just make Name a public property. It's not clear why these are all virtual, mind you... and some could be more concisely implemented as automatically implemented properties, too.

Upvotes: 3

Related Questions