Important alert: (current site time 7/15/2013 11:01:20 PM EDT)
 

VB icon

DynamicArray Class

Email
Submitted on: 3/15/2002 5:09:26 AM
By: Miroslav Hibler 
Level: Intermediate
User Rating: Unrated
Compatibility: ASP (Active Server Pages), VbScript (browser/client side)
Views: 10815
 
     Building a dynamic array of data, that is, an array that "knows" how to Add, Insert or Delete its element. This is a slightly modified (to accept objects as array elements) code from http://www.4guysfromrolla.com. Just use methods: .Add(data), .Insert(data) or .Delete(dataIndex)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
'**************************************
' Name: DynamicArray Class
' Description:Building a dynamic array of data, that is, an array that "knows" how to Add, Insert or Delete its element. This is a slightly modified (to accept objects as array elements) code from http://www.4guysfromrolla.com. Just use methods: .Add(data), .Insert(data) or .Delete(dataIndex)
' By: Miroslav Hibler
'
' Inputs:See code.
'
' Returns:An Array Object.
'
' Assumes:How to use classes in ASP.
'
'This code is copyrighted and has' limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=7341&lngWId=4'for details.'**************************************

<%
'**************************************
' DynamicArray Class
'
' Author:	M. Hibler, July 5, 2001.
' Last change:	M. Hibler, March 15, 2002.
' Purpose:	Dynamically manipulate arrays of data
' Inputs:	See below
' Source:http://www.4guysfromrolla.com/webtech/code/array.class.asp.html
'**************************************
Class DynamicArray
'************* Properties *************
Private aData
'**************************************
'*********** Event Handlers ***********
Private Sub Class_Initialize()
	aData = Array(0)
End Sub
Private Sub Class_Terminate()
	ReDim aData(0)
End Sub
'**************************************
'************ Property Get ************
Public Property Get Data(iPos)
	If iPos < LBound(aData) or iPos > UBound(aData) then
		Exit Property 'Invalid range
	End If
	If IsObject(aData(iPost)) Then
		Set Data = aData(iPos)
	Else
		Data = aData(iPos)
	End If
End Property
Public Property Get DataArray()
	DataArray = aData
End Property
Public Property Get StartIndex()
	StartIndex = LBound(aData)
End Property
Public Property Get StopIndex()
	StopIndex = UBound(aData)
End Property
Public Property Get Count()
	Count = UBound(aData) - LBound(aData)
End Property
'**************************************
'************ Property Let ************
Public Property Let Data(iPos, varValue)
	'Make sure iPos >= LBound(aData)
	If iPos < LBound(aData) Then Exit Property
	If iPos > UBound(aData) then
		'We need to resize the array
		Redim Preserve aData(iPos)
	End If
	If IsObject(varValue) Then
		Set aData(iPos) = varValue
	Else
		aData(iPos) = varValue
	End If
End Property
'**************************************
'************** Methods ***************
Public Function Add(varValue)
	If IsObject(varValue) Then
		Set aData(UBound(aData)) = varValue
	Else
		aData(UBound(aData)) = varValue
	End If
	Add = UBound(aData)
	Redim Preserve aData(UBound(aData) + 1)
End Function
Public Function Insert(iPos, varValue)
	'Make sure iPos is within acceptable ranges
	If iPos > UBound(aData) then
	Call Add(varValue)
	Exit Function
	ElseIf iPos >= LBound(aData) then
		Dim iLoop
		For iLoop = UBound(aData) to iPos + 1 Step -1
			If IsObject(aData(iLoop - 1)) Then
				Set aData(iLoop) = aData(iLoop - 1)
			Else
				aData(iLoop) = aData(iLoop - 1)
			End If
		Next
		If IsObject(varValue) Then
			Set aData(iPos) = varValue
		Else
			aData(iPos) = varValue
		End If
		Insert = UBound(aData)
		Redim Preserve aData(UBound(aData) + 1)
	End If
End Function
Public Sub Delete(iPos)
	'Make sure iPos is within acceptable ranges
	If iPos < LBound(aData) or iPos > UBound(aData) then
		Exit Sub 'Invalid range
	End If
	Dim iLoop
	For iLoop = iPos to UBound(aData) - 1
		If IsObject(aData(iLoop + 1)) Then
			Set aData(iLoop) = aData(iLoop + 1)
		Else
			aData(iLoop) = aData(iLoop + 1)
		End If
	Next
	Redim Preserve aData(UBound(aData) - 1)
End Sub
Public Sub Flush()
	ReDim aData(0)
End Sub
'**************************************
	
End Class
%>


Other 2 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

4/28/2006 2:37:39 PMAK

Works great but I noticed that when you call objDynArray.Data(i) to display the array contents each item is appended by its index in the array for example: Hello1, World2.... but the data inserted was Hello, World... I ve messed around with the code to try and solve the problem but have been unsuccesful.. I would appreciate any advice or help..
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.