Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Code enhancement

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net and creating a class 5 20
Changes to XXX.exe.config file are not retrieved on restart. 3 39
Where is this file? 3 25
Access/Visual Basic Question 3 24
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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

840 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