Need some array help.

  • Thread starter Thread starter James
  • Start date Start date
J

James

Is this possible? I want to pass an array into a function that contains
txtBox.Text properties... I was thinking something like this, but I
know it won't work

Dim vendorFields(9) As String

vendorFields(0) = "txtVendorName.Text"
vendorFields(1) = "txtVendorStreetAddress.Text"
vendorFields(2) = "txtVendorCity.Text"
....


vendorFields(2) = "111 Star Ave"

but really i want it to translate as

txtVendorStreetAddress.Text = "111 Star Ave"


I know my logic is messed up, is this possible, and if so how coud I do
it? Thanks
 
James,

Why not an array of textboxes

dim myTextBoxes() as Textbox = {txtVendorName,txtVendorStreet,etc}
venodorfields(2).Text = "111StarAvenue"

I hope this helps,

Cor
 
hmmm sounds like a good solution, but i also have one combo box... and
ideas how i could incorporate that into the array?

thanks.
 
James,

That is so nice, you can use it with a combobox too.
However than you only need to change the array as

dim ctr() as controls = ...........................

Text is a property from controls and therefore as well in the textbox as in
the combobox. You don't have to do anything more, text is inherited.

I hope this helps,

Cor
 
Cor,

Well, I thought I was done... but when my code gets to this line:

ctrTextFieldNames(intFieldsRead).Text =
Microsoft.VisualBasic.Left(strReadVendorFile, intFieldValue)

it triggers this error:
An unhandled exception of type 'System.NullReferenceException' occurred
in Appointment.exe

Additional information: Object reference not set to an instance of an
object.


Here's the code so you can get a feel of that line in context. Thanks
for your help.


Dim ctrvendorFields() As Control = {txtVendorName,
txtVendorStreetAddress, txtVendorCity, txtVendorState, txtVendorZip,
txtVendorPhone, txtVendorCOutstandingBill, txtVendorSalesRepName,
txtVendorComments, cboVendorPerferredPmtPeriod}


Private Sub fillTextFields(ByVal ctrTextFieldNames() As Control)
Dim intNumOfFields As Integer = 0
Dim intFieldsRead As Integer = 0
Dim intFieldValue As Integer
Dim strFieldValue As String


strReadVendorFile = VendorFile.ReadLine()
'Store the number of records as an integer value
intNumOfFields = ctrTextFieldNames.Length

'loop to fill all fields
Do Until intFieldsRead = intNumOfFields + 1
'this is a comma delimited text file we are reading from
'find the length to the first comma
intFieldValue = InStr(strReadVendorFile, ",")
'based on the position of the first commma, write the value
on the left
'side of the comma to the .Text property of the record in
the Control Array
ctrTextFieldNames(intFieldsRead).Text =
Microsoft.VisualBasic.Left(strReadVendorFile, intFieldValue)
'redefine the string so it only contains the remaining
comma delimited fields
'figure out a way to have a flexible value (ie the 100
needs to change)
strReadVendorFile =
Microsoft.VisualBasic.Right(strFieldValue, 100)
intFieldsRead += 1
Loop


End Sub
 
James wrote:
ctrTextFieldNames(intFieldsRead).Text =
Microsoft.VisualBasic.Left(strReadVendorFile, intFieldValue)

it triggers this error:
An unhandled exception of type 'System.NullReferenceException' occurred
in Appointment.exe

intNumOfFields = ctrTextFieldNames.Length

'loop to fill all fields
Do Until intFieldsRead = intNumOfFields + 1
<snip>

I guess the previous line shoud be

Do Until intFieldsRead = intNumOfFields

HTH.

Regards,

Branco.
 
yeah... but it's still erroring out at that outher spot, i figured what
you just told me needed to be changed, i just wasn't sure yet.
 
Sorry, no response here lately, I still am searching for an answer to
my error. Any ideas? Thanks
 
James said:
Sorry, no response here lately, I still am searching for an answer to
my error. Any ideas? Thanks

Well, I guess your code need some fixes... ;-)

The internal loop can be much simpler:

Private Sub fillTextFields(ByVal TextControls() As Control)
'Populates the controls in TextControls with the fields read from the
'curretn line of the vendor file

'Splits the comma separated items from the line read
'from VendorFile
Dim Texts() As String = VendorFile.ReadLine().Split(","c)

'iterates by each field and saves it on the corresponding control
For Field As Integer = 0 To _
System.Math.Min(Texts.Length, TextControls.Length) - 1
TextControls(Field).Text = Texts(Field).Trim
Next
End Sub
From a previous post:

Private Sub fillTextFields(ByVal ctrTextFieldNames() As Control)
Dim intNumOfFields As Integer = 0
Dim intFieldsRead As Integer = 0
Dim intFieldValue As Integer
Dim strFieldValue As String


strReadVendorFile = VendorFile.ReadLine()

strReadVendorFile is a local string, why isn't it declared inside the
sub? Can it be you're not using "Option Explicit"? If so, you probably
mistyped a control name in your control array. Go activate Option
Explicit first, and *then* come back here... =)))
'Store the number of records as an integer value
intNumOfFields = ctrTextFieldNames.Length
'loop to fill all fields
Do Until intFieldsRead = intNumOfFields + 1

