0

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?

Joshua Son
  • 1,839
  • 6
  • 31
  • 51
  • `throw ex` is evil. http://stackoverflow.com/a/2999314/34397 – SLaks Aug 26 '13 at 02:41
  • You need to access an `Instance` member of Account class which holds the credentials. In your case `_credentials`. You need to make it public though. – Nilesh Aug 26 '13 at 02:47

2 Answers2

2

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

This is happening because Account.Credential is an internal type to Account and isn't used like how you're using it. What code is attempting to do would be similar to trying

var str = String;

You need to initialize an Account.Credential. A simple was is to use new inside Account's ctor or the Login() method.

I noticed you're going to run into further problems with your code - if you want to access the Account.Credential reference declared inside Account, you'll need to expose it through a method or change the access modifier.

jdphenix
  • 15,022
  • 3
  • 41
  • 74