0

I have a library in which a protected method is there whose definition is as below :

public class InvokeCalc : IInvokeCalc
{

    protected InvokeCalc(OtherClass comm);
    public OtherClass();
    
}

I have added this library as reference in my .net core 3.1 console application.
This is what I have written :

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            SampleDAL dal = new SampleDAL();
            dal.Trigger();
        }
    }

public class SampleDAL 
    {
        IInvokeCalc app = null;            
        public async void Trigger()
        {  
            OtherClass comm = new OtherClass();          
            app = new InvokeCalc(comm);   // error        

            Console.ReadLine();

        }
    }
    

This code throws an error saying :

InvokeCalc.InvokeCalc(string) is not accessible due to its protection level.

I have seen in some other answers that if we inherit the InvokeCalc class , we can use its protected methods.
SO for that I have changed the definition of SampleDAL to as below :

public class SampleDAL : InvokeCalc // Inherited InvokeCalc class
    {
        IInvokeCalc app = null;
        public async void Trigger()
        {            
            OtherClass comm = new OtherClass();          
            app = new InvokeCalc(comm);      

            Console.ReadLine();

        }
    }

Doing like this giving an additional error :

InvokeCalc doesnt contain a constructor that takes 0 arguments.

Now I'm not sure on how to proceed further, any guidance in this case would be very helpful.

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • 2
    If the constructor is protected, the class `InvokeCalc` is obviously not meant to be created by `new`. Maybe there is a derived class that can be created by `new`, or there is some factory method? – Klaus Gütter Feb 01 '22 at 07:52
  • 1
    Why do you make the constructor protected and not public? Is there a reason for that? Else just change the protection level to `public`. – Kevin Glier Feb 01 '22 at 07:52
  • 2
    SampleDAL needs a constructor to invoke the protected base class constructor: `public SampleDAL() : base("somestring") {}` But do talk to the author, there is a good reason why they made it protected. – Hans Passant Feb 01 '22 at 07:54
  • @KevinGlier , This class library is created by some other team who have defined this as protected. There is no way for us to change the access level for this method and are stuck with this sadly. – CrazyCoder Feb 01 '22 at 07:55
  • @HansPassant , Thanks for the reply . In place of string , if I have to pass any class object , can you please tell me how to do it . – CrazyCoder Feb 01 '22 at 08:04
  • @CrazyCoder what is the actual constructor signature? You can't just pass any random value to a method. The second error you posted *can't* be thrown by `new InvokeCalc(comm);`. It's thrown by a call to `InvokeCalc()`. Post enough code to actually reproduce that error: the `InvokeCalc` constructor and the actual constructor for `SampleDAL`. – Panagiotis Kanavos Feb 01 '22 at 08:08
  • @CrazyCoder `if I have to pass any class object` don't. You can't - C# won't let you. Find out what the constructor actually expects and only pass that value. If the constructor expects a connection string, or a name, what's the point of trying to pass an object? If the string isn't important, you could just pass `null` or `""`. I doubt your code will work if you do that though. That constructor expects a string for a reason. If the string parameter wasn't important it would have a default value, or the class would have a default constructor – Panagiotis Kanavos Feb 01 '22 at 08:10

1 Answers1

0

You should definitely talk to the authors about using that class.

That said, you are missing a constructor:

public class SampleDAL : InvokeCalc // Inherited InvokeCalc class
{
    public SampleDAL(OtherClass comm) : base(comm)
    {
    }

    IInvokeCalc app = null;

    public async Task Trigger()
    {
        OtherClass comm = new OtherClass();
        app = new SampleDAL(comm);
        await Task.CompletedTask;
        Console.ReadLine();
    }
}

This will compile, but whether it actually works very much depends on the base class' expected behaviour, and how OtherClass is supposed to work.

Also note that your code as written won't compile - the line public OtherClass(); is incorrect. I'm assuming that's a copy-paste error.

Furthermore, your implementation of Trigger() is declared as async Task but lacks any awaits, so I've put in an await Task.CompletedTask;. I assume your real code has some kind of awaitable in that method.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276