Solved

Code enhancement

Posted on 2016-11-15
5
13 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 69

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now