Podobne
- Strona startowa
- (ebook pdf) Teach Yourself Database Programming with Visual C in 21 days
- (ebook pdf) Teach Yourself SQL in 21 Days
- Linux. .Mandrake.10.Podręcznik.Użytkownika.[eBook.PL] (3)
- (ebook PDF Philosophy) Russell, Bertrand Political Ideals
- (eBook) James, William The Principles of Psychology Vol. I
- [eBook] DirectX 3D Graphics Programming Bible
- (ebook PDF) Schreiner Object oriented Programming With A
- Tolkien J R R Niedokonczone opowiesci t I
- Fowles John Mag (2)
- Narcyza Żmichowska Poganka
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- nea111.xlx.pl
Cytat
Do celu tam się wysiada. Lec Stanisław Jerzy (pierw. de Tusch-Letz, 1909-1966)
A bogowie grają w kości i nie pytają wcale czy chcesz przyłączyć się do gry (. . . ) Bogowie kpią sobie z twojego poukładanego życia (. . . ) nie przejmują się zbytnio ani naszymi planami na przyszłość ani oczekiwaniami. Gdzieś we wszechświecie rzucają kości i przypadkiem wypada twoja kolej. I odtąd zwyciężyć lub przegrać - to tylko kwestia szczęścia. Borys Pasternak
Idąc po kurzych jajach nie podskakuj. Przysłowie szkockie
I Herkules nie poradzi przeciwko wielu.
Dialog półinteligentów równa się monologowi ćwierćinteligenta. Stanisław Jerzy Lec (pierw. de Tusch - Letz, 1909-1966)
[ Pobierz całość w formacie PDF ]
.All rights reserved.Array VariablesPrivate Sub Form_Load()ReDim sValues(0)End SubPrivate Sub Command1_Click()sValues(UBound(sValues)) = txtTextBox.TextReDim Preserve sValues(UBound(sValues) + 1)End SubNote that using the UBound function on an uninitialized array generates aSubscript Out of Range error; therefore, the Form_Load event is used to redimen-sion the array to 0 to insure that the array has one element.Setting the lower boundaryBy default, VBA arrays start with element 0.However, you can change this on aper-module basis by using the Option Base statement in the declarations sectionof your module.For example:Option Base 1generates arrays starting with element 1.The Option Base statement must beused in the module before any variable declarations.Another method used to set the lower boundary is to specify both the lower andupper boundaries when the array is dimensioned, as the following syntax shows:Dim arrayname(lowerboundary To upperboundary) As datatypeMultidimensional ArraysThe arrays we have looked at so far are single-dimension arrays; they hold oneelement of data in each index location, which is fine for most needs.However,sometimes you need a full set of data for each element; this is called a multidi-mensional array.In a single-dimension array, the data held within has no structure; it s accessedsequentially, and there is one piece of data for each element.When you need tostore more than this one piece of data for each logical element, you should useeither a multidimensional array or a user-defined type (which is discussed in thenext section).A multidimensional array allows you to have a separate array of data for eachelement of your array.Therefore, each element of the array in turn contains anarray.The structure of a multidimensional array resembles that of a database table.The rows (or records) of the table represent the first dimension, and the columns(or fields) represent by the second dimension, as the following table illustrates.Field 1 Field 2 Field 3Record 1 Array Element (0,0) Array Element (0,2)Record 2 Array Element(1,1)Array Variables 43VB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.Array VariablesField 1 Field 2 Field 3Record 3Record 4 Array Element (3,0) Array Element (3,2)Multidimensional arrays can contain up to 60 dimensions, though it sextremely rare to use more than two or three dimensions.To define a multidimensional array, use the following syntax:Dim arrayname(upperboundDimension1, _upperboundDimension2,.) As DatatypeAs with single-dimension arrays, you can also specify the lower boundary withinthe array definition, and you can specify different lower boundaries for eachelement.For example:Private myArray(1 To 20, 0 To 50) As StringDynamic multidimensional arraysLike single-dimension arrays, multidimensional arrays can be dynamic, and therules for redimensioning them are similar.But since you have more than onedimension to think about, you have to take care how you use and redimensionyour array.The rules for using a dynamic multidimensional array are:" You can ReDim a multidimensional array to change both the number ofdimensions and the size of each dimension.This is illustrated by the follow-ing, where the myArray dynamic array is originally defined as a two-dimen-sional array with 11 elements in the first dimension and 6 in the second, but isthen redimensioned into a three-dimensional array with 5 elements in the firstdimension, 11 in the second, and 3 in the third.Private myArray() As IntegerPrivate Sub cmdButtonOne_OnClickReDim myArray(10,5)End SubPrivate Sub cmdButtonTwo_OnClickReDim myArray(4,10,2)End Sub" If you use the Preserve keyword, you can only resize the last array dimen-sion, and you can t change the number of dimensions at all.For example:.ReDim myArray(10,5,2).ReDim Preserve myArray(10,5,4).44 Chapter 3 VBA Variables and Data TypesVB & VBA in a Nutshell: The Language, eMatter EditionCopyright © 2000 O Reilly & Associates, Inc.All rights reserved.User-Defined TypesUsing UBound and LBound with multidimensional arraysAs you saw earlier, the UBound function returns the highest subscript (elementnumber) in an array that is, its Upper Boundary.You can also use UBound witha multidimensional array, except that to find the largest element of a multidimen-sional array, you need to also specify a dimension:largestElement = UBound(arrayname, dimensionNo)The same is true of the LBound function:smallestElement = LBound(arrayname, dimensionNo)User-Defined TypesOne major limitation of the multidimensional array is that all the dimensionswithin the array must be of the same data type.The user-defined type (UDT),which combines multiple data types into a single new data type, overcomes thislimitation.Since VB 4.0, UDTs have gone out of fashion somewhat, this fall from favorhaving resulted from the introduction of the Collection object, which on thesurface operates like an infinitely flexible UDT.However, VB6 has given thehumble UDT a new lease on life by allowing UDTs to be passed as propertyvalues and to be used in public function declarations.This is good news, as theUDT is far more efficient than a Collection object.So what is a user-defined type? Simply put, it s a pseudo-data type constructedfrom other data types.One of its common applications is the replication of a datarecord in memory.For example, let s say you want to create a local array to holdthe data of your customer records.Because each of the fields within the record isof a different data type, a multidimensional array can t be used.A UDT, on theother hand, is ideal in this situation.The following snippet defines a simple UDT:Private Type custRecordcustAccNo As LongcustName As StringRenewalDate As DateEnd TypePrivate custArray(10) As custRecordThe last line of code creates a local array of the UDT
[ Pobierz całość w formacie PDF ]