Tips for using one line of code to complete 17 types of VB

Source: Internet
Author: User
Tags strfind
Programming should focus on efficiency. Although the speed of today's computers is not a problem, if a line of code can be completed, why should we use more code? Now we will introduce some tips on using one line of code in VB.

1. the following code is caused by unclear logic operations.

If A = true Then
C = Not B
Else
C = B
End If

Yes:

C = A XOR B

2. If the following code is added:

If C = true then
D = 28
Else
D = 29
End IF
D = Iif (a xor B), 28, 29)

3. boolean assignment, often ignored, for example:

If A = 13 then

B = True

Else

B = False

End If

Yes:

B = A = 13

Or:

B = (A = 13)

I prefer the latter so that the code is easy to understand.
4. String validity check:

If IsNull (StrOrg) Or StrOrg = "" then

 

Yes:

If Len (StrOrg & "") <> 0 then

5. Number of string repetitions

RepeatCount = Ubound (Split (StrOrg, StrFind ))

Similarly, if you want to judge the string validity:

RepeatCount = Iif (Len (StrOrg & "") = 0), 0, Ubound (Split (StrOrg, StrFind ))

6. Sometimes you need to determine whether this element exists in the string array. In this case, you 'd better use the separator string instead of the array:

If Len (orgstr) = Len (replace (orgstr, findstr) then

Indicates that this element does not exist.

7. It is best to use variants for Array initialization. In this way, it is also a line of statements, such:

Intarr = array)

Note that the variable suffix is required. The code above, if you want to define it as a long integer, then

Intarr = array (12 &, 28 &, 29 &, 30 &, 31 &, 52 &, 24 &, 60 &)

Define intarr as a variant

8. determine the size:

Intmax = IIF (INTA> intb), INTA, intb)
Intmin = IIF (INTA <intb), INTA, intb)

9. Select case by index

Function getchoice (IND as integer)
Getchoice = choose (IND, "Speedy", "united", "federal ")
End Function

10. Select case by expression (this type of conversion requires no case else. Otherwise, an error will occur)

Function matchup (cityname as string)
Matchup = tch (cityname = "London", "English", cityname _
= "Rome", "Italian", cityname = "Paris", "French ")
End Function

11. Use IIF, which already exists

Function checkit (testme as integer)
Checkit = IIF (testme> 1000, "large", "small ")
End Function

12. Whether the dynamic string array has been initialized

If Len (join (strarr) = 0 then

 

The dynamic string array is not initialized.

13. Specify the current value of the read-only combbox. if you can confirm that this value is in it, it will not be wrong. Then:

Combbox = curvalue

Note: it cannot be written as follows:

Combbox. Text = curvalue

The former is actually the write _ default attribute, while the latter is the write Text because it is read-only, it will cause errors.
14. If you have the following code:

Select case combbox. Text
Case "London"
Call funcstrlang (3)
Case "Rome"
Call funcstrlang (5)
......
End select

 

You can use the ItemData attribute, namely:

Itemdata = 3 for "London"
Itemdata of "Rome" = 5

So:

Call funcstrlang (combbox. itendata)

15. If you have the following code:

Select case combbox. Text
Case "London"
Call clscity. cityintr_london
Case "Rome"
Call clscity. cityintr_rome
......
End select

As long:

Callbyname clscity, "cityintr _" & combbox. Text, vbmethod

16. Copy the array to another variable:

Dim iorgarr (30) as integer
Dim idesarr as Variant
......
Idesarr = iorgarr

That is, if the primary variant directly obtains the array pointer, all elements are copied.

17. If the following code is available:

Do while not rsado. EOF
If Len (desstr) <> 0 then
Desstr = desstr & vbtab
End if
Desstr = rsado! Rec_id
RsAdo. MoveNext
Loop

As long:

DesStr = RsAdo. GetString ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.