开发者

create class library

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()开发者_如何学Go
    ...
End Class

I Do have it added as a reference.

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


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!


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?).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