Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While @Konrad Rudolph's answered your specific question, I'd like to continue where he left off saying: </p> <blockquote> <p>This makes no sense and shouldn’t even compile (or at least give a warning). Your first code is fine – it works always and there is no downside. It’s not clear what the second code is even trying to achieve.</p> </blockquote> <p>He's right in that you shouldn't even be doing what your second line is suggesting. The simple answer that achieves the effect you are trying for, would be to use <code>TryCast</code> instead. You are only assigning a non-empty string value when <code>drTest("column")</code> contains a value. Since that's the case, why not do:</p> <pre><code>Dim sTest As String = TryCast(drTest("column"), String)</code></pre> <p>This way you get your value if the <code>TryCast</code> succeeds, otherwise you get <code>Nothing</code>.</p> <p>Now since <code>TryCast</code> returns <code>Nothing</code> and since your example showed that you wanted to assign <code>""</code>, you might not want to go this route, as it can lead into the frustration of handling String Nothingness vs "" / String.Empty. You could use the <code>String.IsNullOrEmpty()</code> method if you want, but that requires you to pass in your string as a parameter and, to me, that clunks up your code. Alternatively, you could create your own StringExtensions Module like I have and use the extensions to check Nothingness and Emptiness:</p> <pre><code>Option Explicit On Option Strict On Imports System.Runtime.CompilerServices Public Module StringExtensions &lt;Extension()> _ Public Function IsNullOrEmpty(ByVal s As String) As Boolean Return s Is Nothing OrElse s.Trim.Length.Equals(0) End Function &lt;Extension()> _ Public Function IsNotNullOrEmpty(ByVal s As String) As Boolean Return s IsNot Nothing AndAlso s.Trim.Length > 0 End Function End Module</code></pre> <p>Then to achieve your effect of setting <code>""</code> with respect to Nothingness, one could use the combination of the <code>TryCast</code> and my StringExtensions methods to do:</p> <p><strong>With ternary If</strong></p> <pre><code>Dim sColumn1 As String = TryCast(drTest("column1"), String) Dim sColumn2 As String = TryCast(drTest("column2"), String) Dim sColumn3 As String = TryCast(drTest("column3"), String) Dim sTest1 As String = If(sColumn1.IsNotNullOrEmpty, sColumn1, String.Empty) Dim sTest2 As String = If(sColumn2.IsNotNullOrEmpty, sColumn2, String.Empty) Dim sTest3 As String = If(sColumn3.IsNotNullOrEmpty, sColumn3, String.Empty)</code></pre> <p><strong>Without ternary If</strong></p> <pre><code>Dim sTest1 As String = String.Empty Dim sTest2 As String = String.Empty Dim sTest3 As String = String.Empty Dim sColumn1 As String = TryCast(drTest("column1"), String) Dim sColumn2 As String = TryCast(drTest("column2"), String) Dim sColumn3 As String = TryCast(drTest("column3"), String) If sColumn1.IsNotNullOrEmpty Then sTest1 = sColumn1 If sColumn2.IsNotNullOrEmpty Then sTest2 = sColumn2 If sColumn3.IsNotNullOrEmpty Then sTest3 = sColumn3</code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload