<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to return a FeatureLayer with QueuedTask.Run in ArcGIS Pro SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128353#M7526</link>
    <description>&lt;P&gt;I would like to refactor my code and implement a method instead of writing/copying the same code again again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I check TOC whether a certain Layer exists. If TOC does not have it I create the FeatureLayer. I use this process a lot in my applications.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, here is the method but it does not work&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public Task&amp;lt;FeatureLayer&amp;gt; AddLayerToTOC(Uri layerUri, string layerName)
{
  return QueuedTask.Run(() =&amp;gt;
  {
    MainGridEnabled = false;

    FeatureLayer fLayer;
    TOCLayers = MapView.Active.Map.Layers;
    var TOCLayerNames = TOCLayers.Select(p =&amp;gt; p.Name);

    if (TOCLayerNames.Contains(layerName) == false)
    {
     fLayer = LayerFactory.Instance.CreateFeatureLayer(layerUri, MapView.Active.Map, 0, layerName);
    }

    else
    {
       foreach (Layer layer in TOCLayers)
       {
          if (layer.Name == layerName)
          {
             fLayer = layer as FeatureLayer;;
          }
       }
  }

    MapView.Active.ZoomToSelected();

    MainGridEnabled = true;

    return fLayer;
    });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get CS0165: Use of unassigned local variable&lt;/P&gt;&lt;P&gt;Problem coming from else {} part because VS thinks fLayer not assigned but I will do that while application is running.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a solution to this or another way of doing it?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 22 Dec 2021 16:27:03 GMT</pubDate>
    <dc:creator>Amadeus111</dc:creator>
    <dc:date>2021-12-22T16:27:03Z</dc:date>
    <item>
      <title>How to return a FeatureLayer with QueuedTask.Run</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128353#M7526</link>
      <description>&lt;P&gt;I would like to refactor my code and implement a method instead of writing/copying the same code again again.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I check TOC whether a certain Layer exists. If TOC does not have it I create the FeatureLayer. I use this process a lot in my applications.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, here is the method but it does not work&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;public Task&amp;lt;FeatureLayer&amp;gt; AddLayerToTOC(Uri layerUri, string layerName)
{
  return QueuedTask.Run(() =&amp;gt;
  {
    MainGridEnabled = false;

    FeatureLayer fLayer;
    TOCLayers = MapView.Active.Map.Layers;
    var TOCLayerNames = TOCLayers.Select(p =&amp;gt; p.Name);

    if (TOCLayerNames.Contains(layerName) == false)
    {
     fLayer = LayerFactory.Instance.CreateFeatureLayer(layerUri, MapView.Active.Map, 0, layerName);
    }

    else
    {
       foreach (Layer layer in TOCLayers)
       {
          if (layer.Name == layerName)
          {
             fLayer = layer as FeatureLayer;;
          }
       }
  }

    MapView.Active.ZoomToSelected();

    MainGridEnabled = true;

    return fLayer;
    });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get CS0165: Use of unassigned local variable&lt;/P&gt;&lt;P&gt;Problem coming from else {} part because VS thinks fLayer not assigned but I will do that while application is running.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a solution to this or another way of doing it?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Dec 2021 16:27:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128353#M7526</guid>
      <dc:creator>Amadeus111</dc:creator>
      <dc:date>2021-12-22T16:27:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a FeatureLayer with QueuedTask.Run</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128563#M7533</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;It is simple. Set it to null:&lt;/P&gt;&lt;LI-CODE lang="c"&gt;FeatureLayer fLayer = null;&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 23 Dec 2021 06:08:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128563#M7533</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2021-12-23T06:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a FeatureLayer with QueuedTask.Run</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128627#M7534</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/42133"&gt;@GKmieliauskas&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I did not explain my situaton well. I don't want it to be null. I would like to return the fLayer to main thread either as a new layer or a Layer from the TOC. So, I can make queries, extract data, table etc.&lt;/P&gt;&lt;P&gt;Maybe, I should declare the fLayer as public and assign it after checking its existence.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2021 15:58:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128627#M7534</guid>
      <dc:creator>Amadeus111</dc:creator>
      <dc:date>2021-12-23T15:58:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a FeatureLayer with QueuedTask.Run</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128663#M7536</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code is fine. But compiler thinks that there is one situation when your method AddLayerToTOC could&amp;nbsp; return unassigned fLayer value. For example if your foreach cycle will not be executed. This is compiler problem, not yours.&amp;nbsp; And workaround for this is to preassign fLayer value with null in line 7.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Dec 2021 16:35:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128663#M7536</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2021-12-23T16:35:19Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a FeatureLayer with QueuedTask.Run</title>
      <link>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128916#M7540</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/42133"&gt;@GKmieliauskas&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for the solution and detailed explanation, it worked very well. This will help me to reduce lotta repetition.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Dec 2021 12:31:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-sdk-questions/how-to-return-a-featurelayer-with-queuedtask-run/m-p/1128916#M7540</guid>
      <dc:creator>Amadeus111</dc:creator>
      <dc:date>2021-12-27T12:31:52Z</dc:date>
    </item>
  </channel>
</rss>

