Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘array’

BUG: MSChart Control Can’t Plot Data Points >15 Decimal Places

Symptoms
When the Chart Control is assigned an array of data points containing 16 ormore decimal places, those data points do not appear when the chart isupdated.
Resolution
There are two ways to deal with this issue:
Formatting the number to 15 or fewer decimal places.Using the SetData property on each data point.
Workaround 1Before setting the chart to the variant array, “truncate” the decimalvalues to 15 places or less as seen in the form_load event below:

Private Sub Form_Load()Dim data_pts(1 To 3, 1 To 2) As Variantdim i as integerdata_pts(1, 1) = “R1″data_pts(1, 2) = 5.48487730596136E-02data_pts(2, 1) = “R2″data_pts(2, 2) = 7.04216678154124E-02data_pts(3, 1) = “R3″data_pts(3, 2) = 3.10863837084563E-04For i = 1 To 3data_pts(i, 2) = CDbl(Format(data_pts(i, 2), “0.000000000000000″))’Truncate data_pts to 15 decimal places of precisionNextMSChart1 = data_ptsEnd Sub
Workaround 2Use the SetData method to add the data to the chart instead of setting thechart to the variant array. To use this workaround, rewrite the Form_Loadevent to look like this instead:

Private Sub Form_Load()With MSChart1.RowCount = 3.ColumnCount = 1.DataGrid.SetData 1, 1, 5.48487730596136E-02, False.DataGrid.SetData 2, 1, 7.04216678154124E-02, False.DataGrid.SetData 3, 1, 3.10863837084563E-04, FalseEnd WithEnd Sub

BUG: Error Occurs When Printing a Form Stored in an Array

Symptoms
An application error occurs when calling the Print method of a form objectcontained within a form array.
Resolution
To work around this problem, define a non-array form variable and set thevariable to reference the form array element that you want to manipulate.Once this variable has been initialized, you can safely call the Printmethod of the newly defined Form variable, as shown in the example below:

Private Sub Form_Load()Dim MyForm As FormDim fl(1) As Form2Set fl(0) = New Form2Set MyForm = fl(0)MyForm.Show’Show the Form2 formMyForm.Print ‘Print the form to the screenEnd Sub

PRB: Error When You Access an Array Field of a .NET Structure from COM

Symptoms
When you access an Array field of a structure that is defined in .NET from inside a COM DLL, you may receive the following error message:

“An unhandled exception of type ‘System.ArgumentException’ occurred in ProjectName”
Additional information: Wrong number of arguments or invalid property assignment.
Resolution
Because of the late-bound method that is used in this case, the Visual Basic 6.0 runtime cannot get the type information for the Array field that you are attempting to access.

How Visual Basic 4.0 Calls C and Fortran DLLs

