• .Net conversion assesing nulls

    Home » Forums » Developers, developers, developers » DevOps Lounge » .Net conversion assesing nulls

    Author
    Topic
    #468966

    I am converting some code from VB6 to .NET. I am having an issue evaluating an excel cell as empty.

    The goal is to see if an excel cell is empty and, if so, give it the contents of the cell in the column before it. Looping through rows, If the cell in column 8 is blank, replace it with the value in column 7 and format it as a date.

    The pre-conversion code is:
    If Not .Cells(LoopIndex, 8) = “” Then .Cells(LoopIndex, 8) = Format(.Cells(LoopIndex, 7), “mm/dd/yyyy”)

    The .Net conversion offers:
    If Not (.Cells._Default(LoopIndex, 8) = “”) Then .Cells._Default(LoopIndex, 8) = VB6.Format(.Cells._Default(LoopIndex, 7), “mm/dd/yyyy”)

    Using the conversion as-is I get
    *** ERROR *** Overload resolution failed because no Public ‘=’ can be called with these arguments: ‘Public Shared Operator =(a As String, b As String) As Boolean’: Argument matching parameter ‘a’ cannot convert from ‘__ComObject’ to ‘String’.

    So, trying to evaluate the comObject I use .ToString to evaluate the content of the cell:
    If Not (.Cells._Default(LoopIndex, 8).ToString = “”) Then .Cells._Default(LoopIndex, 8) = VB6.Format(.Cells._Default(LoopIndex, 7), “mm/dd/yyyy”)

    Now it runs without errors, but one problem. The condition is never satisfied. The “cell/string” appears to never be blank, null, nothing, isnullorempty, or an empty string even if I Trim it, even though I cannot see anything in it.

    Is my problem in how I am using the comObject or the fact that the cell isnt truly empty? How can I find out what is in that cell and how can I evaluate it for this condition? I appreciate any help in understanding how this cell range is being used further.

    Viewing 1 reply thread
    Author
    Replies
    • #1224272

      You have to use “==” to evaluate a value, “=” is to assign a value.

      You can also use .Equals() method which is the better way. Or better yet, if your using a sting and want to check if it’s null or empty use String.IsNullOrEmpty(valueToCheck).

      Hope this helps

      Ben

    • #1224273

      Thanks! I did use String.IsNullOrEmpty(valueToCheck) as one of my attempts and that does not work. As far as I can tell I think the cell has “spaces” of some kind. I did a Len(), if that is appropriate, as it says 18. I dont know what is in there since I tried to trim to nothing….

    Viewing 1 reply thread
    Reply To: .Net conversion assesing nulls

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: