Posted by Stier, Robert on 1999-08-13
Date: Tue, 10 Aug 1999 21:40:04 +0500 From: "Muhammad Ali Shah" <mashah@xxxxx.xxx.xx> Subject: Visual Basic Strings Hello All, Since there are no posts for the last few days, I have another question for VB programmers. How exactly a string is handled in Visual Basic? In more appropriate terms, I would like to know: 1. In Pascal we have fixed length strings that have length stored in the first byte. In C language, the strings are null terminated. What exactly is a string in VB? A data type consisting of a sequence of contiguous characters that represent the characters themselves rather than their numeric values. A String can include letters, numbers, spaces, and punctuation. The String data type can store fixed-length strings ranging in length from 0 to approximately 63K characters and dynamic strings ranging in length from 0 to approximately 2 billion characters. The dollar sign ($) type-declaration character represents a String in Visual Basic. 2. What is the difference between "Dim aStr as string" and "Dim aStr as string * 20"? Dim aStr as String Defines a Variable Length String Dim aStr as String * 20 Defines a string with a maximum length of 20 3. Why can't I read/write data to a file with the following structure TYPE employee dim name as string dim age as integer END TYPE Type Statement Used at module level <JavaScript:alink_4.Click()> to define a user-defined data type <JavaScript:alink_5.Click()> containing one or more elements. Syntax [Private | Public] Type varname elementname [([subscripts])] As type [elementname [([subscripts])] As type] . . . End Type The Type statement syntax has these parts: Part Description Public Optional. Used to declare user-defined types <JavaScript:alink_6.Click()> that are available to all procedures <JavaScript:alink_7.Click()> in all modules <JavaScript:alink_8.Click()> in all projects <JavaScript:alink_9.Click()> . Private Optional. Used to declare user-defined types that are available only within the module where the declaration <JavaScript:alink_10.Click()> is made. Varname Required. Name of the user-defined type; follows standard variable <JavaScript:alink_11.Click()> naming conventions. Elementname Required. Name of an element of the user-defined type. Element names also follow standard variable naming conventions, except that keyword <JavaScript:alink_12.Click()> s can be used. Subscripts When not explicitly stated in lower, the lower bound of an array is controlled by the Option Base statement. The lower bound is zero if no Option Base statement is present. Type Required. Data type of the element; may be Byte <JavaScript:alink_13.Click()> , Boolean <JavaScript:alink_14.Click()> , Integer <JavaScript:alink_15.Click()> , Long <JavaScript:alink_16.Click()> , Currency <JavaScript:alink_17.Click()> , Single <JavaScript:alink_18.Click()> , Double <JavaScript:alink_19.Click()> , Decimal <JavaScript:alink_20.Click()> (not currently supported), Date <JavaScript:alink_21.Click()> , String <JavaScript:alink_22.Click()> (for variable-length strings), String * length (for fixed-length strings), Object <JavaScript:alink_23.Click()> , Variant <JavaScript:alink_24.Click()> , another user-defined type, or an object type <JavaScript:alink_25.Click()> . Remarks The Type statement can be used only at module level. Once you have declared a user-defined type using the Type statement, you can declare a variable of that type anywhere within the scope <JavaScript:alink_26.Click()> of the declaration. Use Dim, Private, Public, ReDim, or Static to declare a variable of a user-defined type. In standard modules <JavaScript:alink_27.Click()> and class modules <JavaScript:alink_28.Click()> , user-defined types are public by default. This visibility can be changed using the Private keyword. Line numbers <JavaScript:alink_29.Click()> and line labels <JavaScript:alink_30.Click()> aren't allowed in Type...End Type blocks. User-defined types are often used with data records, which frequently consist of a number of related elements of different data types. The following example shows the use of fixed-size arrays in a user-defined type: Type StateData CityCode (1 To 100) As Integer ' Declare a static array. County As String * 30 End Type Dim Washington(1 To 100) As StateData In the preceding example, StateData includes the CityCode static array, and the record Washington has the same structure as StateData. When you declare a fixed-size array within a user-defined type, its dimensions must be declared with numeric literals or constants <JavaScript:alink_31.Click()> rather than variables. The setting of the Option Base statement determines the lower bound for arrays. String Manipulation Keyword Summary Action Keywords Compare two strings. StrComp <vafctstrcomp.htm> Convert strings. StrConv <vafctstrconv.htm> Convert to lowercase or uppercase. Format <vafctformat.htm> , Lcase <vafctlcase.htm> , Ucase <vafctucase.htm> Create string of repeating character. Space <vafctspace.htm> , String <vafctstring.htm> Find length of a string. Len <vafctlen.htm> Format a string. Format <vafctformat.htm> Justify a string. LSet <vastmlset.htm> , Rset <vastmrset.htm> Manipulate strings. InStr <vafctinstr.htm> , Left <vafctleft.htm> , LTrim <vafctltrim.htm> , Mid <vafctmid.htm> , Right <vafctright.htm> , RTrim <JavaScript:alink_2.Click()> , Trim <vafctltrim.htm> Set string comparison rules. Option Compare <vastmoptioncompare.htm> Work with ASCII and ANSI values. Asc <vafctasc.htm> , Chr <vafctchr.htm> If however, I use fiexed length strings, the read/write functions do exactly what they are intended. Hope this helps Sponsored by Wrox Press - www.wrox.com. Programmer to Programmer (TM)
Previous post | Next post | Timeline | Home