无法访问基类的嵌套类或成员 [英] Cannot access nested classes or members of base class

查看:184
本文介绍了无法访问基类的嵌套类或成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些奇怪的问题,这里有一个类,我已经写了。我不能帐户内访问任何,除非我直接访问它 Account.Whatever

我希望能够做到:

 帐户的帐户=新帐户();
用户名...
 

但我不能。在智能感知没有显示。我只能访问的事情,如果我做的:

帐户 - 例如, Account.AccountHolder ...

 类帐户
{
    类账户持有
    {
        枚举称谓
        {
            先生,
            太太,
            女士,
            小姐,
            博士,
            汉
        }

        结构名称
        {
            公共字符串名字{获得;组; }
            公共字符串MiddleName {获得;组; }
            公共字符串名字{获得;组; }
        }

        枚举性别
        {
            男,
            女
        }
    }
}
 

我不明白是怎么回事。请注意,我也尝试过的东西每一个可能的组合,但事情是非常错误的在这里。我试图加入公开我的帐户类。我试图加入公开来我的账户持有类。我已经尝试过使用公共静态等等等等。

我从来没有过这样的问题。为什么我会遇到同样的问题,不管多少,我周围的改变呢?

帐户类是在 Account.cs 文件相同的WinForms项目中。

解决方案
  

一个真正的问题可能是:为什么需要嵌套类型在这里呢?

嵌套类型中专门用于在没有其他类型不能重复使用一种类型的父类型,也就是说,如果你的嵌套类型应公开属性或值仅适用于父类型。否则,它主要是最好创建独立的类型。

对我来说,它看起来有理由认为你可能会使用称谓枚举账户持有类外,作为账户持有人是不是一个法人实体罢了,那就是,一个真正的人或公司。

如果您的系统可以使用称谓其他地方,比它是最好创建枚举每本身,在它自己的文件,并公开财产出你的账户持有类。

称谓

 公开枚举称呼{
    先生
    , 太太
    , 女士
    , 小姐
    博士
    鸿海
}
 

账户持有

 公共类账户持有{
    公共称谓称呼{获得;组; }
    // ...
}
 

在以后,人们也可能insterested知道什么是账户持有人一次?

它可能是一个企业,一个人,客户,供应商,要不?

那么也许你应该考虑定义帐户持有人的层次结构,并使其成为最通用的类​​类型的属性。

LegalEntity

 公共类LegalEntity {
    公共字符串名称{;组; }
}
 

公司

 公共类公司:LegalEntity {
    //具体到一个公司在这里的一些会员...
}
 

 公共类人:LegalEntity {
    公共称谓称呼{获得;组; }
    公共字符串名字{获得;组; }
    公共字符串MiddleName {获得;组; }
    公共字符串名字{得到{base.Name; }集合{base.Name =价值; }}
    //具体到这里的人的一些其他成员...
}
 

那么,你有你的帐户

 公共类的帐户{
    公共LegalEntity账户持有{获得;组; }
}
 

所以,我的观点是,有没有用嵌套类型在这里,根据您的需要,而我实际上并不知道,很明显。而事实证明,一个账户持有现在可以是任何类型的 LegalEntity 派生的。后来,当有需要另一种类型的账户持有,你可以简单地从 LegalEntity 导出,或其他任何类型实际上提炼出来,以使其成为账户持有,作为账户持有简直就是一个属性帐户,而不是每个本身就是一个类。

使用的一些例子嵌套类型充分的:​​

此外,你需要让你的嵌套类型的公共以便从您的类的外部访问它们。这并不意味着将能够避免Parent.NestedType命名法,则不会。

除了它,我看到你的code没有问题。嵌套类型被定义在另一个类型莫名其妙地隐藏起来。所以,当你想访问他们,你总是需要在其中包含您需要访问的类型父名键入。

另外,一旦你可以访问嵌套类型,你将不得不创建成员到您的帐户类holde引用您的嵌套类型的实例。恕我直言,这里存在使用它们的没有收获。但是,嘿,我坚持,我不知道你的现实和你的设计背后的选择。

I'm having some weird problems here with a Class that I've written. I cannot access anything inside of Account, unless I access it directly from Account.Whatever.

I'd like to be able to do:

Account account = new Account();
account.Name...

but I can't. Nothing shows up in intellisense. I can only access things if I do:

Account. - for example, Account.AccountHolder...

class Account
{
    class AccountHolder
    {
        enum Salutation
        {
            Mr,
            Mrs,
            Ms,
            Miss,
            Dr,
            Hon
        }

        struct Name
        {
            public string FirstName { get; set; }
            public string MiddleName { get; set; }
            public string LastName { get; set; }
        }

        enum Sex
        {
            Male,
            Female
        }
    }
}

I don't understand what's going on. Note, that I have also tried every possible combination of things but something is very wrong here. I've tried adding public to my Account class. I've tried adding public to my AccountHolder class. I've tried using public static etc etc etc.

I've never had this problem before. And why am I experiencing this same problem no matter how much I change it around?

The Account class is in an Account.cs file inside the same winforms project.

解决方案

One real question might be: Why do you need Nested Types here at all?

Nested types are especially used when no other types cannot reuse a type of your parent type, that is, if your nested type shall expose properties or values only applicable to your parent type. Otherwise it is mostly best to create independant types.

To me, it looks reasonable to think that you might use the Salutation enumeration outside of the AccountHolder class, as an Account Holder is nothing more than a legal entity, that is, a real person or a company.

If your system could use Salutation elsewhere, than it is best to create the enumeration per itself, in its own file, and expose a property out of your AccountHolder class.

Salutation

public enum Salutation {
    Mr
    , Mrs
    , Ms
    , Miss
    , Dr
    , Hon
}

AccountHolder

public class AccountHolder {
    public Salutation Salutation { get; set; }
    // ...
}

In the later, one might also be insterested to know what's an account holder at once?

Might it be a company, a person, a customer, a supplier, or else?

Then perhaps shall you consider to define a hierarchy of account holders and make it a property of the most general class type.

LegalEntity

public class LegalEntity {
    public string Name { get; set; }
}

Company

public class Company : LegalEntity {
    // Some members specific to a Company here...        
}

Person

public class Person : LegalEntity {
    public Salutation Salutation { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get { return base.Name; } set { base.Name = value; } }
    // Some other members specific to a person here...
}

Then, you have your Account class

public class Account {
    public LegalEntity AccountHolder { get; set; }
}

So my point is that there is no use of Nested Types here, depending on your needs, which I'm not actually aware, obviously. And it turns out that an AccountHolder may now be of any types deriving from LegalEntity. Later on, when there is a need for another type of AccountHolder, you may simply derive from LegalEntity, or any other types which actually derives from it to make it an AccountHolder, as an AccountHolder is simply a property of an Account, and not a class per itself.

Some examples of using Nested Types adequately:

Furthermore, you will need to make your Nested Types public in order to access them from outside of your class. This doesn't mean that will be able to avoid the Parent.NestedType nomenclature, you will not.

Apart from it, I see no problem in your code. Nested Types are by definition hidden somehow within another type. So when you wish to access them, you always need to type in the parent name which contains the type you need to access.

Plus, once you can access the Nested Type, you will be obliged to create members into your Account class to holde references to your instances of those Nested Types. IMHO, there is no gain of using them here. But hey, I insist, I'm not aware of your reality and the choices behind your design.

这篇关于无法访问基类的嵌套类或成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