Results 1 to 4 of 4

Thread: Simple question using NEW

  1. #1

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Simple question using NEW

    Its been a while...

    So I wrote up a class file..compiled to a dll in vb.net..just a simple example..here is the actual class:

    Code:
    Public Class Ticket
        Private lngTicketID As Long
        Private dtOpenDate As Date
        Private dtTargetDate As Date
        Private dtClosedDate As Date
        Private strTicketDescription As String
        Private strAssignedTo As String
    
        Public Sub New()
            lngTicketID = GetNextTicketID()
            dtOpenDate = Format(Now, "dd-MMM-yy")
            dtTargetDate = DateAdd(DateInterval.Day, 7, dtOpenDate)
            dtClosedDate = Nothing
            strTicketDescription = ""
            strAssignedTo = ""
        End Sub
        Public Function GetNextTicketID() As Long
            'do some work...
            GetNextTicketID = 2
        End Function
        Public ReadOnly Property TicketID() As Long
            Get
                Return lngTicketID
            End Get
        End Property
    
        Public Sub SetClosed()
            dtClosedDate = Format(Now, "dd-MMM-yy")
        End Sub
    
        Public Sub SetTicketDescription(ByVal strTD As String)
            strTicketDescription = strTD
        End Sub
    
        Public ReadOnly Property TicketDescription() As String
            Get
                Return strTicketDescription
            End Get
        End Property
    
    End Class
    Then I created a win app form to Instantiate this object and work on some methods..I created an array of Ticket.Ticket objects..(not sure why it requires me to say Ticket.Ticket and not just Dim t as Ticket alone is another issue, even though I have imported at the top Imports Ticket.Ticket). In any event when I run the following code:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim objT1(5) As Ticket.Ticket
            Dim i As Integer = 0
    
            For i = 0 To objT1.Length - 1
                objT1(i) = New Ticket.Ticket
                objT1(i).SetTicketDescription("Hi - " & i)
                i += 1
            Next
    
            For i = 0 To objT1.Length - 1
                MsgBox(objT1(i).TicketDescription)
            Next
        End Sub
    It sets the ticket descriptions for all five elements..then it goes to the loop to messagebox them, and displays the first one fine (objT1(0) works fine)..then it increments i and tries to display objT1(1) and it fails with System.Null exceptions...why ? I have used the New Ticket.Ticket to allocate each element in the array.

  2. #2

    Thread Starter
    Banned
    Join Date
    May 2006
    Posts
    161

    Re: Simple question using NEW

    O crap I accidentally incremented i again in the first for loop...nevermind got it..
    but regarding the Ticket.Ticket why does it make me type that out twice ?

  3. #3
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    Re: Simple question using NEW

    It makes you type it out because your namespace is Ticket. If you didn't specifically set it, it defaults to the project's name (which I assume is Ticket). Since you have a namespace and class that are the same name, it forces you to put the namespace first.

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Simple question using NEW

    What MetalKid said, isn't exactly true.

    You have a namespace name and a class name.

    When you import Ticket.Ticket, that means you can access classes, and shared methods in the Ticket class. If you import only Ticket, then anthing in that namespace can be referenced by name only. So in your case, you only want to reference one Ticket (Imports Ticket).
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width