Need some array help.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James

    Need some array help.

    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) = "txtVendorStree tAddress.Text"
    vendorFields(2) = "txtVendorCity. Text"
    ....


    vendorFields(2) = "111 Star Ave"

    but really i want it to translate as

    txtVendorStreet Address.Text = "111 Star Ave"


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

  • Cor Ligthert [MVP]

    #2
    Re: Need some array help.

    James,

    Why not an array of textboxes

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

    I hope this helps,

    Cor

    "James" <jamesburkett@g mail.comschreef in bericht
    news:1160104995 .324296.74290@m 73g2000cwd.goog legroups.com...
    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) = "txtVendorStree tAddress.Text"
    vendorFields(2) = "txtVendorCity. Text"
    ...
    >
    >
    vendorFields(2) = "111 Star Ave"
    >
    but really i want it to translate as
    >
    txtVendorStreet Address.Text = "111 Star Ave"
    >
    >
    I know my logic is messed up, is this possible, and if so how coud I do
    it? Thanks
    >

    Comment

    • James

      #3
      Re: Need some array help.

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

      thanks.
      Cor Ligthert [MVP] wrote:
      James,
      >
      Why not an array of textboxes
      >
      dim myTextBoxes() as Textbox = {txtVendorName, txtVendorStreet ,etc}
      venodorfields(2 ).Text = "111StarAve nue"
      >
      I hope this helps,
      >
      Cor
      >
      "James" <jamesburkett@g mail.comschreef in bericht
      news:1160104995 .324296.74290@m 73g2000cwd.goog legroups.com...
      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) = "txtVendorStree tAddress.Text"
      vendorFields(2) = "txtVendorCity. Text"
      ...


      vendorFields(2) = "111 Star Ave"

      but really i want it to translate as

      txtVendorStreet Address.Text = "111 Star Ave"


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

      Comment

      • Cor Ligthert [MVP]

        #4
        Re: Need some array help.

        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


        "James" <jamesburkett@g mail.comschreef in bericht
        news:1160107429 .306038.5290@c2 8g2000cwb.googl egroups.com...
        hmmm sounds like a good solution, but i also have one combo box... and
        ideas how i could incorporate that into the array?
        >
        thanks.
        Cor Ligthert [MVP] wrote:
        >James,
        >>
        >Why not an array of textboxes
        >>
        >dim myTextBoxes() as Textbox = {txtVendorName, txtVendorStreet ,etc}
        >venodorfields( 2).Text = "111StarAve nue"
        >>
        >I hope this helps,
        >>
        >Cor
        >>
        >"James" <jamesburkett@g mail.comschreef in bericht
        >news:116010499 5.324296.74290@ m73g2000cwd.goo glegroups.com.. .
        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) = "txtVendorStree tAddress.Text"
        vendorFields(2) = "txtVendorCity. Text"
        ...
        >
        >
        vendorFields(2) = "111 Star Ave"
        >
        but really i want it to translate as
        >
        txtVendorStreet Address.Text = "111 Star Ave"
        >
        >
        I know my logic is messed up, is this possible, and if so how coud I do
        it? Thanks
        >
        >

        Comment

        • James

          #5
          Re: Need some array help.

          Great, thanks for all your help. Much appreciated!
          Cor Ligthert [MVP] wrote:
          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
          >
          >
          "James" <jamesburkett@g mail.comschreef in bericht
          news:1160107429 .306038.5290@c2 8g2000cwb.googl egroups.com...
          hmmm sounds like a good solution, but i also have one combo box... and
          ideas how i could incorporate that into the array?

          thanks.
          Cor Ligthert [MVP] wrote:
          James,
          >
          Why not an array of textboxes
          >
          dim myTextBoxes() as Textbox = {txtVendorName, txtVendorStreet ,etc}
          venodorfields(2 ).Text = "111StarAve nue"
          >
          I hope this helps,
          >
          Cor
          >
          "James" <jamesburkett@g mail.comschreef in bericht
          news:1160104995 .324296.74290@m 73g2000cwd.goog legroups.com...
          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) = "txtVendorStree tAddress.Text"
          vendorFields(2) = "txtVendorCity. Text"
          ...


          vendorFields(2) = "111 Star Ave"

          but really i want it to translate as

          txtVendorStreet Address.Text = "111 Star Ave"


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

          Comment

          • James

            #6
            Re: Need some array help.

            Cor,

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

            ctrTextFieldNam es(intFieldsRea d).Text =
            Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)

            it triggers this error:
            An unhandled exception of type 'System.NullRef erenceException ' 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,
            txtVendorStreet Address, txtVendorCity, txtVendorState, txtVendorZip,
            txtVendorPhone, txtVendorCOutst andingBill, txtVendorSalesR epName,
            txtVendorCommen ts, cboVendorPerfer redPmtPeriod}


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


            strReadVendorFi le = VendorFile.Read Line()
            'Store the number of records as an integer value
            intNumOfFields = ctrTextFieldNam es.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(strReadVe ndorFile, ",")
            '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
            ctrTextFieldNam es(intFieldsRea d).Text =
            Microsoft.Visua lBasic.Left(str ReadVendorFile, 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)
            strReadVendorFi le =
            Microsoft.Visua lBasic.Right(st rFieldValue, 100)
            intFieldsRead += 1
            Loop


            End Sub

            Comment

            • Branco Medeiros

              #7
              Re: Need some array help.


              James wrote:
              <snip>
              ctrTextFieldNam es(intFieldsRea d).Text =
              Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)
              >
              it triggers this error:
              An unhandled exception of type 'System.NullRef erenceException ' occurred
              in Appointment.exe
              <snip>
              intNumOfFields = ctrTextFieldNam es.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.

              Comment

              • James

                #8
                Re: Need some array help.

                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.
                Branco Medeiros wrote:
                James wrote:
                <snip>
                ctrTextFieldNam es(intFieldsRea d).Text =
                Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)

                it triggers this error:
                An unhandled exception of type 'System.NullRef erenceException ' occurred
                in Appointment.exe
                >
                <snip>
                intNumOfFields = ctrTextFieldNam es.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.

                Comment

                • James

                  #9
                  Re: Need some array help.

                  Sorry, no response here lately, I still am searching for an answer to
                  my error. Any ideas? Thanks

                  James wrote:
                  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.
                  Branco Medeiros wrote:
                  James wrote:
                  <snip>
                  ctrTextFieldNam es(intFieldsRea d).Text =
                  Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)
                  >
                  it triggers this error:
                  An unhandled exception of type 'System.NullRef erenceException ' occurred
                  in Appointment.exe
                  <snip>
                  intNumOfFields = ctrTextFieldNam es.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.

                  Comment

                  • Branco Medeiros

                    #10
                    Re: Need some array help.

                    James wrote:
                    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.Read Line().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.Le ngth) - 1
                    TextControls(Fi eld).Text = Texts(Field).Tr im
                    Next
                    End Sub
                    >From a previous post:
                    <snip>
                    Private Sub fillTextFields( ByVal ctrTextFieldNam es() As Control)
                    Dim intNumOfFields As Integer = 0
                    Dim intFieldsRead As Integer = 0
                    Dim intFieldValue As Integer
                    Dim strFieldValue As String
                    >
                    >
                    strReadVendorFi le = VendorFile.Read Line()
                    strReadVendorFi le 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 = ctrTextFieldNam es.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(strReadVe ndorFile, ",")
                    I personally preffer the intrinsic methods unless a library one
                    provides anything extra. In this case, *IMHO*, you should use

                    intFiledValue = strReadVendorFi le.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
                    ctrTextFieldNam es(intFieldsRea d).Text =
                    Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)
                    The same goes for the previous line: you should use (IMHO):

                    ctrTextFieldNam es(intFieldsRea d).Text = _
                    strReadVendorFi le.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)
                    strReadVendorFi le =
                    Microsoft.Visua lBasic.Right(st rFieldValue, 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 strReadVendorFi le.

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

                    strReadVendorFi le = Right(strReadVe ndorFile, _
                    strReadVendorFi le.Length - intFielValue)

                    Or

                    strReadVendorFi le = Mid(strReadVend orFile, intFielValue + 1)

                    Or

                    strReadVendorFi le = strReadVendorFi le.Substring(in tFielValue + 1)

                    Or

                    strReadVendorFi le = strReadVendorFi le.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.

                    Comment

                    • James

                      #11
                      Re: Need some array help.

                      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.Read Line().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.Le ngth) - 1
                      TextControls(Fi eld).Text = Texts(Field).Tr im
                      Next
                      End Sub


                      and it errors out at:

                      TextControls(Fi eld).Text = Texts(Field).Tr im

                      When I put the cursor over ____ it says:

                      TextControls : "TextContro ls = {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.NullRef erenceException '
                      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.


                      Branco Medeiros wrote:
                      James wrote:
                      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.Read Line().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.Le ngth) - 1
                      TextControls(Fi eld).Text = Texts(Field).Tr im
                      Next
                      End Sub
                      >
                      From a previous post:
                      >
                      <snip>
                      Private Sub fillTextFields( ByVal ctrTextFieldNam es() As Control)
                      Dim intNumOfFields As Integer = 0
                      Dim intFieldsRead As Integer = 0
                      Dim intFieldValue As Integer
                      Dim strFieldValue As String


                      strReadVendorFi le = VendorFile.Read Line()
                      >
                      strReadVendorFi le 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 = ctrTextFieldNam es.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(strReadVe ndorFile, ",")
                      >
                      I personally preffer the intrinsic methods unless a library one
                      provides anything extra. In this case, *IMHO*, you should use
                      >
                      intFiledValue = strReadVendorFi le.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
                      ctrTextFieldNam es(intFieldsRea d).Text =
                      Microsoft.Visua lBasic.Left(str ReadVendorFile, intFieldValue)
                      >
                      The same goes for the previous line: you should use (IMHO):
                      >
                      ctrTextFieldNam es(intFieldsRea d).Text = _
                      strReadVendorFi le.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)
                      strReadVendorFi le =
                      Microsoft.Visua lBasic.Right(st rFieldValue, 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 strReadVendorFi le.
                      >
                      If what you want is to discard the slice you just processed, there are
                      a number of approches:
                      >
                      strReadVendorFi le = Right(strReadVe ndorFile, _
                      strReadVendorFi le.Length - intFielValue)
                      >
                      Or
                      >
                      strReadVendorFi le = Mid(strReadVend orFile, intFielValue + 1)
                      >
                      Or
                      >
                      strReadVendorFi le = strReadVendorFi le.Substring(in tFielValue + 1)
                      >
                      Or
                      >
                      strReadVendorFi le = strReadVendorFi le.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.

                      Comment

                      • Branco Medeiros

                        #12
                        Re: Need some array help.


                        James wrote:
                        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:
                        <snip>
                        and it errors out at:
                        >
                        TextControls(Fi eld).Text = Texts(Field).Tr im
                        <split>
                        The error message it displays is:
                        >
                        "An unhandled exception of type 'System.NullRef erenceException '
                        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).Tr im
                        TextControls(Fi eld).Text = Value

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

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

                        Regards,

                        Branco.

                        Comment

                        • James

                          #13
                          Re: Need some array help.

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

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

                          After first line:
                          ? TextControls(Fi eld) 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.


                          Branco Medeiros wrote:
                          James wrote:
                          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:
                          <snip>
                          and it errors out at:

                          TextControls(Fi eld).Text = Texts(Field).Tr im
                          <split>
                          The error message it displays is:

                          "An unhandled exception of type 'System.NullRef erenceException '
                          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).Tr im
                          TextControls(Fi eld).Text = Value
                          >
                          Of course, you might as well check in the immediate window:
                          >
                          ? TextControls(Fi eld) Is Nothing
                          ? Texts(Field) Is Nothing
                          >
                          Regards,
                          >
                          Branco.

                          Comment

                          • Branco Medeiros

                            #14
                            Re: Need some array help.

                            James wrote:
                            Ok, I split the line into two, like so:
                            For Field As Integer = 0 To System.Math.Min (Texts.Length,
                            TextControls.Le ngth) - 1
                            Dim Value As String = Texts(Field).Tr im
                            TextControls(Fi eld).Text = Value
                            Next
                            >
                            It errors out on the TextControls(Fi eld).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.

                            Comment

                            Working...