简体   繁体   中英

Can't create instance since no default constructor, but only available constructor is not public. How to call public members from class?

I am trying to access information from a API trading position by going

Position position = new Position();
int positionQuantity = position.Net;
Console.WriteLine("position amount is: " + positionQuantity);

However, it is giving me a red line under new Position();

and saying the T4.API.Position has no default constructors defined.

but then if I go

Position position = default(Position); 
int positionQuantity = position.Net;
Console.WriteLine("position amount is: " + positionQuantity);

"object reference not set to an instance of an object"

So it is wanting me to use the "new" keyword but I can't because it says it does not have a default constructor

I opened up their Position class and it does have a bunch of delegates should I use one of those?

there is no default constructor however, they do have a constructor with 3 arguments, however, it is an "internal"

public class Position
{
    public delegate DelegateName(arg1, arg2, arg3);
    // a bunch of other delegates

    internal Position(arg1, arg2, arg3)
    {
        //properties, etc..
    }

    public int Net
    {
        get { return _net; }
    }
}

Not sure how to call the member Net if I can't create an instance because no default constructor and the only available constructor is not public but internal?

There could be some reasons why a class will not have an available default constructor:

  • The only constructor(s) the class have requires parameters
  • The default constructor is private, like in singleton
  • The class is static, and can't be instansiated, like System.IO.File
  • The class is abstract and is designed as a base class for the classes you should use, like System.Drawing.Image

I've probably forgot to list some reasons, but I hope you get the picture: You have the code so you can see for yourself, we don't...

As MSDN says

If a class contains no instance constructor declarations, a default instance constructor is automatically provided . That default constructor simply invokes the parameterless constructor of the direct base class. If the direct base class does not have an accessible parameterless instance constructor, a compile-time error occurs. If the class is abstract then the declared accessibility for the default constructor is protected. Otherwise, the declared accessibility for the default constructor is public.

Unless the class is static, classes without constructors are given a public default constructor by the C# compiler in order to enable class instantiation.

In your case you must post your queries to the API vendors as we can not see the code and they are the best doctors for the issue .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM