locked
Interfaces and Inheritance RRS feed

  • Question

  • I have some PHP classes that I am porting to C#.  I almost got it except for the final two classes.  I have be comment with "// ?? HOW TO DO.." and the compiler ERROR .   How to do get two interfaces and two instances of the interfaces and a two classes that inherent a "handling" classes.    How it makes sense of this.  Thanks

    public class CDoor
    {
            interface iWildcatConsoleIO
            {
                string GetDeviceType();
                bool Initialize();
                bool ShutDown();
            }
    
            class CWildcatDeviceDoor32 : iWildcatConsoleIO
            {
                public string GetDeviceType() { return "WCDOOR32"; }
                public bool Initialize() { return wcDoor32API.DoorInitialize(); }
                public bool ShutDown() { return wcDoor32API.DoorShutdown(); }
            }
    
            class CWildcatDeviceCRT32 : iWildcatConsoleIO
            {
                public string GetDeviceType() { return "WCCRT32"; }
                public bool Initialize() { return true; }
                public bool ShutDown() { return true; }
            }
    
            // ?? I don't want this class to be public
            public class CWildcatConsole : wcServerAPI
            {
                private bool Active = false;
                private iWildcatConsoleIO _interface = null;
                private string interfaceType = "";
    
                CWildcatConsole(iWildcatConsoleIO _interface = null)
                {
                    if (this._interface == null)
                    {
                        if (Environment.GetEnvironmentVariable("wcdoorpipename") != "")
                        {
                            _interface = new CWildcatDeviceDoor32();
                            this.interfaceType = "WCDOOR32";
                        }
                        else
                        {
                            _interface = new CWildcatDeviceCRT32();
                            this.interfaceType = "WCCRT32";
                        }
                    }
                    this._interface = _interface;
    
                    if (this._interface.Initialize())
                    {
                        this.Active = true;
                        return; // true;
                    }
    
                    return; // false;
                }
    
                ~CWildcatConsole()
                {
                    if (this.Active)
                    {
                        this._interface.ShutDown();
                    }
                }
                //---------------
                // Public
                //---------------
                public string GetDeviceType()
                {
                    return this._interface.GetDeviceType();
                }
    
            } // end of CWildcatConsole
    
            //------------------------------------------------------------
            // Use this class for pure DOO32 operations
            //------------------------------------------------------------
    
            public class CWildcatDoor : CWildcatConsole
            {
                // ?? HOW TO DO THIS
                CWildcatDoor()
                // Error CS0122 'CDoor.CWildcatConsole.CWildcatConsole(CDoor.iWildcatConsoleIO)'
                // is inaccessible due to its protection level
                {
                    if (Environment.GetEnvironmentVariable("wcdoorpipename") == "")
                    {
                        Console.WriteLine("This class is used for WCDOOR32 mode.");
                        Environment.Exit(1);  // die;
                        return;
                    }
                    // ?? HOW TO DO THI IN C#, __construct is PHP class constructor
                    return parent::__construct(new CWildcatDeviceDoor32());
                    // Error CS0432 Alias 'parent' not found
    
                }
            }
    
            //------------------------------------------------------------
            // Use this class for pure CRT32 local mode operations
            //------------------------------------------------------------
    
            public class CWildcatLocal : CWildcatConsole
            {
                // ?? HOW TO DO THIS
                CWildcatLocal()
                // Error CS0122 'CDoor.CWildcatConsole.CWildcatConsole(CDoor.iWildcatConsoleIO)'
                // is inaccessible due to its protection level
                {
                    if (Environment.GetEnvironmentVariable("wcdoorpipename") != "")
                    {
                        Console.WriteLine("This class is used for WCCRT32 mode.");
                        Environment.Exit(1);  // die;
                    }
                    // ?? HOW TO DO THIS IN C#, __construct is PHP class constructor
                    return parent::__construct(new CWildcatDeviceCRT32());
                    // Error CS0432 Alias 'parent' not found
                }
            }
    
    }
    


    Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com

    Monday, April 20, 2020 6:56 AM

Answers

  •            

                // ?? HOW TO DO THIS
               
    CWildcatLocal()
               
    // Error CS0122 'CDoor.CWildcatConsole.CWildcatConsole(CDoor.iWildcatConsoleIO)'
               
    // is inaccessible due to its protection level

    For this one, just add the keyword "public" to all the constructors in all your classes to change their accessibility:

                public CWildcatLocal()


                   
    // ?? HOW TO DO THIS IN C#, __construct is PHP class constructor
                   
    return parent::__construct(new CWildcatDeviceCRT32());
                   
    // Error CS0432 Alias 'parent' not found
     

    To call the constructor in the parent class from the constructor of the child class, you add :base(args) in the declaration of the child class constructor:

                public CWildcatLocal() : base()


    Monday, April 20, 2020 11:48 AM
  • If you want to debug a program that is not started from Visual Studio, but from a different program, then you have to wait until the other program has started the one that you want to debug (insert a pause in the program if necessary to give you enough time), and then you use from Visual Studio the option Debug -> Attach... and select the running executable. This will enable the breakpoints.
    Friday, May 8, 2020 12:54 PM

