user2994834 user2994834 - 2 months ago 9
C# Question

Message is not re updating

I have 2 kind of "Description" for class "People",


  1. Having data with Message:, Priority: and Tag:

  2. Having data without these

    List<People> lstPeople = new List<People>
    {
    new People { Message = "[1y] Message: test messg1 Priority: top Tag: t1" },
    new People { Message = "without Message, Priority and Tag desc" }
    };
    var data = lstPeople;



I would like to extract data after "Message:", "Priority:" and "Tag:" and re-update of each entity of "People" class "Message", "Priority" and "Tag".

Below code does update for "Priority" and "Tag" but not for "Message". What could be the reason?

public class People
{
const string ALERT_DESC_MARKER = "Message:";
const string PRIORITY_MARKER = "Priority:";
const string TAG_MARKER = "Tag:";
private string _description;
private string _tag;
private string _priority;
public string Message
{
get
{
if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
return _description ?? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim();
else
return _description;
}
set
{
_description = value;
}
}

public string Priority
{
get
{
if (_description.Contains(PRIORITY_MARKER) && _description.Contains(TAG_MARKER))
return _priority = GetTextPart(_description, PRIORITY_MARKER, TAG_MARKER).Trim();
else
return _priority;
}
set
{
_priority = value;
}
}

public string Tag
{
get
{
if (_description.Contains(TAG_MARKER))
return _tag = GetTextPart(_description, TAG_MARKER, null).Trim();
else
return _tag;
}
set
{
_tag = value;
}
}

private string GetTextPart(string text, string before, string after)
{
string result = null;
int posBefore = text.IndexOf(before);

if (after != null)
{
int posAfter = text.IndexOf(after);
result = text.Remove(posAfter).Substring(posBefore + before.Length).TrimEnd();
}
else
result = text.Substring(posBefore + before.Length);

return result;
}
}

Answer

The problem is in the getter of Message. As the documentation says:

The ?? operator is called the null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.

So in your case you always return the entire string if _description is not null. Instead you need to return GetTextPart if this condition (_decription != null) is met. Like this:

if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
    return _description != null ? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim() : _description;

EDIT:

Here is your test example:

List<People> lstPeople = new List<People>
{
    new People { Message = "[1y] Message: test messg1 Priority: top Tag: t1" },
    new People { Message = "without Message, Priority and Tag desc" }
};
var data = lstPeople;


foreach (var item in lstPeople)
{
    Console.WriteLine(item.Message);
    Console.WriteLine(item.Priority);
    Console.WriteLine(item.Tag);
}

Output first Item:

test messg1

top

t1

Output second Item:

without Message, Priority and Tag desc

in the second item Priority and Tag are null.

EDIT 2:

Here is the Message getter. The rest of the code that I used to test is identical to the one that you posted. The commented out part is your old/posted version

public string Message
{
    get
    {
        if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
            return _description != null ? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim(): _description;

        //if (_description.Contains(ALERT_DESC_MARKER) && _description.Contains(PRIORITY_MARKER))
        //    return _description ?? GetTextPart(_description, ALERT_DESC_MARKER, PRIORITY_MARKER).Trim();
        else
            return _description;
    }
    set
    {
        _description = value;
    }
}
Comments