Symptoms
This sample demonstrates how a Visual Basic 4.0 application calls Fortranand Visual C DLL’s. Specifically, it shows how to pass fixed lengthstrings to and from those DLL’s.
Resolution
The following file is available for download from the Microsoft Download Center:
Vbstring.exe(http://download.microsoft.com/download/vb40ent/sample40/1/w9xnt4/en-us/vbstring.exe)Release Date: Jan-01-1997
For additional information about how to download Microsoft Support files, click the following article number to view the article in the Microsoft Knowledge Base:
119591?(http://support.microsoft.com/kb/119591/EN-US/)How to Obtain Microsoft Support Files from Online ServicesMicrosoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help to prevent any unauthorized changes to the file.
Visual Basic 4.0 replaced the string management system used by previousversions of Visual Basic with a more robust string management system.Visual Basic 4.0 relies on the OLE automation data types, such as BSTR andsafe arrays, to manage string usage. However, passing arrays of numerictypes, integers, or reals were not changed. For instance, before passing anarray of strings to a DLL, the strings must be converted to an array ofbytes, and then converted back to strings on return from the DLL. Please,refer to VB4DLL.TXT that ships with Visual Basic 4.0 for more information.
All new C DLL’s should use the syntax described in the VB4DLL.TXT filebecause the API calls that are employed allow for improved error handling.The programmer can do something in the event of an error, such as display amessage box and abort the operation.
Please note that the new Fortran DLL’s cannot use the syntax described inVB4DLL.TXT because they have no direct access to the correct APIfunctions.
The sample also demonstrates different methods you can use to resolve thefunction symbol within the DLL. A mismatch in the function symbol resultsin a Visual Basic runtime error, “Specified DLL function not found (Error453).” The sample includes two methods to resolve C function symbols andthree methods to resolve Fortran function symbols.
Each source file contains more information pertinent to that language.
This sample also enables you to choose whether the data is passed to eitherthe C or Fortran DLL.
This sample shows how to pass the following:
Two dimensional array of 4-byte integers.Two dimensional array of 8-byte floating point numbers.String.One dimensional array of strings.Two dimensional array of strings.One dimensional array of 4-byte integers, a one dimensional array of8-byte floating point numbers, and a one dimensional array of strings.
Sample Files

FileNameDescription——————————————————-cdll.cC source file to build 32-bit DLLfordll.forFortran source file for FPS NT 1.0 referencefordll32.f90Fortran source file for the 32-bit DLLvbstring.vbpVisual Basic 4.0 project filevbstring.frmVisual Basic form including event handlers
Building DLLsThis sample assumes you are using the following development environments:
Microsoft Visual C++, version 4.0Microsoft FORTRAN PowerStation for Windows 95 and Windows NT, version 4.0You will need to setup the environment before running BUILDDLL.BAT.At an MS-DOS command-line prompt type:

C:\MSDEV\BIN\VCVARS32.BAT Build the C DLL. At the command-line prompt type:

BUILDDLL C Build the Fortran DLL. At the same prompt type:

BUILDDLL Fortran Start Visual Basic 4.0 and open the project file VBSTRING.VBP.Run the Visual Basic application by creating an executable file (.exe)or running the program from within Visual Basic.IMPORTANT: The DLLs must be in the \WINDOWS\SYSTEM directory, the directory containing the Visual Basic executable file, or one of the environment file paths.

Notes on Arrays in Different LanguagesGiven an upper bound of n, note that in Visual Basic all of the arrayranges are specified as “1 to n”. When a lower bound is not specified,Visual Basic, by default, assumes 0<=index<=n where “n” is inclusive. C array subscripts are assumed to be in the range 0<=index<=n-1. Fortran array ranges are 1<=index<=n. You need to be careful when you assign the index ranges, and make sure that they match. If you exceed them, you will probably get an Access Violation, an Unhandled Exception, or a random run-time error in your Visual Basic Application.

LanguageDeclarationRange# Items in Array—————————————-BasicDim X(10) As Integer0<=index<=1011Basic*Dim X(1 to 10) As Integer1<=index<=1010Cint X[10];0<=index<=910Fortraninteger X(10)1<=index<=1010Fortran*integer X(0:9)0<=index<=910* With lower bound specified. C is always zero-based.

FIX: “Invalid Procedure Call” Error with Preserve Keyword

Symptoms
The following error occurs if a variant set equal to an Array() function isredimensioned using the Preserve keyword:

Run-time error ‘5′ “Invalid Procedure Call or Argument”NOTE: This behavior occurs only under the following two conditions:If Microsoft Data Access Components (MDAC) 2.0 or Visual Studio 6.0is installed on the computer.
-and-If the array has not been initialized.
Resolution
This error occurs with the newer version of the OLE automation core file,(version 2.304261). The Oleaut32.dll file is installed in your systemdirectory when you install MDAC 2.0 or Visual Studio 6.0 products.

“Can’t create a child list” error message occurs when you open a form in Visual Studio .NET

Symptoms
When you delete the table that is bound to a control form of the DataSet Schema and then save the project in Microsoft Visual Studio .NET, you may receive the following error message when you open the form in Design view:

Can’t create a child list for field TableName.
The controls that are added before the data bound control are deleted from the form by Visual Studio .NET, and you receive the following error message in the data bound control:

System.ArgumentException: Can’t create a child list for field TableName.
Resolution
In Windows Form Designer-generated code, the controls are displayed on the form when the Control array is added to the Controls collection of the form. The last control that is added to the form has the index 0 in the Control array, the first control has the highest index, and then the other controls. When the XML Schema is changed, the data bound control raises an error and the controls that were added to the form before the data bound control are removed from the array because the controls are after the data bound control in the Control array. Therefore, the controls are not added to the Controls collection of the form and are not displayed.