|
-
Apr 22nd, 2008, 09:43 AM
#1
Thread Starter
Frenzied Member
[2005] Array List Error
Hello i made a few text boxes and i want all the text of the text boxes to become combined into an array here is the code i used. (Some of it.)
Code:
Try
My.Settings.CustomerNum = My.Settings.CustomerNum + 1
CustomerNum = My.Settings.CustomerNum
My.Settings.CustomerID.Add(" Name: " & txtName.Text.Trim & " Phone#: " & txtPhone.Text.Trim & " Country: " & txtCountry.Text.Trim & " State/Province: " & txtProvince.Text.Trim & " City: " & txtCity.Text.Trim & " Address: " & txtAddress.Text.Trim & " C#: " & (CustomerNum))
My.Settings.CustomerNum = CustomerNum
CustomerRecords.frmCustomerView.customerview.Items.Add(My.Settings.CustomerID(CustomerNum))
Catch ex As Exception
MsgBox("Sorry, Customer Tracker 1.0 has encounted a problem. " & ex.Message, MsgBoxStyle.Critical, "Error")
End Try
I always get the exception error message. The actual error message the ex.message is:
Object referance not set to an instant of an object.
CustomerNum is an integer (constantly updates from my.settings etc...)
"CustomerView" is a list box.
-
Apr 22nd, 2008, 09:53 AM
#2
Re: [2005] Array List Error
The problem is likely that you have declare My.Settings.CustomerID as an ArrayList, but haven't instantiated it anywhere, so there's no object to add the string to.
Before the .Add line, put in:
Code:
If My.Settings.CustomerID Is Nothing Then
My.Settings.CustomerID = new ArrayList() 'Or whatever you've declare it as.
End If
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 10:01 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Array List Error
- I have delcared My.Settings.CustomerID as an ArrayList.
I want to add more stuff to the arraylist then just one so i cant just do Is Nothing Then etc...
Do you mean i must declare My.Settings.CustomerID as a new ArrayList() then were do i do the code i declared it as.... ill try figure it out if not ill come back here.
-
Apr 22nd, 2008, 10:03 AM
#4
Thread Starter
Frenzied Member
Re: [2005] Array List Error
nvm i see i forgot how arrays work.
ArrayList(Then here i put how many items/strings are in here) is it possible to make it infinity so i can add any amount?
-
Apr 22nd, 2008, 10:05 AM
#5
Re: [2005] Array List Error
 Originally Posted by noahssite
- I have delcared My.Settings.CustomerID as an ArrayList.
I want to add more stuff to the arraylist then just one so i cant just do Is Nothing Then etc...
Do you mean i must declare My.Settings.CustomerID as a new ArrayList() then were do i do the code i declared it as.... ill try figure it out if not ill come back here.
That's why there's the If there. The first time you access it, it's going to be Nothing, so it will get instantiated. The next time you access it, it will not be Nothing, so you'll skip the instantiation and just go onto the Add.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 10:16 AM
#6
Thread Starter
Frenzied Member
Re: [2005] Array List Error
Sorry im a bit confused it doesnt seem to work i may have done it wrong when inserting your code...
Can you give me a small example with the code i used.
-
Apr 22nd, 2008, 10:19 AM
#7
Re: [2005] Array List Error
Code:
Try
My.Settings.CustomerNum = My.Settings.CustomerNum + 1
CustomerNum = My.Settings.CustomerNum
If My.Settings.CustomerID Is Nothing Then
My.Settings.CustomerID = new ArrayList() 'Or whatever you've declare it as.
End If
My.Settings.CustomerID.Add(" Name: " & txtName.Text.Trim & " Phone#: " & txtPhone.Text.Trim & " Country: " & txtCountry.Text.Trim & " State/Province: " & txtProvince.Text.Trim & " City: " & txtCity.Text.Trim & " Address: " & txtAddress.Text.Trim & " C#: " & (CustomerNum))
My.Settings.CustomerNum = CustomerNum
CustomerRecords.frmCustomerView.customerview.Items.Add(My.Settings.CustomerID(CustomerNum))
Catch ex As Exception
MsgBox("Sorry, Customer Tracker 1.0 has encounted a problem. " & ex.Message, MsgBoxStyle.Critical, "Error")
End Try
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 10:21 AM
#8
Re: [2005] Array List Error
Just an FYI, if you are using .NET 2.0, the DO NOT USE arraylists.
The ONLY time you should use an arraylist, is when you need to group objects of DIFFERENT types.
Otherwise use a generic class of the type your list holds. Generics were added in .NET 2.0, and all but killed the need to use generic arraylists that need constant type conversions.
-
Apr 22nd, 2008, 10:37 AM
#9
Thread Starter
Frenzied Member
Re: [2005] Array List Error
It says index was out of range... Same message i got on my own code.
That means i cant just right .....New ArrayList() i need to add something in between the ()? or what?
What do you mean by:
My.Settings.CustomerID = new ArrayList() 'Or whatever you've declare it as.
I declared it as a ArrayList...
-
Apr 22nd, 2008, 10:44 AM
#10
Re: [2005] Array List Error
 Originally Posted by noahssite
It says index was out of range... Same message i got on my own code.
That means i cant just right .....New ArrayList() i need to add something in between the ()? or what?
What line does the error come on? You don't need any parameters to declare an arraylist
 Originally Posted by noahssite
What do you mean by:
I declared it as a ArrayList...
As kleinma said, why are you using an ArrayList when you simply want to add a bunch of strings to it? A List(Of String) would be much more efficient.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 01:00 PM
#11
Thread Starter
Frenzied Member
Re: [2005] Array List Error
How does the List(Of String) work? Ill look into that then. I wanted an arraylist because im not good with databases so i wanted to replace a datebase with just many strings...
So i thought it would be reasonable to make an Array List.
-
Apr 22nd, 2008, 01:08 PM
#12
Re: [2005] Array List Error
An ArrayList is a list of objects. If you want to use the data for anything, then it has to be cast into the appropriate type in order to be accessed. When you use a List, you can specify what's going to be in the List, so you don't have to do any conversions to save and retrieve it. It's faster and more efficient.
(VB/C#) is clearly superior to (C#/VB) because it (has/doesn't have) <insert trivial difference here>.
-
Apr 22nd, 2008, 02:31 PM
#13
Re: [2005] Array List Error
not being good at something is no excuse to not use something. It presents the chance to learn about it and then be able to use it.
A list of strings and a database are 2 totally different animals. If you need any type of data structure at all, then storing a whole bunch of info in 1 string is a very bad idea.
There are lots of tutorials about databases and XML documents on this forum and around the web, and of course you can ask questions when you are stuck somewhere.
So I have no idea what your app does, but you should use the right tools for the job if you want to make the app any good.
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
|