Solved

Code enhancement

Posted on 2016-11-15
5
35 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
[X]
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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

740 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