|
-
Sep 2nd, 2010, 02:41 PM
#1
Thread Starter
Addicted Member
[RESOLVED] List of New Custom Object
Ok be easy on me, this is simple for most, but I'm having trouble.
I created a simple (for now) custom object:
VB Code:
Public Class Form1
Dim AllPartFiles As List(Of Part)
.
.
.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'**NEED HELP HERE**
End Sub
.
.
.
End Class '[Form1]
Public Class Part
Public Shared FullFilePath As String
Public Shared Part Number as Integer
Public Shared Description as String
End Class '[Part]
As I search a folder for files, it returns FilePath as a string for each file in the folder, then assigns a new Part.FullFilePath to the current returned FilePath, then add the Part to AllPartFiles list. So I can retrieve each Part from AllPartFiles later. But everything I try doesnt work.
I know below is wrong, but might show what Im trying to do:
VB Code:
Dim Files() As String
Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt")
Dim k As String
For Each k In Files
Part.FullFilePath = k
AllPartFiles.Add(New Part)
Next
Dim j As Part
For Each j In AllPartFiles
MsgBox(j.Count)
Next
So how would you do this? Create a New Part, change its values, and then add it to AllPartFiles list for later, then repeat?
-
Sep 2nd, 2010, 02:49 PM
#2
Re: List of New Custom Object
first... your properties should NOT be shared... making them shared means that there will only ever be one copy. Just make them public, and that should be enough.
You're close with your loop... but you ran into problems because of your shared properties. After you've removed the shared qualifier, try this:
Code:
For each k in files
Dim newPart As New Part ' This creates a new instance of your part class
newPart.FullFilePath = k 'This sets the property
AllPartFiles.Add(newPart) 'note that is newPart, the name of the variable... not New Part :)
Next
Also, if what you are trying to get is the count of the items in your list, it's just AllPartFiles.Count... otherwise you're getting the Count property of your j varaible... with is a Part... and doesn't have a count property.
And I just noticed you have a property called "Part Number"... that shouldn't work... you can't put spaces in property names like that... jsut call it PartNumber.
-tg
-
Sep 2nd, 2010, 02:52 PM
#3
Re: List of New Custom Object
You need to create the part, THEN add it:
Code:
Dim k As String
dim p As Part
For Each k In Files
p= New Part
p.FullFilePath = k
AllPartFiles.Add(p)
Next
My usual boring signature: Nothing
 
-
Sep 2nd, 2010, 02:54 PM
#4
Re: List of New Custom Object
Doggone it. I got called away by a magazine salesman. I knew somebody would post ahead of me, so I just hastened it up.
On the other hand, the salesman was the most entertaining of the sort that I have ever encountered. Didn't buy anything, but that kid has a future.
My usual boring signature: Nothing
 
-
Sep 2nd, 2010, 02:54 PM
#5
Thread Starter
Addicted Member
Re: List of New Custom Object
@tg -
Thanks for the help. I'm still a beginner programmer so this is still a learning process for me. Not sure why I used "shared", maybe b/c some sample code on a forum somewhere used it. As for "Part Number" I just quickly typed that in and put the space in there by accident. Thanks for the help!
MotoMan
-
Sep 2nd, 2010, 03:05 PM
#6
Thread Starter
Addicted Member
Re: List of New Custom Object
Ok, still running into a problem. I have this:
VB Code:
Dim AllPartFiles As List(Of Part)
.
.
.
Dim Files() As String
Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt")
Dim k As String
Dim newPart As Part
For Each k In Files
newPart = New Part
newPart.FullFilePath = k
AllPartFiles.Add(newPart)
Next
And I'm getting this error: "Object reference not set to an instance of an object."
Nesting the "Dim newPart As Part" or having it before the "For Each" yields same result
-
Sep 2nd, 2010, 03:09 PM
#7
Re: List of New Custom Object
this is where you need to develop some debugging skills... .first thing... find out which of your objects doesn't exist yet... I know which one it is... I spotted it the first time... but this is where you need to start learning a few tricks. Hint: It isn't related to Part... but something else on the line ... remember objects must be created before you can use them... diming the variable isn't enough...
-tg
-
Sep 2nd, 2010, 03:18 PM
#8
Re: List of New Custom Object
Another hint would be: Lists are objects.
This is actually a VERY common error to get, by the way. I would say that it is the most commonly posted error message by a very wide margin. I would expect that most of us get them from time to time (I have even gotten that exception by running a particular query in SQL Server), so knowing how to investigate it is a critical skill. The steps I prefer to take are these:
1) Put a breakpoint on the line that throws the exception.
As an alternative, you could just wait until the exception is raised, but I like getting a look at the line prior to the exception. For some complex lines that involve function calls, it can give you slightly better information...maybe...I haven't fully thought it through. I guess I just prefer the breakpoint.
2) Highlight each object in turn. You may have to press Shift+F9 to see what is in the variable. Sometimes it shows up as a tooltip, sometimes it doesn't, at least that's how it works for me.
In your case, you are getting the exception on this line:
AllPartFiles.Add(newPart)
The two pieces to look at are:
AllPartFiles and newPart
The Add is a method, not an object, so it isn't worth a look. One of those two objects is Nothing. That will tell you which object was not created prior to use.
These same steps can be used to investigate any instance of this particular exception.
My usual boring signature: Nothing
 
