Interesting property behavior

Does anyone have any idea why this is not working (C # or VB.NET or another .NET language doesn't matter). This is a very simplified example of my problem (sorry for VB.NET):

    Private itsCustomTextFormatter As String
    Public Property CustomTextFormatter As String
        Get
            If itsCustomTextFormatter Is Nothing Then CustomTextFormatter = Nothing  'thinking this should go into the setter - strangely it does not'
            Return itsCustomTextFormatter
        End Get
        Set(ByVal value As String)
            If value Is Nothing Then
                value = "Something"
            End If
            itsCustomTextFormatter = value
        End Set
    End Property

If you do:

Dim myObj as new MyClass
Console.WriteLine(myObj.CustomTextFormatter)

You will be surprised at the result. He will print "Nothing." Anyone have an idea why he doesn't print "Something"

Here's a Unit Test per sentence:

Imports NUnit.Framework

<TestFixture()> _
Public Class Test
   Private itsCustomTextFormatter As String
    Public Property CustomTextFormatter As String
        Get
            If itsCustomTextFormatter Is Nothing Then CustomTextFormatter = Nothing 'thinking this should go into the setter - strangely it does not' 
            Return itsCustomTextFormatter
        End Get
        Set(ByVal value As String)
            If value Is Nothing Then
                value = "Something"
            End If
            itsCustomTextFormatter = value
        End Set
    End Property

    <Test()>
    Public Sub Test2()
        Assert.AreEqual("Something", CustomTextFormatter)
    End Sub
End Class

This returns:

Test2 : Failed  
  Expected: "Something"
  But was:  null

at NUnit.Framework.Assert.That(Object actual, IResolveConstraint expression, String message, Object[] args)
at NUnit.Framework.Assert.AreEqual(Object expected, Object actual)
+5
source share
3 answers

Your comment:

'thinking this should go into the setter - strangely it does not'    

causes an error. There are two ways in Visual Basic to return something from a function:

Function GetSomeValue() As String
    Return "Hello"
End Function

or

Function GetSomeValue() As String
    GetSomeValue = "Hello"
End Function

Mixing these two styles is perfectly legal, but confusing and bad practice:

Function GetSomeValue() As String
    GetSomeValue = "Hello" ' I should return Hello... '
    Return "GoodBye"       ' ...or perhaps not. '
End Function

, getter. ; , , , , . Return.

, , .

+13

unit test; # (. ).

( )

, ! , CustomTextFormatter = Nothing, Getter - ( , , ).

, ; . ( , # ??):

Public Property CustomTextFormatter As String
    Get
        If itsCustomTextFormatter Is Nothing Then
            Return "Something"
        End If
        Return itsCustomTextFormatter

    End Get
    Set(ByVal value As String)
        itsCustomTextFormatter = value
    End Set
End Property

#

    private string _foo;
    private string foo
    {
        get
        {
            if (_foo == null)
                foo = null;
            return _foo;
        }
        set
        {
            if (value == null)
                value = "Something";
            _foo = value;
        }
    }

    [TestMethod]
    public void Test()
    {
        Assert.AreEqual("Something", foo);
    }
+4

Because you never initialized a variable. You will have to "set" the property to nothing in order to actually get "something." To fix this, you should probably set the default value when declaring an internal variable

Private itsCustomTextFormatter As String = "Something"
-3
source

All Articles