Calling the subclass in a parent class array causes an error called & ldquo; can not refer to a type in an expression & rdquo; in C #

advertisements

I have a class called "Account". Here are my codes:

// Account.cs
public partial class Account
{
    private Account.Credential _credential = new Account.Credential();

    public void Login(string UserID, string UserPW)
    {
        try
        {
            // do something

            _credential.CookieCollection = browser.CookieCollection;
            _credential.CookieContainer = browser.CookieContainer;
            _credential.UserID = "test";
            _credential.UserPW = "test";
        }
        catch (Exception err)
        {
            throw err;
        }
    }
}

// Credential.cs
public partial class Account
{
    public class Credential
    {
        // Model
        public CookieCollection CookieCollection { get; set; }
        public CookieContainer CookieContainer { get; set; }
        public string UserID { get; set; }
        public string UserPW { get; set; }
    }
}

// Form1.cs
public void ABC()
{
    Account[] _account = new Account[2];
    _account[0].Login("myID", "myPW");

    Account.Credential _cred = _account[0].Credential; ---> I get an error.
}

But when I write a mothod call the Account class in array and call the sub class which is Credential, it gives me an error.

'Credential': cannot reference a type through an expression; try 'Account.Credential' instead.

Because, Login method is in Account Class I should have make an array of Account class. Not Credential class.

Does anyone know how to fix this?


As the error is trying to tell you, the Credential type is a part of the Account type; not any single Account instance.

It makes no sense to refer to the type myAccounts[42].Credential or mikesAccount.Credential.

If you want to get that user's credential, you'll need to refer to a field or property of the Account class.
You could write myAccounts[42]._credential, except that _credential is private, so that will give a different error.
You should make a public, probably-read-only, property.