As I told you, this line must be

Do Until intFieldsRead = intNumOfFields
'this is a comma delimited text file we are reading from
'find the length to the first comma
intFieldValue = InStr(strReadVendorFile, ",")

I personally preffer the intrinsic methods unless a library one
provides anything extra. In this case, *IMHO*, you should use

intFiledValue = strReadVendorFile.IndexOf(","c)

instead of InStr... besides, intFieldValue should be named Pos,
Separator, Split, Comma etc...
'based on the position of the first commma, write the value
on the left
'side of the comma to the .Text property of the record in
the Control Array
ctrTextFieldNames(intFieldsRead).Text =
Microsoft.VisualBasic.Left(strReadVendorFile, intFieldValue)

The same goes for the previous line: you should use (IMHO):

ctrTextFieldNames(intFieldsRead).Text = _
strReadVendorFile.Substring(0, intFielValue - 1 ).Trim
'redefine the string so it only contains the remaining
comma delimited fields
'figure out a way to have a flexible value (ie the 100
needs to change)
strReadVendorFile =
Microsoft.VisualBasic.Right(strFieldValue, 100)

Oooops, what was that? You are trying to get the rightmost 100 chars
from strFieldValue, but this string was never initialized. Therefore,
you're assigning an empty string to strReadVendorFile.

If what you want is to discard the slice you just processed, there are
a number of approches:

strReadVendorFile = Right(strReadVendorFile, _
strReadVendorFile.Length - intFielValue)

Or

strReadVendorFile = Mid(strReadVendorFile, intFielValue + 1)

Or

strReadVendorFile = strReadVendorFile.Substring(intFielValue + 1)

Or

strReadVendorFile = strReadVendorFile.Remove(0, intFielValue + 1)

Etc, etc.

intFieldsRead += 1
Loop

Hmmm if you're just incrementing the intFieldsRead var (which controls
the loop) why not use a For...Next loop, instead?
<snip>

HTH.

Regards and good luck.

Branco.
 
Ok, well I have some bad news... Option Explicit is on, and all of my
control names are correct.... I replaced my code with yours:

Private Sub fillTextFields(ByVal TextControls() As Control)
'Populates the controls in TextControls with the fields read from the
'curretn line of the vendor file

'Splits the comma separated items from the line read
'from VendorFile
Dim Texts() As String = VendorFile.ReadLine().Split(","c)

'iterates by each field and saves it on the corresponding control
For Field As Integer = 0 To _
System.Math.Min(Texts.Length, TextControls.Length) - 1
TextControls(Field).Text = Texts(Field).Trim
Next
End Sub


and it errors out at:

TextControls(Field).Text = Texts(Field).Trim

When I put the cursor over ____ it says:

TextControls : "TextControls = {Length = 10}"
(Field): "Field = 0"
..Text: "Public Overridable property Text() As String"
Texts: "Text = {Length = 10}"
(Field): "Field = 0"
..Trim: = "Public Function Trim() As String"

The error message it displays is:

"An unhandled exception of type 'System.NullReferenceException'
occurred in Appointment.exe

Additional information: Object reference not set to an instance of an
object."


If there is anything else that I could give you that you think would
help, let me know. Thanks.
 
James said:
Ok, well I have some bad news... Option Explicit is on, and all of my
control names are correct.... I replaced my code with yours:
and it errors out at:

TextControls(Field).Text = Texts(Field).Trim
The error message it displays is:

"An unhandled exception of type 'System.NullReferenceException'
occurred in Appointment.exe

Additional information: Object reference not set to an instance of an
object."


If there is anything else that I could give you that you think would
help, let me know. Thanks.

Glad to know Option Explicit is on.

Now, the error is coming from the string or it's coming from the
control. One way to know would be to split the offending line in two:

Dim Value As string = Texts(Field).Trim
TextControls(Field).Text = Value

Of course, you might as well check in the immediate window:

? TextControls(Field) Is Nothing
? Texts(Field) Is Nothing

Regards,

Branco.
 
Ok, I split the line into two, like so:
For Field As Integer = 0 To System.Math.Min(Texts.Length,
TextControls.Length) - 1
Dim Value As String = Texts(Field).Trim
TextControls(Field).Text = Value
Next

It errors out on the TextControls(Field).Text = Value line

After first line:
? TextControls(Field) Is Nothing
True

? Texts(Field) Is Nothing
False

I couldn't test it after the second line b/c it errors out.

I'm pretty sure I had it narrowed down to this issue earlier, do you
have any ideas why the control array isnt working? Thanks for all of
your help.
 
James said:
Ok, I split the line into two, like so:
For Field As Integer = 0 To System.Math.Min(Texts.Length,
TextControls.Length) - 1
Dim Value As String = Texts(Field).Trim
TextControls(Field).Text = Value
Next

It errors out on the TextControls(Field).Text = Value line
<snip>

Put a breakpoint in the line where the control array is initialized and
doublecheck its contents, trying to locate the control name that
doesn't turn into an actual control reference.

Is the array declaration and initialization at form scope? If not, post
here the code block where it is initialized, parameters and all.

Post also the snippet from where your sub (the one we're working on) is
beng called...

HTH.

Regards,

Branco.
 
Back
Top