Clearing Arrays
Clearing Arrays
Clearing Arrays
Clearing Arrays
Clearing Arrays
Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays
Clearing Arrays Clearing Arrays
Clearing Arrays
Go Back  Xtreme Visual Basic Talk > > > Clearing Arrays


Reply
 
Thread Tools Display Modes
  #1  
Old 10-17-2011, 10:51 AM
sanderson sanderson is offline
Junior Contributor
 
Join Date: Feb 2006
Location: Minnesota, USA
Posts: 217
Default Clearing Arrays


Hi:

I would like to delete the contents of my array in .NET code. The examples I have seen say...

HTML Code:
        Array.Clear(adoc, 0, UBound(adoc))
Which produces: An unhandled exception of type 'System.ArgumentNullException' occurred in Microsoft.VisualBasic.dll

Additional information: Value cannot be null.


So I modified the code like this...

HTML Code:
        If adoc IsNot Nothing Then
            Array.Clear(adoc, 0, UBound(adoc))

        End If
But it does not clear out the array.

This is way too simple to be so complicated. I just want to delete the array between populations.

Thanks in advance for any insights.
Reply With Quote
  #2  
Old 10-17-2011, 12:11 PM
PlausiblyDamp's Avatar
PlausiblyDampClearing Arrays PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

When you say it doesn't clear out the array what exactly do you mean? Array.Clear will simply zero, null or set to false each item, in the array depending on it's type.

If you need a new array then just let the variable go out of scope and create a new array when you need one.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 10-17-2011, 12:15 PM
sanderson sanderson is offline
Junior Contributor
 
Join Date: Feb 2006
Location: Minnesota, USA
Posts: 217
Default

The length of adoc just keeps on growing! If there a way to just wipe it out completely and let the code recreate?
Reply With Quote
  #4  
Old 10-17-2011, 02:41 PM
PlausiblyDamp's Avatar
PlausiblyDampClearing Arrays PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

I assume you are using ReDim somewhere to increase the size of the array then, in which case it will keep on growing as Array.Clear doesn't change the size of the array. You could just simply assign a new array to the variable - that would do the trick. Although if you are growing an array it might be easier to use something like the List(Of ...) class instead as it is far easier to work with.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #5  
Old 10-17-2011, 03:01 PM
sanderson sanderson is offline
Junior Contributor
 
Join Date: Feb 2006
Location: Minnesota, USA
Posts: 217
Default

Okay, here's some code to ponder...

HTML Code:
    Private Sub cmdBOMCompare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBOMCompare.Click
        ' Clear adoc

        If adoc IsNot Nothing Then
            Array.Clear(adoc, 0, adoc.Length)

        End If

        For Each file As String In mlstFiles.CheckedItems

            Try
                GetVDocument(vb.Left(file, InStr(file, ".") + 7))
            Catch

            End Try

        Next

        lblStatus.Text = "..."
        lblStatus2.Text = adoc.Length & " Files have been added to the Array..."

    End Sub
On the first run... 12 files have been added to the library
On the second run (same data)... 24 files have been added to the library

If ther array were clearing out, the second run would read again, 12 files have been added to the library.

And here's the sub for GetVDocument...
HTML Code:
    Private Sub GetVDocument(ByVal FileName As String)
        Dim objDocument As IPDMWDocument = conn.GetSpecificDocument(FileName)
        If objDocument IsNot Nothing Then
            Try
                Dim intResult As Integer = objDocument.Save(PDMFolder)
                If intResult = 0 Then
                    ' Local copy passed

                Else
                    ' Local copy failed

                End If

            Catch
                lblStatus.Text = "..."
                lblStatus2.Text = "Sorry, SolidWorks Crashed - Please re-run..."

            End Try

            Dim objList As PDMWLinks = objDocument.References
            For Each objLink As PDMWLink In objList
                Dim objLinkDoc As PDMWDocument = objLink.Document
                lblStatus2.Text = "Processing " & objLinkDoc.Name & "..."
                GetVDocument(objLinkDoc.Name)
                Dim objDocList As PDMWLinks = objLinkDoc.WhereUsed
                For Each objDocLink As PDMWLink In objDocList
                    Try
                        If GetRoot(objDocLink.Document.Name) = GetRoot(objLinkDoc.Name) Then
                            AddElementToStringArray(objDocLink.Document.Name)

                        End If

                    Catch

                    End Try

                Next

            Next

        Else
            ' No PDM file")

        End If

    End Sub
Going crazy with this, thanks again!
Reply With Quote
  #6  
Old 10-17-2011, 04:15 PM
PlausiblyDamp's Avatar
PlausiblyDampClearing Arrays PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

Nothing in the posted code shows anything being added or extending the adoc array though, I am guessing you need to show us the contents of the AddElementToStringArray method.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #7  
Old 10-17-2011, 04:30 PM
sanderson sanderson is offline
Junior Contributor
 
Join Date: Feb 2006
Location: Minnesota, USA
Posts: 217
Default

Here you go...

HTML Code:
    Public Sub AddElementToStringArray(ByVal stringToAdd As String)
        ReDim Preserve adoc(adocElements)
        adoc(adocElements) = stringToAdd
        adocElements += 1

    End Sub
Thanks again!
Reply With Quote
  #8  
Old 10-18-2011, 07:28 AM
sanderson sanderson is offline
Junior Contributor
 
Join Date: Feb 2006
Location: Minnesota, USA
Posts: 217
Default Thank you.

I figured it out. I added this in a separate sub to call, abd everything is good.
HTML Code:
    Public Sub DeleteEntireStringArray()
        If adoc IsNot Nothing Then
            Array.Clear(adoc, 0, adocElements)
            adocElements = 0
            ReDim Preserve adoc(adocElements)

        End If

    End Sub
Thanks for your help.
Reply With Quote
  #9  
Old 10-18-2011, 07:55 AM
PlausiblyDamp's Avatar
PlausiblyDampClearing Arrays PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

You are probably better of using a List(Of String) rather than an array then, Lists are designed to grow and shrink as needed without the need to constantly Redim them.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Clearing Arrays
Clearing Arrays
Clearing Arrays Clearing Arrays
Clearing Arrays
Clearing Arrays
Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays Clearing Arrays
Clearing Arrays
Clearing Arrays
 
Clearing Arrays
Clearing Arrays
 
-->