none
can we use == null as a comparison

    Question

  • im using state machine workflow to create workflow for sharepoint. i manage to check if column 'checker 2' has value or not. if its not null it will be directed to a state and if its null to another state. i just want to know if this possible since when the workflow is running theres a notification of 'error occured' when theres no value in 'checker 2' column 

      private void checker1(object sender, ConditionalEventArgs e)
            {
                string checker2 = workflowProperties.Item["Checker 2"].ToString();


                if (checker2 != null)
                {
                    e.Result = true;
                }
                else if (checker2 == null)
                {
                    e.Result = false;

                }

            }

    Wednesday, November 21, 2012 8:27 AM

Answers

  • Hi,

    Don't use ToString() otherwise if there is no value then it will throw an error. Cast that column and just try like this:

    string checker2 = Convert.ToString(workflowProperties.Item["Checker 2"]);
    
    if(checker2 != null || checker2 != "")
    {}
    
    

    Hope it could help


    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    • Marked as answer by MRu_ Thursday, November 22, 2012 2:54 AM
    Wednesday, November 21, 2012 8:36 AM
    Moderator
  • Try this If (workflowProperties.Item["Checker 2"] == Null) Let me know if this helped...
    • Marked as answer by MRu_ Thursday, November 22, 2012 2:54 AM
    Thursday, November 22, 2012 2:17 AM
  • somehow it does the job. i dont know why bcoz i dont see any much difference compared to mine or hemendra's solution. thanks guys!

    There is a difference. When you get string data using ToString() and value is null then you will get the casting error but when you use my code then it will cast the string and avoid the error. If you have any doubt then check and see the result.

    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    • Marked as answer by MRu_ Friday, November 23, 2012 7:27 AM
    Thursday, November 22, 2012 4:54 AM
    Moderator
  • My suggestion is always to check the object whether its NULL and if it is not NULL then use the object's value. I recommend this as the best practice... this is what we have been doing for several years...

    • Marked as answer by MRu_ Friday, November 23, 2012 7:27 AM
    Thursday, November 22, 2012 3:37 PM

All replies

  • Hi,

    Don't use ToString() otherwise if there is no value then it will throw an error. Cast that column and just try like this:

    string checker2 = Convert.ToString(workflowProperties.Item["Checker 2"]);
    
    if(checker2 != null || checker2 != "")
    {}
    
    

    Hope it could help


    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    • Marked as answer by MRu_ Thursday, November 22, 2012 2:54 AM
    Wednesday, November 21, 2012 8:36 AM
    Moderator
  • ive already try your solution but it still 'error occured'. when i debug it, it says that checker2 Object reference not set to an instance of an object. any suggestion?
    Thursday, November 22, 2012 2:02 AM
  • Try this If (workflowProperties.Item["Checker 2"] == Null) Let me know if this helped...
    • Marked as answer by MRu_ Thursday, November 22, 2012 2:54 AM
    Thursday, November 22, 2012 2:17 AM
  • somehow it does the job. i dont know why bcoz i dont see any much difference compared to mine or hemendra's solution. thanks guys!
    Thursday, November 22, 2012 2:54 AM
  • somehow it does the job. i dont know why bcoz i dont see any much difference compared to mine or hemendra's solution. thanks guys!

    There is a difference. When you get string data using ToString() and value is null then you will get the casting error but when you use my code then it will cast the string and avoid the error. If you have any doubt then check and see the result.

    Hemendra: "Yesterday is just a memory,Tomorrow we may never see"

    Whenever you see a reply and if you think is helpful, click "Alternate TextVote As Helpful"! And whenever you see a reply being an answer to the question of the thread, click "Alternate TextMark As Answer

    • Marked as answer by MRu_ Friday, November 23, 2012 7:27 AM
    Thursday, November 22, 2012 4:54 AM
    Moderator
  • My suggestion is always to check the object whether its NULL and if it is not NULL then use the object's value. I recommend this as the best practice... this is what we have been doing for several years...

    • Marked as answer by MRu_ Friday, November 23, 2012 7:27 AM
    Thursday, November 22, 2012 3:37 PM
  • thanks guys! ^^
    Friday, November 23, 2012 7:37 AM
  • thanks. i will look into this ^^
    Friday, November 23, 2012 7:39 AM