none
Replace returning Nothing

    Question

  • Hi

    I have code below;

    Msg = ""
    Msg = Replace(Msg, "'", "''")
    After the second line Msg is Nothing while I expected it to be "". What is going on here and how can I avoid Nothing?


    Thanks

    Regrads

    Wednesday, March 15, 2017 9:18 PM

All replies

  • You set Msg="" which means Msg = Nothing

    Unless Msg contains something it will be nothing...


    Wednesday, March 15, 2017 9:37 PM
  • Generally speaking given a string with data and using the following using VB.NET.

    Dim Msg As String = "Karen"
    Msg = Nothing
    If String.IsNullOrWhiteSpace(Msg) Then
        MessageBox.Show("Is nothing")
    End If
    

    While this will not be empty

    Dim Msg As String = "Karen"
    Msg = Msg.Replace("'", "''")
    If String.IsNullOrWhiteSpace(Msg) Then
        MessageBox.Show("Is nothing")
    End If


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by George-Frias Wednesday, March 15, 2017 10:09 PM
    • Unproposed as answer by George-Frias Wednesday, March 15, 2017 10:09 PM
    Wednesday, March 15, 2017 9:40 PM
    Moderator
  • Hi

    I have code below;

    Msg = ""
    Msg = Replace(Msg, "'", "''")
    After the second line Msg is Nothing while I expected it to be "". What is going on here and how can I avoid Nothing?


    Thanks

    Regrads

    Seems a bit odd to me also

    Option Explicit On
    Option Compare Binary
    Option Infer Off
    Option Strict On
    Public Class Form1
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim Msg As String = Nothing
            MessageBox.Show((Msg Is Nothing).ToString)
            Msg = ""
            MessageBox.Show((Msg Is Nothing).ToString)
            Msg = Replace(Msg, "", "Lorem Ipsum Dolor Antipoloponese")
            MessageBox.Show((Msg Is Nothing).ToString)
        End Sub
    End Class

    Have a look at that, it appears that the replace line can be anything and doesn't affect the change.

    Wednesday, March 15, 2017 9:59 PM
  • So... Because String.Empty is not legal in that statement it reverts to initial type? That must be some type of overload miscreant.

    Because that's wrong.

    • Edited by George-Frias Wednesday, March 15, 2017 10:27 PM clarity
    Wednesday, March 15, 2017 10:16 PM
  • So... Because String.Empty is not legal in that statement it reverts to initial type? That must be some type of overload.

    Because that's wrong.

     Huh?

     If you have an empty string to start with and try to replace any character(s) in it,  you will get an empty string back.  You can't replace characters if they don`t exist to begin with.  There is nothing illegal about passing an empty string to the function though.

     If i put this in a Button Click event,  it says it is "Empty",  not "Nothing".

            Dim msg As String = ""
            msg = Replace(msg, "'", "''")
    
            If msg = Nothing Then Me.Text = "Nothing"
            If msg = "" Then Me.Text = "Empty"
    


    If you say it can`t be done then i`ll try it

    • Proposed as answer by Frank L. Smith Wednesday, March 15, 2017 10:47 PM
    • Edited by IronRazerz Wednesday, March 15, 2017 10:47 PM
    Wednesday, March 15, 2017 10:40 PM
  • Hi

    I have code below;

    Msg = ""
    Msg = Replace(Msg, "'", "''")
    After the second line Msg is Nothing while I expected it to be "". What is going on here and how can I avoid Nothing?


    Thanks

    Regrads

    Additionally, the Replace function you're using is a left-over from VB6. The string class has its own:

    https://msdn.microsoft.com/en-us/library/system.string.replace(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, March 15, 2017 10:49 PM
  • In my opinion that should not matter.
    Wednesday, March 15, 2017 10:52 PM
  • In my opinion that should not matter.
    Nothing refers to the condition of the variable - it has no object to point to.  An empty string is a valid object that is pointed to by the variable, but which happens to have no characters.  The difference matters a lot.
    Wednesday, March 15, 2017 11:10 PM
  • In my opinion that should not matter.

    George,

    I didn't know who you were talking to or "that" was a reference to, but Acamar is correct.

    Not to be taking this thread too far off-topic but so you understand what null (Nothing) is?

    I'm not being snarky here - it really is important to understand the difference between reference types and value types:

    https://msdn.microsoft.com/en-us/library/t63sy5hs.aspx

    If you're a bit fuzzy on that then start a discussion and many folks here will chime in to help fill in the gaps. :-)


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, March 15, 2017 11:33 PM
  • I must be understood because I tested the code and responded to what he said about a string being empty or not making the difference in the replacement return. The default type is string, why would the last MsgBox from the second example return nothing.

    That's what this is all about right?

    Wednesday, March 15, 2017 11:44 PM
  • I must be understood because I tested the code and responded to what he said about a string being empty or not making the difference in the replacement return. The default type is string, why would the last MsgBox from the second example return nothing.

    That's what this is all about right?

    Who are you talking to?

    We don't all see this forum the same way and I can't tell, but that notwithstanding, until the OP replies back, I'll stay quiet.

    A null string is a very different animal than an empty string though...


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Wednesday, March 15, 2017 11:52 PM
  • Your question misleads a little bit, you tell it "is" Nothing. Probably it "=" nothing and that is not the same in VB.

    Nothing can mean in VB

    1. With value types the default value, for a string that is "" or otherwise written "zero characters". 
    2. For reference types it means always not instanced. 

    2 can also be if a string is not yet used because the default setting is done as soon as it is used and not earlier. 

    Therefore "Is" Nothing means not instanced, and "=" Nothing means that it has the default value. 

    Be aware that this is a big difference with C#, which has not default values of value types. 

    Be aware that a not yet set string also returns with = nothing true. 

    Just because I saw something in this thread what was consequent told in 2002. Be aware that Replace is a   VB function which is completely implemented in all .Net Frameworks and a main element of VB. 

    https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.strings.replace.aspx


    Success
    Cor



    Wednesday, March 15, 2017 11:57 PM
  • Frank.

    This is the first and last time I will address you.

    I'm sorry that you cannot see that I replied to you.

    If you cannot see that, it's not my problem.

    I'm kind and considerate and would appreciate if you did not mock me in front of my peers.

    Thank you.

    Wednesday, March 15, 2017 11:59 PM
  • Frank.

    This is the first and last time I will address you.

    I'm sorry that you cannot see that I replied to you.

    If you cannot see that, it's not my problem.

    I'm kind and considerate and would appreciate if you did not mock me in front of my peers.

    Thank you.

    Mock you??

    Ok - whatever.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Thursday, March 16, 2017 12:02 AM
  • In my opinion that should not matter.

    Nothing refers to the condition of the variable - it has no object to point to.  An empty string is a valid object that is pointed to by the variable, but which happens to have no characters.  The difference matters a lot.

    Can you explain this sample? Button_Click event

            Dim Msg As String = Nothing
            MessageBox.Show("Line : Dim Msg As String = Nothing" & vbNewLine & "Msg is Nothing ? = " & (Msg Is Nothing).ToString)
            Msg = ""
            MessageBox.Show("Line : Msg = " & Chr(34) & Chr(34) & vbNewLine & "Msg is Nothing ? = " & (Msg Is Nothing).ToString)
            Msg = Replace(Msg, "lallo perego", "Lorem Ipsum Dolor Antipoloponese")
            MessageBox.Show("Line : Msg = Msg.Replace(" & Chr(34) & Chr(34) & "Lorem Ipsum Dolor Antipoloponese)" & vbNewLine & "Msg is Nothing ? = " & (Msg Is Nothing).ToString)
            Msg = String.Empty
            MessageBox.Show("Line : Msg = String.Empty" & vbNewLine & "Msg is Nothing ? = " & (Msg Is Nothing).ToString)

    First Line = It is nothing
    Second Line = It is not nothing
    Third Line = It is Nothing
    fourth Line = It is not nothing

    It's the third line that has me baffled, using the .net Replace changes it to "It is not nothing" 

    Edit - Pasted poorly, third message box has wrong text
    • Edited by Devon_Nullman Thursday, March 16, 2017 12:21 AM messed it up
    Thursday, March 16, 2017 12:13 AM
  • Listen I may have overreacted a little. I'd like to apologize and say that it happened because I felt like you are picking on me.

    I believe that is not your intention. I know that now.

    Thursday, March 16, 2017 12:33 AM