Advertisement
Java_Volume1 Coding Standards #86161

Select Case

A beginner's look into the Select Case Structure . Comparing Select Case to If...ElseIf....End If.... This Tutorial is for beginner's .

AI

Yapay Zeka Özeti: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Kaynak Kod
original-source
<font color="#FF0000">Comparing</font> <font color="#0000FF">Select Case</font> 
to <font color="#0000FF">If</font>...<font color="#0000FF">ElseIf</font>....<font color="#0000FF">End 
If</font>.... 
<p>Whenever possible try to use the <font color="#0000FF">Select Case</font> structure.<br>
 Reason's are as follow's ;<br>
 <font color="#FF0000">A</font><font color="#0000FF">></font> Better understandability 
 of the code you just wrote.<br>
 <font color="#FF0000">B</font><font color="#0000FF">></font> Lead's to smaller 
 coding structure's .<br>
 <font color="#FF0000">C</font><font color="#0000FF">></font> Will decrease 
 the Informational Complexity of the <br>
 procedure by at least 10% ( This is a good thing ! )</p>
 
<font color="#FF0000">D</font><font color="#0000FF">></font> Lead's to re-use-ability 
of your code . 
<p>For instance ;<br>
 Look at the following structure's :</p>
<p><font color="#FF0000">A</font><font color="#0000FF">:</font> <br>
 <font color="#0000FF">Private Sub</font> DecisionStructuresCollide<font color="#0000FF">()</font><br>
 <font color="#0000FF">If </font>Text1.Text = 1 <font color="#0000FF">Then</font> 
 <br>
 <font color="#008000">' Do something</font><br>
 <font color="#0000FF">ElseIf</font> Text1.Text = 2 <font color="#0000FF">Then 
 </font><br>
 <font color="#008000">' Do something</font><br>
 <font color="#0000FF">End If</font><br>
 <font color="#0000FF">End Sub</font></p>
<p>To use the above structure is messy and will <br>
 lead to relentlessly<br>
 repeated code , thus over-bloating your procedure.</p>
<p><font color="#FF0000">B</font><font color="#0000FF">:</font><br>
 <font color="#0000FF">Private Sub</font> DecisionStructuresCollide<font color="#0000FF">()</font><br>
 <font color="#0000FF">Dim</font> sMyString <font color="#0000FF">As</font> <font color="#0000FF">String</font> 
 <font color="#008000">' Build the Variable Structure</font><br>
 sMyString = Text1.Text <font color="#008000">' Pass the Text in Text1.Text into 
 the Variable as a String</font><br>
</p>
<p><font color="#0000FF">Select Case</font> sMyString <br>
 <font color="#0000FF">Case 1</font> <font color="#008000">' Text1.Text Equaled 
 1</font><br>
 <font color="#008000">' Do Something</font><br>
 <font color="#0000FF">Case 2</font><font color="#008000"> ' Text1.Text Equaled 
 2</font><br>
 <font color="#008000">' Do Something else</font><br>
 <font color="#0000FF">Case Else</font> <font color="#008000">' Text1.Text Equaled 
 something else so <br>
 ' we can still be flexible and do <br>
 ' something else. Although the Case <br>
 ' Else statement is not required</font></p>
<p>SeeInCaseProcedureDoSomethingElse <font color="#008000">' Do Something else</font></p>
<p><font color="#0000FF">End Select</font><font color="#008000"> ' Each Select 
 Case Statement must be <br>
 ' accompanied with a closing End Select <br>
 ' statement </font> </p>
<p><font color="#0000FF">End Sub</font></p>
<p>Example <font color="#FF0000">B</font> is definately much cleaner code and 
 much <br>
 easier to read , not to mention the benefit's from the<br>
 Informational Complexity being smaller.</p>
<p>I'm not saying that in 100% of all cases should one use the<br>
 <font color="#0000FF">Select Case</font> Structure , however , whenever possible 
 it <br>
 will lead to a much better coding practice that you would<br>
 automatically try to stick to and then if your procedure<br>
 cannot be done by using this structure , then you can fall <br>
 back onto the old <font color="#0000FF">If</font>.....<font color="#0000FF">ElseIf</font>......<font color="#0000FF">End 
 If</font>...... routine.</p>
Orijinal Yorumlar (3)
Wayback Machine'den kurtarıldı