创建类库

发布于 2024-10-27 12:26:10 字数 1035 浏览 3 评论 0原文

我正在使用 Visual Studio 2010 并尝试创建一个类库,然后在应用程序中使用该类库,该应用程序将使用 TaskFactory 创建任务并执行新创建的类库中的类函数。但是我在应用程序中收到实例未定义错误。

类库:
程序集名称:GPStream
根命名空间:TFStream

具有不同类的项目: GPStream:
gStream.vb,
解析器T.vb,
insertDB.vb

Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Net.Mail
Imports System.Threading
Imports System.Threading.Tasks

Public Class gStream
   Private Shared settings As New System.Configuration.AppSettingsReader
   ' application configuration variables'

   Public Sub New()
      getConfigurations()
   End Sub
   ...
End Class


Imports System.IO
Imports System.Web
Imports TFStream
Imports System.Configuration
Imports System.Threading
Imports System.Threading.Tasks


Public Class Form1
    Private gp1 As TFStream.GPStream.gStream = New TFStream.GPStream.gStream()
    ...
End Class

我确实将其添加为参考。

类库:程序集名称GPStream,根命名空间GPStream 项目名称GPStream,主类gStream

I am using visual studio 2010 and tried to create a class library which I then use in an application that will create tasks using the TaskFactory and execute the class functions from the newly created class library. However I get the instance not defined errors in the application.

CLASS LIBRARY:
Assembly Name: GPStream
Root namespace: TFStream

Project with different classes:
GPStream:
gStream.vb,
parserT.vb,
insertDB.vb

Imports System.Data.SqlClient
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Net.Mail
Imports System.Threading
Imports System.Threading.Tasks

Public Class gStream
   Private Shared settings As New System.Configuration.AppSettingsReader
   ' application configuration variables'

   Public Sub New()
      getConfigurations()
   End Sub
   ...
End Class


Imports System.IO
Imports System.Web
Imports TFStream
Imports System.Configuration
Imports System.Threading
Imports System.Threading.Tasks


Public Class Form1
    Private gp1 As TFStream.GPStream.gStream = New TFStream.GPStream.gStream()
    ...
End Class

I Do have it added as a reference.

Class library: Assembly name GPStream, Root Namespace GPStream
Project name GPStream, Main Class gStream

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇年纸鸢 2024-11-03 12:26:10

好的,我将提供一个可能更简单的示例,我希望它能帮助您实现目标。

假设我们有一个业务实体项目,我们希望在 WinForm 项目中使用它。然后我们有两个项目,为了简单起见,在同一个解决方案中。

  1. WinFormApp(MyCompany.MyProject.WinFormApp.exe);
  2. 业务实体(MyCompany.MyProject.BusinessEntities.dll)。

我们将其视为这些程序集文件包含相同的程序集名称和默认命名空间。

我的 WinFormApp 项目的命名空间是:MyCompany.MyProject.WinFormApp
并且:MyCompany.MyProject.BusinessEntities 用于类库。让我们从包含以下两个类的类库开始:

  1. Customer;
  2. 供应商;
  3. 信用条款。

在会计中,这两者从不同的角度使用,即针对客户的应收账款和针对供应商的应付账款。可以分配适当的命名空间来反映这一现实。

Customer 类应如下所示:

Imports MyCompany.MyProject.BusinessEntities

Namespace AccountReceivable
    Public Class Customer
        Private _id As Integer
        Private _name As String
        Private _phoneNumber As Long
        Private _creditTerm As CreditTerm

        Public Sub New()
            Term = CreditTerm.None
        End Sub

        Public Sub New(ByVal pId As Integer _
                        , ByVal pName As String _
                        , ByVal pPhoneNumber As Long)
            _id = pId
            Name = pName
            PhoneNumber = pPhoneNumber
            Term = CreditTerm.None
        End Sub

        Public ReadOnly Property Id As Integer
            Get
                Return _id
            End Get
        End Property

        Public Property Name As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                If (String.IsNullOrWhiteSpace(value)) Then _
                    Throw New ArgumentNullException("Name")

                _name = value
            End Set
        End Property

        Public Property PhoneNumber As Long
            Get
                Return _phoneNumber
            End Get
            Set(ByVal value As Long)
                _phoneNumber = value
            End Set
        End Property

        Public Property Term As CreditTerm
            Get
                Return _term
            End Get
            Set(ByVal value As CreditTerm)
                _term = value
            End Set
        End Property
    End Class
End Namespace

然后,Supplier 类:

Imports MyCompany.MyProject.BusinessEntities ' Used for the CreditTerm Enum Property'

Namespace AccountPayable
    Public Class Supplier
        ' We will here consider having the same properties as Customer for simplicity.'
    End Class
End Namespace

还有 CreditTerms 枚举,此处可以将其分配给我们购买的供应商和我们销售的客户。从这个角度来看,此类可能位于项目的根目录中以说明其常见用途。

Public Enum CreditTerm
    None
    N30
    N45
    N60
    N90
End Enum

然后,我们的 WinFormApp 项目应类似于以下内容:

Imports System
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Imports MyCompany.MyProject.BusinessEntities 'Indeed you have to add the reference...'
Imports MyCompany.MyProject.BusinessEntities.AccountReceivable

Public Partial Class Form1
    Inherits Form

    Private _netTerm As CreditTerm = CreditTerm.None
    Private _customer As Customer = New Customer()
    Private _supplier As AccountPayable.Supplier = New AccountPayable.Supplier()
    ...
End Class

请注意,我已明确导入 MyCompany.MyProject.BusinessEntitiesMyCompany.MyProject.BusinessEntities.AccountReceivable 以说明差异CreditTerm 枚举和 Customer 类之间的命名空间。因此,我没有导入 Supplier 类的命名空间,这迫使我指向查找该类的位置。

最后,我可以使用 MyCompany.MyProject.BusinessEntities.Customer 命名空间将与客户相关的所有内容分组到同一命名空间下,同时将 Customer 类放在 Customer 命名空间内。但是,即使在导入名称空间时,您也必须编写以下内容:

Private _customer As Customer.Customer = New Customer.Customer()

编写时想到了另一件事。尝试构建/生成/重新生成类库,然后将其添加为引用。也许您引用类库的项目无法找到二进制文件,除非您确实通过项目引用引用了它。

请随时提出任何有关被误解的细节的问题,甚至可以说我自己可能误解了您的问题和担忧。

我真的希望这有帮助!

Okay, I'll have it with a perhaps simpler sample which, I hope, will help you achieve your goal.

Take it we have a business entities project which we want to use within our WinForm project. We then have two projects, say within the same solution for simplicity sake.

  1. WinFormApp (MyCompany.MyProject.WinFormApp.exe);
  2. BusinessEntities (MyCompany.MyProject.BusinessEntities.dll).

We shall take it as these assembly files contains the same assembly name and default namespace.

The namespace of my WinFormApp project is: MyCompany.MyProject.WinFormApp
And: MyCompany.MyProject.BusinessEntities for the class library. Let's begin with the class library which contains the two following classes:

  1. Customer;
  2. Supplier;
  3. CreditTerms.

In accounting, these two are used from a different point of views which are Account Receivable for the Customer and Account Payable for the Supplier. Proper Namespace could be assigned to reflect this reality.

The Customer class shall look like this:

Imports MyCompany.MyProject.BusinessEntities

Namespace AccountReceivable
    Public Class Customer
        Private _id As Integer
        Private _name As String
        Private _phoneNumber As Long
        Private _creditTerm As CreditTerm

        Public Sub New()
            Term = CreditTerm.None
        End Sub

        Public Sub New(ByVal pId As Integer _
                        , ByVal pName As String _
                        , ByVal pPhoneNumber As Long)
            _id = pId
            Name = pName
            PhoneNumber = pPhoneNumber
            Term = CreditTerm.None
        End Sub

        Public ReadOnly Property Id As Integer
            Get
                Return _id
            End Get
        End Property

        Public Property Name As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                If (String.IsNullOrWhiteSpace(value)) Then _
                    Throw New ArgumentNullException("Name")

                _name = value
            End Set
        End Property

        Public Property PhoneNumber As Long
            Get
                Return _phoneNumber
            End Get
            Set(ByVal value As Long)
                _phoneNumber = value
            End Set
        End Property

        Public Property Term As CreditTerm
            Get
                Return _term
            End Get
            Set(ByVal value As CreditTerm)
                _term = value
            End Set
        End Property
    End Class
End Namespace

Then, the Supplier class:

Imports MyCompany.MyProject.BusinessEntities ' Used for the CreditTerm Enum Property'

Namespace AccountPayable
    Public Class Supplier
        ' We will here consider having the same properties as Customer for simplicity.'
    End Class
End Namespace

And the CreditTerms enumeration which here can be assigned to both a Supplier from which we buy, and a Customer to which we sell. Under that perspective, this class could either be at the root of your project to illsutrate its common use.

Public Enum CreditTerm
    None
    N30
    N45
    N60
    N90
End Enum

Then, our WinFormApp project should look something similar to:

Imports System
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms

Imports MyCompany.MyProject.BusinessEntities 'Indeed you have to add the reference...'
Imports MyCompany.MyProject.BusinessEntities.AccountReceivable

Public Partial Class Form1
    Inherits Form

    Private _netTerm As CreditTerm = CreditTerm.None
    Private _customer As Customer = New Customer()
    Private _supplier As AccountPayable.Supplier = New AccountPayable.Supplier()
    ...
End Class

Notice here that I have expressly imported the MyCompany.MyProject.BusinessEntities and MyCompany.MyProject.BusinessEntities.AccountReceivable to illsutrate the differences of namespace between the CreditTerm enumeration and the Customer class. Therefore, I have not imported the the Supplier class' namespace, which obliges me to point to the location where to find the class.

In the end, I could have had MyCompany.MyProject.BusinessEntities.Customer namespace to group everything that regards a customer under the same namespace while having the Customer class inside the Customer namespace. But then, even when importing the namespace, you would have to write the following:

Private _customer As Customer.Customer = New Customer.Customer()

Another thing comes to mind while writing. Try to build/generate/regenerate the class library, then add it as a reference. Perhaps your project where the class library is referenced can't find the binaries, except if you did reference it through a Project Reference.

Please feel free to ask any question about misunderstood details or so, or even to say I myself have perhaps misunderstood your question and concerns.

I really DO hope this helps!

花之痕靓丽 2024-11-03 12:26:10

最后一个 Imports 语句有效吗?如果是这样,则表明 GPStream.gstream 是一个命名空间,而不是一个类。您的类可能具有不同的名称或嵌套在该名称空间内(您尝试过 GPStream.gstream.gstream 吗?)。

Does the last Imports statement work? If so, that indicates that GPStream.gstream is a namespace, not a class. Your class probably has a different name or is nested inside that namespace (have you tried GPStream.gstream.gstream?).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文