• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 469
  • Last Modified:

How to clear and reuse an Array

How might I clear and reuse an array?  Something like this (which doesn't work).

dim ar() as string = {"XYZ","ABC","123"}
Do_A(ar)
ar.clear                                   << doesn't work
ar() = {"PQR","HJK","435"}        << doesn't work
Do_B(ar)

I'm using these arrays as low-syntax, easy to maintain temporary value holders, for use in building an in-memory data structure.
I'd like to avoid having to declare a new array every time I want to use it as a temporary value holder.

Any suggestions would be appreciated.

Thanks!
0
codequest
Asked:
codequest
  • 3
  • 3
2 Solutions
 
hatem72Commented:


ar = Nothing ' Will set the refrence of the array to nothing

OR

ReDim ar(0)
0
 
hatem72Commented:


for dealing with data try this :

    Public Structure employees

        Dim EmpID As Integer
        Dim EmpName As String
        Dim EmpAdd As String

    End Structure

    Dim MyArray(5) As employees
...

        MyArray(1).EmpAdd = EmpAddVar
        MyArray(1).EmpID = EmpIDVar
        MyArray(1).EmpName = EmpNameVar

if isnt clear i am here
0
 
codequestAuthor Commented:
Thanks for the input.   I'll check out the array reset's a little later and let you know if I can get them to work.
Thanks for the info and example on structure.  That's very helpful...I hadn't figure those out yet.
Question:  where do I declare structures?  At the top of classes?  Inside methods?
Thanks!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
codequestAuthor Commented:
Also, if I use a structure in one class, how do I make it available in another class...do I have to redefine it there?
Thanks!
0
 
hatem72Commented:


u can Declare structure like any variables anywhere (Not in Procedure level )

http://msdn2.microsoft.com/en-us/library/k69kzbs1(VS.80).aspx

my Example :

Imports System.Windows

Public Class MyClassName

    Public Structure employees

        Dim EmpID As Integer
        Dim EmpName As String
        Dim EmpAdd As String

    End Structure
    Dim MyArray(5) As employees

    Public Sub PopData()
       
        MyArray(1).EmpAdd = EmpAddVar
        MyArray(1).EmpID = EmpIDVar
        MyArray(1).EmpName = EmpNameVar

    End Sub

...
...
...

End Class

'  Another Class to write Data ( Just Example )

Public Class MyClassName

   Public Sub GetData()
       
        EmpAddVar=MyArray(1).EmpAdd  
        EmpIDVar=MyArray(1).EmpID
        EmpNameVar=MyArray(1).EmpName

    End Sub


End Class
0
 
PockyMasterCommented:
ReDim is oldschool VB stuff.

They've invented Array now.

'init some array
 Dim ar() As String = {"123", "12312", "123"}

'e.g. clear all the items
Array.Clear(ar, 0, ar.Length)
'e.g. resize it
 Array.Resize(ar, 2)
'e.g. copy another array into your array
   Array.Copy(New String() {"12312", "123123"}, ar, 2)

there's more where that came from...

good luck
0
 
codequestAuthor Commented:
PockyMaster:  good stuff, very handy, thanks!

hatem72:  please collect extra points at a question I'll enter with
Q_21788716.html#16289923 in the title...just make any enty and I'll accept it as the answer

codequest
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now