Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an anonymous type in a try catch c#

Tags:

c#

I have struggled with this for years and normally just code my way around it, but it is time to solve it.

I am declaring a var that returns a new anon type and want to put it in a try/catch. However, doing that means it is out of scope and cannot be seen by later code obviously. Normally I just declare it first, then wrap the code in the try/catch then reassign inside of it like:

int result = 0;

try
{
    result = 77; //real code goes here
}
catch (Exception)
{
    throw;
}

But here is my real code that I cannot figure out how to do something like that:


    try
    {
        var dt_stop = (from s in cDb.DistributionStopInformations
                       join r in cDb.DistributionRouteHeaders on s.RouteCode equals r.RouteCode
                       where r.RouteDate == s.RouteDate &&
                       r.BranchId == s.BranchId &&
                       (r.CompanyNo == companyNo && s.CompanyNo == companyNo)
                       && s.UniqueIdNo == uniqueId

                       select new
                       {
                           s,
                           r
                       }).Single();
    }
    catch (Exception)
    { //no this will not be blank
        throw;
    }

UPDATE: I do use dt_stop extensively after this, I am wanting to catch if there is a problem with it is assigned data.

I created the following class:

 public class StopData
{
    public DistributionStopInformation S { get; set; }

    public DistributionRouteHeader R { get; set; }
}

Then I attempt to use is like:

 StopData dt_stop = null;

        try
        {
            dt_stop = (from S in cDb.DistributionStopInformations
                       join R in cDb.DistributionRouteHeaders on S.RouteCode equals R.RouteCode
                       where R.RouteDate == S.RouteDate &&
                       R.BranchId == S.BranchId &&
                       (R.CompanyNo == companyNo && S.CompanyNo == companyNo)
                       && S.UniqueIdNo == uniqueId

                       select new StopData
                       {
                           S,
                           R
                       }).Single();

        }
        catch (Exception)
        {
            //YES....THERE WILL BE CODE HERE
            throw;
        }

I am getting Cannot initialize type 'StopData' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

like image 890
Joe Ruder Avatar asked Jan 27 '23 02:01

Joe Ruder


2 Answers

Anonymous types are syntactic sugar to avoid you to name them. Anonymous types are used by the compiler to let you focus on what you want the program do.

For this reason, if you end up on referencing an anonymous type, it means that it is no longer anonymous:) Just give it a name and your issue goes away:

MyType dt_stop = null;
try
{
   dt_stop = (from s in cDb.DistributionStopInformations
                  join r in cDb.DistributionRouteHeaders on s.RouteCode equals r.RouteCode
                  where r.RouteDate == s.RouteDate &&
                  r.BranchId == s.BranchId &&
                  (r.CompanyNo == companyNo && s.CompanyNo == companyNo)
                  && s.UniqueIdNo == uniqueId

                  select new MyType
                  {
                      s,
                      r
                  }).Single();
}
catch (Exception)
{
   // here dt_stop can be used
   throw;
}

MyType can be a System.Tuple or a standard class. To preserve your semantics, you can make it a DTO (fill in your types as I cannot infer them from your source):

internal sealed class MyType
{
    public <The type of s> S {get; set;}
    public <The type of r> R {get; set;}
}
like image 155
Yennefer Avatar answered Feb 07 '23 17:02

Yennefer


You can declare a default instance of your anonymous type like:

var temp = new {A = default(int), B = default(int)};
try
{
    temp = new  {A= 1, B=2};
}
catch (Exception)
{
}
like image 45
Bruno Belmondo Avatar answered Feb 07 '23 17:02

Bruno Belmondo