Solved

Code enhancement

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tool Box 2 34
I need help embedding Base64 Images using VB.Net - When image is a .jpg 2 41
vb.net checkbox 7 41
C# Offline Apllication 5 54
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

25 Experts available now in Live!

Get 1:1 Help Now