All replies

  •            

                // ?? HOW TO DO THIS
               
    CWildcatLocal()
               
    // Error CS0122 'CDoor.CWildcatConsole.CWildcatConsole(CDoor.iWildcatConsoleIO)'
               
    // is inaccessible due to its protection level

    For this one, just add the keyword "public" to all the constructors in all your classes to change their accessibility:

                public CWildcatLocal()


                   
    // ?? HOW TO DO THIS IN C#, __construct is PHP class constructor
                   
    return parent::__construct(new CWildcatDeviceCRT32());
                   
    // Error CS0432 Alias 'parent' not found
     

    To call the constructor in the parent class from the constructor of the child class, you add :base(args) in the declaration of the child class constructor:

                public CWildcatLocal() : base()


    Monday, April 20, 2020 11:48 AM
  • Thank you.  Your input lead me to solving this, However I had one question.

    To get it compile, I had to make the following public as well

    public interface iWildcatConsoleIO {...}

    public class CWildcatConsole : wcServerAPI {...}

    When creating an instance in the main program, you can see  iWildcatConsoleIO and CWildcatConsole expose.  See image snippet. 

    

    I would like to not expose CWildcatConsole and iWildcatConsoleIO interface. Without the public for the class CWildcatConsole() then its constructor give me an inaccessible error.

    thanks


    Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com



    Monday, April 20, 2020 3:35 PM
  • You will need to evaluate carefully which things you want to make public and which not, and make sure that they "nest" properly. For example, you cannot make public a method that needs to be passed an argument which is of a type that is not public; it would not be possible to call such a method. Similarly, you cannot make public a class that inherits from another class (or an interface) which is not public, because the callers of the class need access to its parent to be able to resolve its usage.

    I am not aware of any trick that will resolve the whole of your hierarchy in one go. The only solution is to consider one by one all the object relationships and then change the accessibility of each type in a way that is consistent with the accessibility of other types that depend on it. The compiler will help you by pointing out the inconsistencies. For example, if you change to private something that was public, the compiler will show an error on any types that become inconsistent because they are more visible than the one they depend on and you just reduced in visibility. You can solve the issue by either reducing the visibility of the dependent type or increasing the visibility of the type it depends on.

    Monday, April 20, 2020 3:55 PM
  • Thank you. I got it working for now and I am now completing my wcSDK classes for .net with all its methods. I only showed 3 methods in the interface here.   I used the interface approach this time around, first time I ever did that first with PHP and now the port to C#.  But I am pretty sure it can be done virtual functions as I have done it with C/C++ (not .Net). 

    I will explore it again when I am done.  I will mark your first comment as the answer. 

    Thank you again. 

    Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com

    Monday, April 20, 2020 5:54 PM
  • Alberto, I have a question in debugging this C# program.  I can't seem to get a breakpoint set. It goes into a disabled state when I start to run it.  Ok this is how it starts:

    The C# program is called xyz-c#.exe 

    And I set the breakpoint to the beginning of the xyz-c#.exe so I beginning tracing it.  But the C# program is not designed to be run byself. It needs to be loaded by a parent process.

    In the debug properties, I have:

    (o) Start External Program:  c:\wc8beta32\wcLocal.exe

    When I hit F5, it starts wcLocal.exe which will run the xyz-c#.exe program.  At this point the breakpoint is disabled.

    I tried attaching wcLocal.exe and before starting xyz-c#.exe.  That didn't work   

    How can I enable the breakpoint?


    Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com


    Friday, May 8, 2020 12:08 PM
  • If you want to debug a program that is not started from Visual Studio, but from a different program, then you have to wait until the other program has started the one that you want to debug (insert a pause in the program if necessary to give you enough time), and then you use from Visual Studio the option Debug -> Attach... and select the running executable. This will enable the breakpoints.
    Friday, May 8, 2020 12:54 PM
  • Thanks,  I put a readkey pause in the xyx-c#.exe main block, F5 started wcLocal.exe, it started xyz-c#, at the pause I attached xyz-c#.exe.  It works!   Thanks


    Hector Santos, CTO Santronics Software, Inc. http://www.santronics.com


    Friday, May 8, 2020 2:02 PM