Results 1 to 12 of 12

Thread: [RESOLVED] How to remove Nothing elements from Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    18

    Resolved [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

  2. #2
    Member
    Join Date
    Mar 2011
    Posts
    55

    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.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    18

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by Aerowinder View Post
    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.

  4. #4
    Member
    Join Date
    Mar 2011
    Posts
    55

    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?

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by Aerowinder View Post
    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    18

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by jmcilhinney View Post
    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    18

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by Shaggy Hiker View Post
    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?

  9. #9
    PowerPoster kaliman79912's Avatar
    Join Date
    Jan 2009
    Location
    Ciudad Juarez, Chihuahua. Mexico
    Posts
    2,593

    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:
    1. Dim flag As Boolean = False
    2. Dim found As Integer
    3. Dim m As Integer = 0
    4.  
    5.      Do While m < carArray.Length - 1
    6.         If carArray(m).carid = txtDelete.Text Then
    7.             For i As Integer = m To carArray.Lenght - 2
    8.                 carArray(m) = carArray(m + 1)
    9.             Next i
    10.             flag = True
    11.             found = m
    12.             ReDim Preserve carArray(carArray.Length - 1)
    13.         Else
    14.             m += 1
    15.         End If
    16.     Loop
    Last edited by kaliman79912; Oct 24th, 2011 at 08:23 AM.
    More important than the will to succeed, is the will to prepare for success.

    Please rate the posts, your comments are the fuel to keep helping people

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by TheStudent870 View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Sep 2011
    Posts
    18

    Re: How to remove Nothing elements from Array

    Quote Originally Posted by kaliman79912 View Post
    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:
    1. Dim flag As Boolean = False
    2. Dim found As Integer
    3. Dim m As Integer = 0
    4.  
    5.      Do While m < carArray.Length - 1
    6.         If carArray(m).carid = txtDelete.Text Then
    7.             For i As Integer = m To carArray.Lenght - 2
    8.                 carArray(m) = carArray(m + 1)
    9.             Next i
    10.             flag = True
    11.             found = m
    12.             ReDim Preserve carArray(carArray.Length - 1)
    13.         Else
    14.             m += 1
    15.         End If
    16.     Loop
    Yes! It worked. Thanks everyone.

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