[RESOLVED] How to remove Nothing elements from Array
How to remove the Nothing elements from an array using a For or For Each loop?
I have to display the array to a datagrid, but the Nothing elements of the shows up as zeros.
This is the code I have so far for my button click event
Code:
Dim flag As Boolean = False
Dim found As Integer
For m As Integer = 0 To carArray.Length - 1
If carArray(m).carid = txtDelete.Text Then
carArray(m) = Nothing
flag = True
found = m
End If
Next
Re: How to remove Nothing elements from Array
carArray(m) = ""
I've found that it is generally not a good idea for strings to have a null value. Empty string seems to be the better option in nearly every case.
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
Aerowinder
carArray(m) = ""
I've found that it is generally not a good idea for strings to have a null value. Empty string seems to be the better option in nearly every case.
It will not let me do that. It gives an error that says "Value of String cannot be converted to CarsProj.Form1.Cars" Cars is the name of my Structure.
Re: How to remove Nothing elements from Array
Is there any particular reason you need to use an array? Can I see more of your code?
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
Aerowinder
carArray(m) = ""
I've found that it is generally not a good idea for strings to have a null value. Empty string seems to be the better option in nearly every case.
I have to disagree there. Null references are quite valid regardless of the type. The only time you would have an issue is if you were accessing member without checking first, in which case you just have to make sure you check for nulls in cases where nulls are allowed.
As for the original question, why would Nothing be displayed as 0 unless the column contained numbers, in which case an empty string is obviously no help because it's not a number. It sounds to me like the user should be using a collection instead of an array, so that items can be genuinely removed and not just "cleared".
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
jmcilhinney
I have to disagree there. Null references are quite valid regardless of the type. The only time you would have an issue is if you were accessing member without checking first, in which case you just have to make sure you check for nulls in cases where nulls are allowed.
As for the original question, why would Nothing be displayed as 0 unless the column contained numbers, in which case an empty string is obviously no help because it's not a number. It sounds to me like the user should be using a collection instead of an array, so that items can be genuinely removed and not just "cleared".
I have to use an array because it is a part of the assignment. The button allows the user to delete items from the array. The problem is that Nothing still holds a place in the array. I want to get rid of it entirely.
Re: How to remove Nothing elements from Array
If you HAVE to use arrays, then you will have to use two of them. You would be doing that anyways, because you can't actually resize an array. Whenever you try that, what you are really doing is throwing out the array you have and creating a new one. Therefore, you might as well do that directly, and in one step:
Loop through the array that you have. For each item that belongs, put it in the new array. For each item that should be removed, don't put it anywhere. Once you have the new array, then assign that back to the original array.
It's a nasty piece of work. The alternative would be to use a List(of Cars). That would allow you to remove items directly, or insert them, or add them, or whatever. You could then turn that into an array with the .ToArray method. Since this is a class, it is even money that your instructor is out of date and doesn't know about the List(of T) that was added in .Net2005.
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
Shaggy Hiker
If you HAVE to use arrays, then you will have to use two of them. You would be doing that anyways, because you can't actually resize an array. Whenever you try that, what you are really doing is throwing out the array you have and creating a new one. Therefore, you might as well do that directly, and in one step:
Loop through the array that you have. For each item that belongs, put it in the new array. For each item that should be removed, don't put it anywhere. Once you have the new array, then assign that back to the original array.
It's a nasty piece of work. The alternative would be to use a List(of Cars). That would allow you to remove items directly, or insert them, or add them, or whatever. You could then turn that into an array with the .ToArray method. Since this is a class, it is even money that your instructor is out of date and doesn't know about the List(of T) that was added in .Net2005.
In my attempt to escape nasty pieces of work, can you give me a quick run through of how Lists work?
Re: How to remove Nothing elements from Array
Although, as others have mentioned, there are many ways of doing this better, I am guessing your assignment has to use what you were tought. Maybe this can help.
Everytime you find something you want to delete, move all the rest of the items in the array one place back. and redim the array, as Shaggy mentioned this actually creates a new array and disposes of the old one. Note: important to use preserve or you lose the data
vb.net Code:
Dim flag As Boolean = False
Dim found As Integer
Dim m As Integer = 0
Do While m < carArray.Length - 1
If carArray(m).carid = txtDelete.Text Then
For i As Integer = m To carArray.Lenght - 2
carArray(m) = carArray(m + 1)
Next i
flag = True
found = m
ReDim Preserve carArray(carArray.Length - 1)
Else
m += 1
End If
Loop
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
TheStudent870
I have to use an array because it is a part of the assignment. The button allows the user to delete items from the array. The problem is that Nothing still holds a place in the array. I want to get rid of it entirely.
You can't, which is the whole point. Think of an array as being like an egg carton. each element in the array is a cup in the carton. If take an egg out of the carton, it leaves an empty cup behind. You can't do anything about that. The cup is always there. It either contains an egg or it doesn't. Arrays are the same. They always have the same number of elements as when they were created. To "remove" an item from the array, all you can do is set an element to Nothing. The element still exists but it is empty. You can't change that.
The whole point of a List is to provide array-like behaviour in accessing items but to have the collection grow and shrink dynamically as you add and remove items. That is what you would do in a real application but, if you have to use arrays, you have to use arrays. If you really want to get rid of empty elements then your only choice is to create a new array and copy the old data to it.
Re: How to remove Nothing elements from Array
Code:
'Debugging Your Visual Basic Application
'http://msdn.microsoft.com/en-us/library/ms172744.aspx
'few comments since this is homework
'test array
Dim ipsumA() As String = New String() {"foo", "Lorem", "ipsum", "dolor", "sit", _
"amet", "consectetur", "adipisicing", _
"et", "dolore", "magna", "aliqua", "foo", "Ut", _
"ea", "commodo", "consequat", "foo", "Excepteur", _
"sint", "occaecat", "cupidatat", "non", "proident", _
"mollit", "foo", "anim", "id", "est", "laborum", "foo", "foo"}
Dim rslt(ipsumA.Length - 1) As String
Stop 'verify two arrays of equal length
Dim stringToRemove As String = "foo"
Dim rsltPtr As Integer = 0
For x As Integer = 0 To ipsumA.Length - 1
If ipsumA(x) <> stringToRemove Then
rslt(rsltPtr) = ipsumA(x)
rsltPtr += 1
End If
Next
Stop
If rsltPtr <> 0 Then
Array.Resize(ipsumA, rsltPtr)
Array.Copy(rslt, ipsumA, rsltPtr)
End If
Stop
Re: How to remove Nothing elements from Array
Quote:
Originally Posted by
kaliman79912
Although, as others have mentioned, there are many ways of doing this better, I am guessing your assignment has to use what you were tought. Maybe this can help.
Everytime you find something you want to delete, move all the rest of the items in the array one place back. and redim the array, as Shaggy mentioned this actually creates a new array and disposes of the old one. Note: important to use preserve or you lose the data
vb.net Code:
Dim flag As Boolean = False
Dim found As Integer
Dim m As Integer = 0
Do While m < carArray.Length - 1
If carArray(m).carid = txtDelete.Text Then
For i As Integer = m To carArray.Lenght - 2
carArray(m) = carArray(m + 1)
Next i
flag = True
found = m
ReDim Preserve carArray(carArray.Length - 1)
Else
m += 1
End If
Loop
Yes! It worked. Thanks everyone.