question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Constructor called 10 times when converting

See original GitHub issue

Why this code:

public class Program
    {

        static int Main(string[] args)
        {
            var verbs = typeof(Program).Assembly.DefinedTypes
                .Where(t => t.CustomAttributes.Any(a => a.AttributeType == typeof(VerbAttribute)))
                .Select(ti => ti.AsType())
                .ToArray();
            var resultCode = Parser.Default.ParseArguments(args, verbs).MapResult(
                (IVerb verb) => verb.Execute(),
                errs => 1
            );

            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();

            return resultCode;
        }
    }

    [Verb("testWeeklySchedule", HelpText = "Test using some fake data.")]
    internal class TestWeeklySchedule : IVerb
    {
        public TestWeeklySchedule(TimeOfDay startTime, IEnumerable<DayOfWeek> daysOfWeek)
        {
            //StartTime = TimeSpan.Parse(startTime);
            StartTime = startTime;
            DaysOfWeek = daysOfWeek;
        }

        [Option(HelpText = "Time to start event.")]
        public TimeOfDay StartTime { get; }

        [Option(Required = true, HelpText = "Days to execute event.", Min = 1)]
        public IEnumerable<DayOfWeek> DaysOfWeek { get; }

        public int Execute()
        {
            Console.WriteLine("Hello world with schedule " + JsonConvert.SerializeObject(this));
            return 0;
        }
    }

    internal class TimeOfDay
    {
        public static int timesCalled = 0;
        public TimeOfDay(string timeAsString)
        {
            timesCalled++;
            Console.WriteLine("Constructor called with " + timeAsString);
            Time = TimeSpan.Parse(timeAsString);
        }

        public TimeSpan Time { get; }
    }

Calls the constructor 10 times?!?!?!?!?!?!?! The output is

Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Constructor called with 00:00 Hello world with schedule {“StartTime”:{“Time”:“00:00:00”},“DaysOfWeek”:[1]} Press any key to exit…

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
moh-hassancommented, May 27, 2020

Tested with v2.4.3+ and Constructor is called only once.

0reactions
antonioortizpolacommented, Mar 20, 2018

Sure, it is just what we are doing, just simple and quick converters that do not cause trouble if they are called some extra times and just at the beginning of the app, however, is nice to see the root of the problem, i could not make the time to check the source so thanks a lot for the clarification.

I know is too radical to change the code to avoid this, but maybe a note in the documentation to warn the newcomers to be careful about this could be great.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructors, C++ FAQ
Which constructor gets called when I create an array of Fred objects? ... int main(); {; Fred a[10]; // Calls the default constructor...
Read more >
What is conversion constructor in C++?
A conversion constructor is a single-parameter constructor that is declared without the function specifier explicitly.
Read more >
c++ - Number of times a constructor is called
In this case as well, I thought 2 constructor calls should be made - converting constructor call for CustomStr("Some char pointer") and copy ......
Read more >
Conversion constructors (C++ only)
A conversion constructor is a single-parameter constructor that is declared without the function specifier explicit . The compiler uses conversion ...
Read more >
9.6. Constructors And Initializer Lists
Programs call a constructor whenever they instantiate an object. ... This constructor converts an integer into an instance of Time (i.e., into a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found