Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Referencing Contents of Array in Other Areas

Status
Not open for further replies.

dataman86

Technical User
Oct 11, 2008
104
US
I am very new to VB and trying to understand Array's. I am having trouble referencing the contents of the Array in other areas of my application and I ma getting object Reference Errors and the contents of the Array appear as Nothing and I get a null reference error.

The Array is being loaded from the Sequel Server and I am adding two columns to the Array to add data. I am getting all of my Array in the Return Array, however I acm unable to reference contents of the Array and write out the contents. I need to reference the contents.

The Array is called MyArray, and I need to reference each part of the Array as MyArray(irow,1) for example. The irow will always vary but I will always have 10 columns.

How can I reference the Array in other parts of the application? I tried dimensioning the array at the Class Level, but the array shows nothing in it.

My Code here works and the Array Returns with all of my data that I need all ten columns, but I can't reference the pieces of it elsewhere in the application in another button event in a Private Sub or Public Sub.

The Coding as a Private Function:

Private Function BuildArray() As Array
Dim i As Int32 = CType(Me.Name.Substring(1, 1), Int32)
Dim iFinish, iPage, iRow, iStart As Int32

Dim MyArray(,) As String = CType(_sData, String(,))

For iPage = i To 1 Step -1 ' start with the current and back thru the forms to 1
Dim frm As String = "f" & iPage

If iPage = i Then ' last page has variable number of rows
iStart = _sta
iFinish = _fin
Else
iStart = (12 * iPage) - 12 ' not last page has fixed number of rows
iFinish = iStart + 11
End If
If MyArray(iRow, 8) Is Nothing Then
MyArray(iRow, 8) = 0
End If
For iRow = iStart To iFinish ' put data from the column nine and ten textboxes into the array
'MyArray(iRow, 0) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "0").Text.ToString
'MyArray(iRow, 1) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "1").Text.ToString
'MyArray(iRow, 2) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "2").Text.ToString
'MyArray(iRow, 3) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "3").Text.ToString
'MyArray(iRow, 4) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "4").Text.ToString
'MyArray(iRow, 5) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "5").Text.ToString
'MyArray(iRow, 6) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "6").Text.ToString
'MyArray(iRow, 7) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "7").Text.ToString

'MyArray(iRow, 8) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "8").Text.ToString

MyArray(iRow, 9) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "9").Text.ToString

MyArray(iRow, 10) = My.Application.OpenForms.Item(frm).Controls("TextBox" & iRow & "10").Text.ToString


Next
Next


Return MyArray ' return the array MyArray to the routine that called it


Any help is truly appreciated!

DataMan86

End Function
 
There are a number of ways but the easiest would be to declare the array in a Module as in

[tt]
Module Module1

Public MyArray1(9) As Integer
Public MyArray2() As Integer

End Module
[/tt]
 
I have a VB Application that is loading an Array with Numbers from the Sequel Server that have numbers 315054 no cents and leaving the .00 off even numbers. I need to write out the number to a textfile that has 14 zero's to the left removing the decimal point from the number.

The Number should format and be written out like this:

00000031505400 in the text file.


My coding does this as long as the even number has a decimal point .00 but if the number comes in as 315054 into my array my coding gives an Augument error with the coding below.

The Coding:

SCreditTextBox1 = Microsoft.VisualBasic.Right("00000000000000" & Microsoft.VisualBasic.Left(_sData(Irow, 7), _sData(Irow, 7, ".") - 1) & _
Microsoft.VisualBasic.Right(_sData(Irow, 7), 2), 14)





Is there a way to check for no decimal point and then place the number with no .00 as 00000031505400?

The accounting people are entering the numbers into the server this way incorrectly. The .00 is suppose to be entered with it, but not all even numbers are entered this way without .00, just a few, but I need to check the number to see if there are decimals and without decimals do something.

Could my coding be re-written to account for not having the .00 and to format the number correctly to avoid the augument error?

I need for any number to format with:

$3,000.00 would appear in server as 3000 or 3000.00 and should write out like this below: I need to check for decimals and without decimals and writeout the format like this below:

00000000300000.

DataMan86
 
A couple of things to note:
The accounting people are entering numbers into the server this way incorrectly
I disagree. The program they are using is writing the numbers into the server incorrectly. Users shouldn't be required to enter numeric values in such a rigid format. Whole numbers shouldn't need a decimal point typed in; even values such as "2.1" should be correctly identified as two dollars and 10 cents. You may want to look into sprucing up the program they use for data entry to correct the values before they get into the database.

Here's an example of a function that will convert your data from a raw string with or without decimal points to a zero padded string:
Code:
    Private Function ZeroPaddedStringFromMoneyString(ByVal moneyString As String) As String
        Dim valueAsDecimal As Decimal
        Dim valueAsInteger As Integer
        Dim valueAsString As String

        If (Decimal.TryParse(moneyString, valueAsDecimal)) Then
            valueAsDecimal = Decimal.Round(valueAsDecimal, 2) * 100
            valueAsInteger = Convert.ToInt32(valueAsDecimal)
            valueAsString = valueAsInteger.ToString.PadLeft(14, "0"c)
        Else
            ' Invalid number - default to all zeros
            valueAsString = "".ToString.PadLeft(14, "0"c)
        End If

        Return valueAsString
    End Function

BTW - It's probably a good idea to start a new thread when the subject takes a right turn like this.
 

Just out of curiosity, what is this form doing? It sounds like you've basically created a grid out of textboxes and are filling them from a 2 dimensional array that's being used as a table. It sounds like something that might be more easily managed through ADO.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top