-1

There has to be a better way to do this?! I was messing around with ?. etc but could not figure out the proper context for it. I need to add several more items to the console output, so adding several more nested try-catch is daunting yet do-able. would have to handle 4 different items that might throw exceptions.

Perhaps, I should be building up the string piece by piece before the writeline statement?

"for now" I have this mess:

foreach (WorkItem workItem in workItems){
// write work item to console
try  // ideal both assigned and tagged
{
    Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id, 
    workItem.Fields["System.Title"], workItem.Fields["System.WorkItemType"], 
    workItem.Fields["System.AssignedTo"], workItem.Fields["System.State"], 
    workItem.Fields["System.Tags"]);
}
catch (Exception) // at least one not correct, maybe two
{
    try  // is it the assigned?
    {
        Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id, 
        workItem.Fields["System.Title"], 
        workItem.Fields["System.WorkItemType"], "Unassigned", 
        workItem.Fields["System.State"], workItem.Fields["System.Tags"]);
    }
    catch (Exception)
    {
        try // is it the tags?
        {
            Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id, 
            workItem.Fields["System.Title"], 
            workItem.Fields["System.WorkItemType"], 
            workItem.Fields["System.AssignedTo"], 
            workItem.Fields["System.State"], "NoTags");

        }
        catch (Exception)  // its both
        {
            Console.WriteLine("{0} {1} {2} {3} {4} {5}", workItem.Id, 
            workItem.Fields["System.Title"], 
            workItem.Fields["System.WorkItemType"], "Unassigned", 
            workItem.Fields["System.State"], "NoTags");
        }
    }
}

}

Piotr P
  • 294
  • 1
  • 4
  • 12
David Elgstuen
  • 153
  • 2
  • 7
  • This is a question about Exception handling, and I ahve two articles that I like to link on the mater: https://blogs.msdn.microsoft.com/ericlippert/2008/09/10/vexing-exceptions/ | https://www.codeproject.com/Articles/9538/Exception-Handling-Best-Practices-in-NET | They should get you up to speed on the do's and don'ts – Christopher Nov 14 '19 at 15:52
  • `workItem?.Fields["x"] ?? "No x"` ? as long as `WorkItem.Fields[..]` doesn't throw but returns null – Selvin Nov 14 '19 at 15:54
  • 1
    What about using `workItem.Fields.Contains("name") ? workItem.Fields["name"] : "default"` instead. Assuming that `WorkItem` is `Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem` – juharr Nov 14 '19 at 16:12
  • @juharr it is // Create instance of WorkItemTrackingHttpClient using VssConnection WorkItemTrackingHttpClient witClient = connection.GetClient(); So I tried this format: workItem.Fields.Contains("System.AssignedTo")? workItem.Fields["System.AssignedTo"] :"Unassigned" But ran into CS1503 Argument 1: cannot convert from 'string' to 'System.Collections.Generic.KeyValuePair' – David Elgstuen Nov 14 '19 at 18:14
  • @Selvin tried your proposal but it must be throwing. Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Generic.Dictionary`2.get_Item(TKey key) at AzureDevOpsApp.Program.Main(String[] args) – David Elgstuen Nov 14 '19 at 18:18
  • So this is Dictionary... The obviously use TryGetValue – Selvin Nov 14 '19 at 18:21
  • @Selvin No it's a [`FieldCollection`](https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2013/bb171991%28v%3dvs.120%29) which only has `TryGetById`. – juharr Nov 14 '19 at 18:52
  • 1
    @DavidElgstuen just slap a `ToString` in there `workItem.Fields.Contains("name") ? workItem.Fields["name"].ToString() : "default"`. Although the mention of `KeyValuePari` is odd. – juharr Nov 14 '19 at 18:53
  • @juharr The above class in the link is out of date. You can refer to current [sdk](https://learn.microsoft.com/en-us/dotnet/api/microsoft.teamfoundation.workitemtracking.webapi.workitemtrackinghttpclientbase.getworkitemsasync?view=azure-devops-dotnet). The Fields property for the workItem is a collection. – Levi Lu-MSFT Nov 15 '19 at 06:02

1 Answers1

2

Base on the discussion of the comments. I would like to summary the solution as below. Thank juharr and Selvin for sharing ideas.

1,Since Fields.Contains("") throws the error. You can use workItem.Fields.ContainsKey(). The the example code is as below:

workItem.Fields.ContainsKey("name") ? workItem.Fields["name"] : "default"

2, use TryGetValue()

workItem.Fields.TryGetValue("System.AssignedTo", out Object identityOjbect)? identityOjbect:"not exist"
Levi Lu-MSFT
  • 27,483
  • 2
  • 31
  • 43
  • Option 1 did work for me however I couldn't get option 2 to work. Console.WriteLine("{0}" "{1}" "{2}" "{3}", workItem.Id, workItem.Fields["System.Title"], workItem.Fields["System.WorkItemType"], (workItem.Fields.TryGetValue("System.AssignedTo", out Object identityOjbect) ? identityOjbect : "not exist")); has a syntax error missing a ',' expected – David Elgstuen Nov 15 '19 at 19:51
  • @David Elgstuen the format should be in a single double quote. Console.WriteLine("{0} {1} {2} {3}", workItem.Id,...}. `"{0}" "{1}" "{2}" "{3}"` is not a valid format – Levi Lu-MSFT Nov 16 '19 at 00:48