Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hello,

 

I'm declaring a structure (that will include other variable types in the future) as follows:

 

Structure SearchResults
    Dim FileNames As ArrayList
    Dim Paths As ArrayList
End Structure

 

But, I'm getting the following error:

 

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.

 

I tried changing the structure declaration to include ...as New ArrayList, but thats not allowed...

 

Thanks for any help :)

Posted

It's best to put reference types inside classes, not a structure. Structures work a bit differently and you can't set default values from outside of properties or methods like you can in classes.

 

Change your structure to a class, and change your ArrayList declarations to this;

 

Dime xxx As New ArrayList()

Gamer extraordinaire. Programmer wannabe.
Posted

I kind of understand what your saying, but I don't have much experience (read: no experience :) ) using classes. How would I access the two variables afterwards?

 

The way I wanted to use the structure was to declare variables of the structure's type and then use the variables to return various items from a function.

 

for example:

 

Structure SearchResults
    Dim FileNames As ArrayList
    Dim Paths As ArrayList
End Structure

Private Function DoStuff() as SearchResults
   Dim Results as SearchResults

   '''''do the stuff this function does

   Return SearchResults
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Returned as SearchResults

    Returned = DoStuff()

    ''''do some more stuff
End Sub

 

I'm going to go look up some stuff on classes, I'm pretty new to .NET and used VB6 for not very long.... Thanks .. :)

  • *Experts*
Posted
If you want to use properties, like this:
Public Class SearchResults
 'these are the private fields
 Private _fileNames As New ArrayList()
 Private _paths As New ArrayList()

 'these are the public properties
 Public Property Paths As ArrayList
   Get
     Return _paths
   End Get
   Set (value As ArrayList)
     _paths = value
   End Set
 End Property    

 Public Property FileNames As ArrayList
   Get
     Return _fileNames
   End Get
   Set (value As ArrayList)
     _fileNames = value
   End Set
 End Property
End Class

To use it,

Dim results As New SearchResults()

'do stuff

Return results

It may even be wise to put the "stuff" inside the class so that it can fill itself out. Makes for better OOP code.

Posted

Thank you very much, VolteFace and wyrd, that worked perfectly... :)

 

Also, I'm going to buy a book on VB.NET, any recommendations?

 

I don't need to learn the basics like what a string variable is and stuff, just other syntax such as dealing with classes, owner-drawing, etc....

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...