0

When I execute these code lines

var lengths = new CPhqLength[]{new CPhqLength(5, EnumUnitLength.Meter)};
lengths.First().ToString(6)

in the immediate window while the execution of my program is interrupted, I get

Expression has been evaluated and has no value

from the first line and

"5,000000 m"

from the second line.

But when I use the LINQ method Select(), an exception is thrown somewhere and it is stored as an object in the returned collection:

?lengths.Select (l => l.ToString (6));
{System.Linq.Enumerable.WhereSelectArrayIterator<Schmoll.SwCore.CPhqLength, string>}
    Data: {System.Collections.ListDictionaryInternal}
    HResult: -2147024809
    HelpLink: null
    InnerException: null
    Message: "mc_dictUnitInfoDictionary:  The enum value Meter is not defined in the enum Schmoll.SwCore.EnumUnitLength."
    ParamName: null
    Source: "System.Core"
    StackTrace: "   at System.Linq.Enumerable.WhereSelectArrayIterator`2[[Schmoll.SwCore.CPhqLength, schmoll_swcore_basics, Version=0.24.9000.0, Culture=neutral, PublicKeyToken=f8a6c8bb21c5d4a1],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].MoveNext()\r\n   at System.Linq.SystemCore_EnumerableDebugView`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].get_Items()"
    TargetSite: {Boolean MoveNext()}

and I have no chance to debug it in any way. Every breakpoint I set is ignored. No exception settings are respected.
This is a simplified example, but it produces the same failure.
mc_dictUnitInfoDictionary is a static readonly ReadOnlyDictionary, and it definitly is initialized (because it's used) by the program, which is interrupted when executing the failing line in the immediate window. The keys of that dictionary are of System.Type, and

public static CUnitInfo GetInfo<TUnit> (TUnit i_unit)
{
  var enumType = i_unit.GetType ();
  if (!mc_dictUnitInfoDictionary.ContainsKey (enumType))
    throw CException.Create_EnumUndefined (nameof (mc_dictUnitInfoDictionary) + ": ", i_unit, null);

is used to access the keys.
The exception above is an ArgumentException which my code throws if an entry is not found in the dictionary.

How can I find out what is going wrong here?
The same line executes fine if written inside a program.

I am using VS 2019 (v.16.5.5) on Win 10 x64. Compilation for x86 or x64 does not make a difference.

Tobias Knauss
  • 3,361
  • 1
  • 21
  • 45
  • Why have you not updated to the latest version of Visual Studio 2019? – Ian Kemp Oct 29 '20 at 10:01
  • @IanKemp: Because we need to update offline on our business computers, which actually is almost impossible: https://developercommunity.visualstudio.com/content/problem/1034990/visual-studio-offline-installation-and-update-suck.html Beside that, I don't think it would make a difference. – Tobias Knauss Oct 29 '20 at 10:07
  • What does the `ToString` method on your `CPhqLength` class do? Why do you pass `6` in as the argument? It could be that this is what's raising the exception. – ChrisF Jan 27 '21 at 10:32
  • @ChrisF: That `ToString(int)` is like `ToString("F6")` and adds the unit text. And this method actually **is** the problem. It throws if executed via immediate window, but works well if executed in the code. And I don't understand why, because this method had been executed without problems before I ran it in the immediate window, therefore I expect it to work there, too. – Tobias Knauss Jan 27 '21 at 13:39

0 Answers0