Solved

Code enhancement

Posted on 2016-11-15
5
25 Views
Last Modified: 2016-11-15
Hi,
is there a better way of writing this piece of code:

Private Sub LoadDataInvoiceFields()
     

        Select Case StrPayTypeSpPayProc
            Case 1
                txtAccInv.Text = StrGrossValueSpPayProc
            Case 2
                txtHotelInv.Text = StrGrossValueSpPayProc
            Case 3
                txtFoodBevInv.Text = StrGrossValueSpPayProc
            Case 4
                txtLaundryInv.Text = StrGrossValueSpPayProc
            Case 5
                txtParkInv.Text = StrGrossValueSpPayProc
            Case 6
                txtPhone.Text = StrGrossValueSpPayProc
            Case 7
                txtMisInv.Text = StrGrossValueSpPayProc

        End Select

Open in new window

0
Comment
Question by:RIAS
  • 2
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41887846
As every case statement is identical then the following should be OK

txtAccInv.Text = StrGrossValueSpPayProc
0
 

Author Comment

by:RIAS
ID: 41887847
is my code fine?

Can the repetion like

= StrGrossValueSpPayProc
be avoided?
0
 
LVL 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 41887852
Instead of setting the value of the textbox, you could set it only once;
Private Sub LoadDataInvoiceFields()
     dim ctlX as textbox

        Select Case StrPayTypeSpPayProc
            Case 1
                ctlX = txtAccInv
            Case 2
                ctlX = txtHotelInv
            Case 3
                ctlX = txtFoodBevInv
            Case 4
                ctlX = txtLaundryInv
            Case 5
                ctlX = txtParkInv
            Case 6
                ctlX = txtPhone
            Case 7
                ctlX = txtMisInv
        End Select
ctlX.Text = StrGrossValueSpPayProc

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41887855
Sorry I'd not looked closely enough.  Ignore my previous comment.

The select...case is probably your best option, it really depends on how your form is designed and if you do have all seven textboxs.
0
 

Author Closing Comment

by:RIAS
ID: 41887856
Cheers!
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question