Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In this code filter variable is not clearing in loop, if it is not initialized, but if i initialize it it works proper. Whats the reason it not cleared when not initialized?

Dim tables() As String = {"AcDailyTxnDetailPreMoc31032020Final" _
                                , "AdvAcBalanceDetai_110720202" _
                                , "AdvAcBalanceDetail" _
                                , "AdvAcBalanceDetail_New"}
        For Each tableName As String In tables
            Dim filter As String
            If tableName.Contains("AcDailyTxnDetail") Then
                filter = IIf(filter = "", " TxnDate>'31/mar/2020'", filter)
            End If
        Next


What I have tried:

Dim tables() As String = {"AcDailyTxnDetailPreMoc31032020Final" _
                                , "AdvAcBalanceDetai_110720202" _
                                , "AdvAcBalanceDetail" _
                                , "AdvAcBalanceDetail_New"}
        For Each tableName As String In tables
            Dim filter As String=""
            If tableName.Contains("AcDailyTxnDetail") Then
                filter = IIf(filter = "", " TxnDate>'31/mar/2020'", filter)
            End If
        Next
Posted
Updated 11-Sep-20 20:07pm

To add to what Patrice has said, if you do not initialize a variable, it is given a default based on it's type. A "basic variable" such as an integer or double is given zero, so
VB
Dim i As Integer
Dim d As Double
Is the same as saying
VB
Dim i As Integer = 0
Dim d As Double = 0.0

But for more complex variables such as strings they aren't - these variables are given class default values, which is a special value called Nothing - which contains ... well, nothing. That's actually very important to know, because Nothing is not the same as "empty".
An empty string:
VB
Dim s As String = ""
means that the variable contains a real, live, genuine - if empty - string.
VB
Dim s As String
is different - it doesn't contain anything at all,, and if you try to use s to call any of the string methods, you will get a "Null reference exception" - which is saying "there is nothing here to call a method on!"

It's a bit like cars. You have a car which you always park in the same space. You go to the space, get in the car, and you can drive to work. The works fine, even if you sell the car and get a new one: it's in the space, so you can drive it. The car is the value (a string); the space is the variable (a container for a Car instance)

But ... if your wife gets up early and takes then the car the space is empty, and when you try to drive to work you have a problem: you can't do anything that you would normally do:
VB
Dim mySpace as Car
mySpace.OpenDoor(Cardoors.Driver)
Will fail (with a Null Reference) when you try to call it because the space is empty. In real life, if you approach your empty car parking space and try to open the door an sit down you are going to get some very strange looks, and you really aren't going to get to work any time soon! The computer world is the same - trying to use a variable containing Nothing means the computer gives you strange looks and doesn't work the way you hoped!

As a result, it's a good practice to always set an initial value when you declare a variable, even if it contains a zero or empty value.

Make more sense now?
 
Share this answer
 
Quote:
Whats the reason it not cleared when not initialized?

Because uninitialized string variable is not the same as empty string.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900