optimize code

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

    optimize code

    Hi,

    I have a ComboBox which is filled with 66 items on form_load event:

    Private Sub Form_Load()
    Combo1.AddItem "Tekvin" 'item number: 1
    Combo1.AddItem "Cikis" 'item number: 2
    Combo1.AddItem "Levililer" '....
    Combo1.AddItem "Sayilar"
    ...
    Combo1.AddItem "Vahiy" 'item number:66
    End sub

    When user selects one of them, beside this control in 4 diff. Label
    (InfoLbl) controls the app
    shows the description of the item selected:

    Private Sub Combo1_Click()
    If Combo1.Text = "Tekvin" Then
    InfoLbl1.Captio n = "Musa"
    InfoLbl2.Captio n = "Col"
    InfoLbl3.Captio n = "M.O. 1513"
    InfoLbl4.Captio n = "Yaradilist an M.O. 1657'ye kadar"
    End If
    ...
    ...
    'this means for 66 items = 66 conditions..
    End sub

    As I'm a beginner, I do not know whether this code is good. Is there another
    way of doing the same thing, more advanced, or much faster? (For ex.: Select
    Case, is it better?) If yes what is the optimized way?

    Thanks in advance!
    --
    Cengiz Ulku
    cengizu@bluewin .ch



  • J French

    #2
    Re: optimize code

    Yes, there is an easier way

    - actually I would be hard pressed to find a more difficult way

    Put all the data in a file
    - the solution will then become obvious

    On Thu, 28 Aug 2003 17:32:53 +0200, "Cengiz Ulku" <cengizu@bluewi n.ch>
    wrote:
    [color=blue]
    >Hi,
    >
    >I have a ComboBox which is filled with 66 items on form_load event:
    >
    >Private Sub Form_Load()
    > Combo1.AddItem "Tekvin" 'item number: 1
    > Combo1.AddItem "Cikis" 'item number: 2
    > Combo1.AddItem "Levililer" '....
    > Combo1.AddItem "Sayilar"
    > ...
    > Combo1.AddItem "Vahiy" 'item number:66
    >End sub
    >
    >When user selects one of them, beside this control in 4 diff. Label
    >(InfoLbl) controls the app
    >shows the description of the item selected:
    >
    >Private Sub Combo1_Click()
    > If Combo1.Text = "Tekvin" Then
    > InfoLbl1.Captio n = "Musa"
    > InfoLbl2.Captio n = "Col"
    > InfoLbl3.Captio n = "M.O. 1513"
    > InfoLbl4.Captio n = "Yaradilist an M.O. 1657'ye kadar"
    > End If
    > ...
    > ...
    >'this means for 66 items = 66 conditions..
    >End sub
    >
    >As I'm a beginner, I do not know whether this code is good. Is there another
    >way of doing the same thing, more advanced, or much faster? (For ex.: Select
    >Case, is it better?) If yes what is the optimized way?
    >
    >Thanks in advance!
    >--
    >Cengiz Ulku
    >cengizu@bluewi n.ch
    >
    >
    >[/color]

    Comment

    • Dag Sunde

      #3
      Re: optimize code

      "J French" <erewhon@nowher e.com> wrote in message
      news:3f4e24b5.2 [email protected] click.com...[color=blue]
      > Yes, there is an easier way
      >
      > - actually I would be hard pressed to find a more difficult way
      >
      > Put all the data in a file
      > - the solution will then become obvious
      >
      > On Thu, 28 Aug 2003 17:32:53 +0200, "Cengiz Ulku" <cengizu@bluewi n.ch>
      > wrote:
      >[color=green]
      > >Hi,
      > >
      > >I have a ComboBox which is filled with 66 items on form_load event:[/color][/color]
      <snipped />[color=blue][color=green]
      > >[/color][/color]

      I foresee another question from Cengiz, so i will elaborate a little
      bit on our close-mouthed friend Jerry's idea:

      If you put your 66 items into a textfile (Create it with notepad),
      one item on each line, with a separator character that isn't used
      elsewhere in your text between each field (Here I've used the "Bar" "|"):

      Tekvin|Musa|Col |M.O. 1513|Yaradilist an M.O. 1657'ye kadar
      ....
      ....

      Now, open the file and read the lines into ie. a string-array
      (You can ReDim on your way, or hard-code it to 66)

      (I would have created an UDT array, and split each line as I read it)

      Fill your combobox with the first field in each line in the array
      In the combo's click event, use combo.listindex to select the
      corresponding line from the array.

      Split that line in to it's respective fields and show them.
      (Mid$() and instr(), or Split() are your friends here)

      I am vague on purpose here, since you will learn *a lot* from this.

      When you hit the wall, post, and thou shall be enlightened...

      --
      Dag.


      Comment

      • Cookie

        #4
        Re: optimize code

        Dag Sunde wrote:[color=blue]
        > "J French" <erewhon@nowher e.com> wrote in message
        > news:3f4e24b5.2 [email protected] click.com...
        >[color=green]
        >>Yes, there is an easier way
        >>
        >>- actually I would be hard pressed to find a more difficult way
        >>
        >>Put all the data in a file
        >>- the solution will then become obvious
        >>
        >>On Thu, 28 Aug 2003 17:32:53 +0200, "Cengiz Ulku" <cengizu@bluewi n.ch>
        >>wrote:
        >>
        >>[color=darkred]
        >>>Hi,
        >>>
        >>>I have a ComboBox which is filled with 66 items on form_load event:[/color]
        >>[/color]
        > <snipped />
        >
        >
        > I foresee another question from Cengiz, so i will elaborate a little
        > bit on our close-mouthed friend Jerry's idea:
        >
        > If you put your 66 items into a textfile (Create it with notepad),
        > one item on each line, with a separator character that isn't used
        > elsewhere in your text between each field (Here I've used the "Bar" "|"):
        >
        > Tekvin|Musa|Col |M.O. 1513|Yaradilist an M.O. 1657'ye kadar
        > ....
        > ....
        >
        > Now, open the file and read the lines into ie. a string-array
        > (You can ReDim on your way, or hard-code it to 66)
        >
        > (I would have created an UDT array, and split each line as I read it)
        >
        > Fill your combobox with the first field in each line in the array
        > In the combo's click event, use combo.listindex to select the
        > corresponding line from the array.
        >
        > Split that line in to it's respective fields and show them.
        > (Mid$() and instr(), or Split() are your friends here)
        >
        > I am vague on purpose here, since you will learn *a lot* from this.
        >
        > When you hit the wall, post, and thou shall be enlightened...
        >
        > --
        > Dag.
        >
        >[/color]

        If you don't want to use an external file...
        Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
        in a DATA statement and READ them out...
        (Also something to learn ;-))

        --
        \|||/
        (. .)
        *--------ooO-(_)-Ooo--------*
        | |
        | [email protected] |
        | |
        *---------------------------*

        Comment

        • Bob Butler

          #5
          Re: optimize code

          "Cookie" <[email protected] e> wrote in message
          news:3F4EA086.2 [email protected]
          <cut>[color=blue]
          > If you don't want to use an external file...
          > Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
          > in a DATA statement and READ them out...
          > (Also something to learn ;-))[/color]

          In VB?

          Comment

          • Mike Williams

            #6
            Re: optimize code


            "Cookie" <[email protected] e> wrote in message
            news:3F4EA086.2 [email protected] ...
            [color=blue]
            > If you don't want to use an external file...
            > Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
            > in a DATA statement and READ them out...[/color]

            This is VB, Cookie. The man's using VB.

            Mike




            Comment

            • Cookie

              #7
              Re: optimize code

              Mike Williams wrote:[color=blue]
              > "Cookie" <[email protected] e> wrote in message
              > news:3F4EA086.2 [email protected] ...
              >
              >[color=green]
              >>If you don't want to use an external file...
              >>Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
              >>in a DATA statement and READ them out...[/color]
              >
              >
              > This is VB, Cookie. The man's using VB.
              >
              > Mike
              >
              >
              >
              >[/color]

              Isn't DATA supported in VB? Never had to use it in VB though,
              but that suprises me :)


              --
              \|||/
              (. .)
              *--------ooO-(_)-Ooo--------*
              | |
              | [email protected] |
              | |
              *---------------------------*

              Comment

              • Cookie

                #8
                Re: optimize code

                Cookie wrote:[color=blue]
                > Mike Williams wrote:[color=green]
                >> "Cookie" <[email protected] e> wrote[color=darkred]
                >>> If you don't want to use an external file...
                >>> Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
                >>> in a DATA statement and READ them out...[/color]
                >>
                >> This is VB, Cookie. The man's using VB.
                >>
                >> Mike[/color]
                >
                > Isn't DATA supported in VB? Never had to use it in VB though,
                > but that suprises me :)[/color]

                LOL, guess not... Well I'm learning still :-)

                Comment

                • mike

                  #9
                  Re: optimize code

                  new NG reader here. (vb GUI fanaic)

                  but from what i see in this situation
                  the GUI is what it's all about

                  a form can double as a "data" entry page with the various fields written as strings to a file.
                  i would imagine the easiest way is to have fixed length fields that are written in sequence.
                  using
                  open <filename>
                  put <string>

                  ok that's a easy way of saying something that really isen't all that tricky
                  but the GUI would double as a viewer, where each field could be a drop down box (ordered/optional) and a selection would fill ni the
                  rest of the fields ...
                  the only problem with that is duplicate entries where the first instance would always be the one pulled up. so it sould include a
                  <next/previous> system..


                  what i'm still buzzed about is the actual lack od a search engine for the data
                  (data = file containg data not sql type - which would still be a viable option in BV)
                  there is even a "matrix" type grid control that is standard in comctl.oxc (sp?)
                  but i've never used it successfully yet)


                  ok that's my 2 cents
                  now i'll shut up

                  mike





                  "Cookie" <[email protected] e> wrote in message news:3F505096.9 [email protected] ...[color=blue]
                  > Cookie wrote:[color=green]
                  > > Mike Williams wrote:[color=darkred]
                  > >> "Cookie" <[email protected] e> wrote
                  > >>> If you don't want to use an external file...
                  > >>> Put those lines ("Tekvin|Musa|C ol|blablablabla blabla")
                  > >>> in a DATA statement and READ them out...
                  > >>
                  > >> This is VB, Cookie. The man's using VB.
                  > >>
                  > >> Mike[/color]
                  > >
                  > > Isn't DATA supported in VB? Never had to use it in VB though,
                  > > but that suprises me :)[/color]
                  >
                  > LOL, guess not... Well I'm learning still :-)
                  >[/color]


                  Comment

                  Working...