-
Sep 2nd, 2010, 03:20 PM
#9
Thread Starter
Addicted Member
Re: List of New Custom Object
@tg, I applaud you in forcing people to LEARN rather than giving the answer (with these forums, it's too easy to grab the answer and run without actually learning anything)
Now let me break it down, and see where my flaw might be.....
VB Code:
Dim Files() As String 'Created, but not set to anything yet Files = System.IO.Directory.GetFiles(RootFolder, "*.ipt") 'now "Files" is set to what GetFiles returned Dim k As String 'Created but not set Dim newPart As Part 'Created as Part, but not set For Each k In Files 'Sets k to next entry in Files newPart = New Part 'Sets newPart to look like Part, which is still not set newPart.FullFilePath = k 'Sets string to k value AllPartFiles.Add(newPart) 'Adds newPart to AllPartFiles list Next
And..... I'm still lost...Crap.... The only thing I could think of is newPart object isnt defined yet? Even tho it's housed string is? Yet it does bark at me saying "newPart is not defined".... Didnt I define it when I set the newPart.FillFilePath?
-
Sep 2nd, 2010, 03:26 PM
#10
Re: List of New Custom Object
You defined it here: Dim newPart As Part
You instanciated it here: newPart = New Part
Are you *SURE* it's newPart that is saying is not defined? And not maybe AllPartFiles? double-click AllPartFiles to highlight it, then right-click and select "Quick Watch" ... see what it says.
-tg
-
Sep 2nd, 2010, 03:35 PM
#11
Thread Starter
Addicted Member
Re: List of New Custom Object
Well AllPartFiles is define in the beginning to be a list of "Part"s, and Im adding newPart which is a Part... Using analogies, I'm defining AllPartFiles as a tray that can only hold Parts, nothing else will fit. So in the beginning I have an empty tray. Then later I'm adding a Part to the tray. So I dont see whats wrong. What am I missing? Is my understanding of a list wrong? I'm guessing I defined the tray, yet havent created a tray to hold the parts?
-
Sep 2nd, 2010, 03:41 PM
#12
Thread Starter
Addicted Member
Re: List of New Custom Object
Ok, with what you said, sparked something... So I did this....
Dim AllPartFiles As New List(Of Part)
And it works! But whats the difference? Why does defining it as a "New" list any different than just "List"?
-
Sep 2nd, 2010, 03:47 PM
#13
Re: List of New Custom Object
Don't get caught up in the word 'List(Of Part)'. A List(Of Part) is just a class, just like Part is a class. The (Of ...) part means that it is a generic class, and the '...' can be replaced by any other type. You could have a List(Of String) or List(Of Integer) or List(Of List(Of Part)).
So, now that you know that a List(Of Part) is just another class, you can apply the same principles that you already know:
If you want to use a Part, you do two things:
1. You declare a variable that holds a Part
2. You assign a new instance of the Part class to that variable.
Code:
Dim p As Part '1.
p = New Part() '2.
You can shorten this to
Code:
Dim p As Part = New Part()
or even
Code:
Dim p As New Part()
which means exactly the same, but is just a little 'syntactic sugar'.
After the first line, all you have is some empty 'space' for a Part to exist in. But there isn't any Part in that space yet. Only after line 2 have you created a new Part and assigned it to the variable.
It works exactly the same with a list:
1. You declare a variable that can hold a List(Of Part)
2. You assign a new instance of the List(Of Part) class to that variable
Code:
Dim parts As List(Of Part)
parts = New List(Of Part)
and again, you can shorten this to
Code:
Dim parts As List(Of Part) = New List(Of Part)
or even
Code:
Dim parts As New List(Of Part)
and that's what you got.
So, you forgot to do step 2: all you had was an 'empty space' (called 'null' as the error mentions), you never assigned a list to that space. Then, you try to call the Add method of 'empty space', and this is what causes the null reference exception (you can't do that on 'nothing' obviously).
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|