Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug object initializer code?

Is there a way to step by step debug the object initializer code in Visual Studio?

Example:

return new Veranstaltung()             {                 ID = tblVeranstaltung.VeranstaltungsID,                 Titel = tblVeranstaltung.Titel,                 KursNummer = tblVeranstaltung.Kursnummer,                 ErsterTermin = tblVeranstaltung.ersterTermin,                 Dauer = tblVeranstaltung.schulungsTage,                 StartZeit = tblVeranstaltung.BeginnZeit,                 EndZeit = tblVeranstaltung.Endzeit,                 KostenNettoValue = tblVeranstaltung.PreisNetto ?? default(decimal),                 IsLastMinute = tblVeranstaltung.lastMinute == 1,                 IsVerkuerzt = tblVeranstaltung.istVerkuerzt == 1,                 IsGeschlossen = tblVeranstaltung.istGeschlosseneVeranstaltung == 1,                 IsIntern = tblVeranstaltung.istInterneVeranstaltung == 1,                 StandortID = Convert.ToInt32(tblVeranstaltung.StandortID),                 LastMinuteRabatt = tblVeranstaltung.lastMinuteRabatt ?? default(decimal)             }; 

Sometimes I get errors in this kind of code (for example when a conversion to int fails) and VS seems to be unable to step through it, it just throws an error for the whole line and I have to try out which of the initializations failed.

Is there an easy way to debug this or is it better to avoid the object initializer for large or complex initializiations?

I am using VS 2010 and C# 4.0.

like image 562
magnattic Avatar asked Mar 30 '11 16:03

magnattic


People also ask

What is an object initializer?

An object initializer is an expression that describes the initialization of an Object . Objects consist of properties, which are used to describe an object. The values of object properties can either contain primitive data types or other objects.

What is an object initializer in C#?

Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to invoke a constructor followed by lines of assignment statements.

What is difference between initializer and constructor?

A constructor is a defined method on a type which takes a specified number of parameters and is used to create and initialize an object. An object initializer is code that runs on an object after a constructor and can be used to succinctly set any number of fields on the object to specified values.


1 Answers

Object initializers should be kept for simple object initialization. If you are in the point where your object constructor has code that may fail (e.g. throwing an exception), don't use it. Better rely on an object construction pattern, which depending on your needs may be a factory method, an abstract factory,etc... This also ensures that all the users of your class cannot build an instance that is in an invalid state (e.g. they forget to initialize a member, or they initialize related members with incorrect values, etc...)

like image 114
Haplo Avatar answered Sep 23 '22 14:09

Haplo