C# constructor overload trouble

Hi there,
I know this is a totally Rhino-free question, but I need some help here.
I am trying to create a heatmap from int values or string values.

Therefore I am creating a class with 2 constructors that then “fire” an initializer.
When I use the the first constructor Heatmap(List<System.Guid> objs, List<int> values) everything is fine. Yet the other Heatmap(List<System.Guid> objs, List<string> criteria) is resulting in nothing at all. Even breakpoints are not indicating anything, the object simply is not initialized.

Any ideas?
Thanks,
T.

    public Heatmap(List<System.Guid> objs, List<int> values)
    {
        Start(objs, values);
    }

    public Heatmap(List<System.Guid> objs, List<string> criteria)
    {
        List<string> set = criteria.Distinct().ToList();
        List<int> result = criteria.Select(x => set.IndexOf(x)).ToList();

        Start(objs, result);
    }

    public void Start(List<System.Guid> ids, List<int> values)
    {
         // ...
    }

Hi Tobias.

… No C# expert here :blush:
But your code seems to work fine.

using System;
using System.Collections.Generic;
using System.Linq;

class Heatmap
{
  public Heatmap(List<System.Guid> objs, List<int> values)
  {
    Start(objs, values);
  }

  public Heatmap(List<System.Guid> objs, List<string> criteria)
  {
    List<string> set = criteria.Distinct().ToList();
    List<int> result = criteria.Select(x => set.IndexOf(x)).ToList();
    Start(objs, result);
  }

  public void Start(List<System.Guid> ids, List<int> values)
  {
    for( int i = 0; i < 3; i++ )
      Console.WriteLine( $"Guid {ids[ i ]}, Int {values[ i ]}" );
  }
}

class main
{
  static void Main()
  {
    var g = new List<System.Guid>();
    g.Add( Guid.NewGuid() );  
    g.Add( Guid.NewGuid() );  
    g.Add( Guid.NewGuid() );  
    var i = new List<int> { 11, 22, 33 };
    var s = new List<string> { "AA", "BB", "CC" };
    Console.WriteLine( "(0)" );
    var h1 = new Heatmap( g, i );
    Console.WriteLine( "(1)" );
    var h2 = new Heatmap( g, s );
    Console.WriteLine( "(2)" );
  }
}

Output:

c:\mono>tob1
(0)
Guid 9a477e86-7b57-4dd8-b5b8-507053f73048, Int 11
Guid 0d14639f-93e1-4610-913a-74c76e2af997, Int 22
Guid 6ae552c4-b2a2-4aea-9263-e8aaa0bbfb68, Int 33
(1)
Guid 9a477e86-7b57-4dd8-b5b8-507053f73048, Int 0
Guid 0d14639f-93e1-4610-913a-74c76e2af997, Int 1
Guid 6ae552c4-b2a2-4aea-9263-e8aaa0bbfb68, Int 2
(2)

c:\mono>

… Maybe something in the Start() method ?
Or in the data you’re using to initialize ? :confused:

Hi @emilio,
thanks.

I tried to call the first constructor and did what Start() does from outside the class.
It works perfectly fine.

Very strange!

1 Like