简体   繁体   English

如何在项目vb.net中声明两个具有相同名称的类

[英]How to declare two classes with the same name in the project vb.net

I'm starting in vb.net and I'm migrating my project vb6 to vb.net. 我从vb.net开始,我正在将我的项目vb6迁移到vb.net。 In my project I have in a file, a class named A, declared as follows: 在我的项目中,我有一个文件,一个名为A的类,声明如下:

Public Class A
.....
End Class

In another file I have to declare another class for local use, with the same class name already declared earlier in another file (Class A). 在另一个文件中,我必须声明另一个类供本地使用,并且在另一个文件(A类)中已经声明了相同的类名。 But both classes are different. 但两个班级都不同。

Then I tried the following: 然后我尝试了以下内容:

Public Class Z
  Private _a As A
    Public Property a () As A
        Get
            Return _a
        Get End
        Set (ByVal value As A)
            _a = value
        End Set
    End Property
End Class Z

Class A
......
End Class

Then I tried the following: 然后我尝试了以下内容:

Public Class Z
  Private _a As A
    Public Property a () As A
        Get
            Return _a
        Get End
        Set (ByVal value As A)
            _a = value
        End Set
    End Property

Class A
......
End Class

End Class Z

The need is that when instantiating ZI get access local Class A But I have the error message with saying: 需要的是,在实例化ZI时访问本地A类但我有错误信息说:

Error 4 class 'A' and class 'A', declared in 'C: \\ TFS \\ SigaCom \\ BsControl System 2014 \\ BsSystem \\ nfe \\ A.vb' conflict in namespace 'BSSystemControl'. 错误4类'A'和类'A',在名称空间'BSSystemControl'中的'C:\\ TFS \\ SigaCom \\ BsControl System 2014 \\ BsSystem \\ nfe \\ A.vb'冲突中声明。 C: \\ TFS \\ SigaCom \\ BsControl System 2014 \\ BsSystem \\ nfe \\ impostoDevol.VB 12 7 BSControl C:\\ TFS \\ SigaCom \\ BsControl System 2014 \\ BsSystem \\ nfe \\ impostoDevol.VB 12 7 BSControl

If I place the Class A statement within the class Z Scope, I got the following message: 如果我将A类语句放在Z Scope类中,我收到以下消息:

Error 4 'A' is already declared the 'Public Property A The A' in this class. 错误4'A'已在此类中声明为'公共财产A A'。 C: \\ TFS \\ SigaCom \\ BsControl System 2014 \\ BsSystem \\ nfe \\ impostoDevol.VB 12 11 BSControl C:\\ TFS \\ SigaCom \\ BsControl系统2014 \\ BsSystem \\ n \\ n \\ impostoDevol.VB 12 11 BSControl

If your goal is serialization, your class names do not matter in this regard. 如果您的目标是序列化,那么您的类名在这方面无关紧要。 By default when serializing to XML the properties are what matter, the use of the property name can be overridden using the XmlElement(string) attribute. 默认情况下,在序列化为XML时,属性是重要的,可以使用XmlElement(string)属性覆盖属性名称的使用。 For instance, given the following structure: 例如,给定以下结构:

Public Class Z
    Private _a As A2
    <XmlElement("A")> _
    Public Property a () As A2
        Get
            Return _a
        Get End
        Set (ByVal value As A2)
            _a = value
        End Set
    End Property

   Class A2
      '......
   End Class

End Class Z

Will serialize the to the same XML as: 将序列化为相同的XML:

Public Class Z
    Private _a As A2
    <XmlElement("A")> _
    Public Property ZZZZ () As A2
        Get
            Return _a
        Get End
        Set (ByVal value As A2)
            _a = value
        End Set
    End Property

   Class A2
      '......
   End Class

End Class Z

Which will also serialize to the same XML as: 这也将序列化为相同的XML:

Public Class Z
    Private _a As ZZZZ
    <XmlElement("A")> _
    Public Property CDEF () As ZZZZ
        Get
            Return _a
        Get End
        Set (ByVal value As ZZZZ)
            _a = value
        End Set
    End Property

   Class ZZZZ
      '......
   End Class

End Class Z

The naming of your members does not follow naming conventions. 您的成员的命名不遵循命名约定。 Private objects should be named in camelCase, avoid using the "_" char at start, end, neither in the middle, public properties ans classes in TitleCase, also, names should be intuitive, a kind of short-name of a larger name. 私有对象应该在camelCase中命名,避免在开始,结束时使用“_”字符,也不要在中间使用TitleCase中的公共属性和类,名称应该是直观的,一种较大名称的短名称。

Secondlly, I don't think you need a backing field at all for the sceneraio that you've exposed. 其次,我认为你所暴露的场景照片根本不需要背景场。

Anyways, to solve the problem that you are having just move each class with same classname to different namespaces with different names, this way you will avoid ambiguity, but that style of programming is incorrect. 无论如何,要解决您只是将具有相同类名的每个类移动到具有不同名称的不同命名空间的问题,这样您将避免歧义,但这种编程风格是不正确的。

If I understood good, this is what you intent to do?: 如果我理解得很好,这就是你打算做的事情:

Public class TopLevelClass

Namespace name1

    Public Class Zclass

        Public Property A As Aclass

        Public Sub New()
            Me.A = New Aclass
        End Sub

        Public NotInheritable Class Aclass
            Public Sub New()
            End Sub
        End Class

    End Class

End Namespace

Namespace name2

    Public Class Zclass

        Public Property A As Aclass

        Public Sub New()
            Me.A = New Aclass
        End Sub

        Public NotInheritable Class Aclass
            Public Sub New()
            End Sub
        End Class

    End Class

End Namespace

End Class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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