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.

Property value "get" returns 0 even though there's a value

See original GitHub issue

issue-blazor wasm

This happens on the WASM in a blazor project. When in debug and hovering on c.Id you can see there is a value, but when you assign it to a variable it assigns the value 0. This causes the ToDictionary function to fail because of duplicate keys (0) while there is no Id with a value of 0 in the collection.

When I am in debug mode and I hover over c.Id, it shows the id, when I assign it to a variable and I hover over that variable it shows 0. How is that even possible?

Here’s the simplified version of the code that fails private void CollectionToDictionary<T>(List<T> collection) where T : IUniqueResource { var dictionary = collection.ToDictionary(t => t.Id, t => t); return dictionary; }

Here’s the simplified code of the class implementing : IUniqueResource ` public interface IUniqueResource { public long Id { get; } }

public class BaseDealerLocation :  IUniqueResource
{
        public long Id { get; set; }
}

public class DealerLocation : BaseDealerLocation
{}

public class GameTable : DealerLocation
{}

`

Here’s a simplified sample of what the data being passed to the method looks like: var gameTables = new List<GameTable> { new GameTable { Id = 1 }, new GameTable { Id = 2 }, new GameTable { Id = 3 }, new GameTable { Id = 4 }, new GameTable { Id = 5 }, };

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
javiercncommented, Feb 17, 2021

How do you explain when you are in debug mode and you evaluate the Id in the immediate window or by hovering you get the proper value, but when you try to assign it to a variable it assigns 0?

That might be a bug in the debugger/inmediate window where it’s showing the field on the base class instead of the value the runtime produces (that’s why the differences happen, but TBH this is a C# question, so don’t take me as an authoritative source 😃)

That said I did get a warning, but I’d expect to get an error if it’s going to be causing bugs such as that.

This has been historically the C# behavior. WarningsAsErrors is always an option here. In general it’s discouraged to hide members unless you truly know what you are doing.

What was being passed to the method was the derived class so in that case I would expect to get the value from the derived class since it “replaces” the base class version. At least that’s what normally happens with polymorphism.

That’s not how hiding works. Your property is not virtual, and the binding to get_Id() is done at compile time based on the compile time type. Your generic method is calling IUniqueResource.Id which is implemented by BaseType

1reaction
mkArtakMSFTcommented, Feb 16, 2021

Thanks @danypellerin. We’ll wait for you to share a small repro project so that we can move forward with this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

My get method is returning 0 even though it has a value
I see you use getInputTime() in two places: inside onTextChanged() it is preceded by setInputTime() so it should has value there ...
Read more >
Vlookup returning zero where there's a value
Formula VLOOKUP always picks up first find value from your database. As you stated, there are duplicate values in which few are zeros....
Read more >
Solutions When Your Excel Lookup Returns 0 | Excelchat
Excel VLOOKUP retrieves 0 value for the empty cells in the lookup array. To get an empty string as a lookup result we...
Read more >
Display or hide zero values
To display zero (0) values as blank cells, uncheck the Show a zero in cells that have zero value check box. Hide zero...
Read more >
Object.defineProperty() - JavaScript - MDN Web Docs - Mozilla
Return value. The object that was passed to the function, with the specified property added or modified. Description. Object ...
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