Why is my C# template method complaining about accepting a specific type? - asp.net-mvc-2

I'm diving into ASP.NET MVC 2 and I'm walking through a tutorial and I'm getting an error related to a template method in my unit tests. The erroneous code is...
var displayedProducts = (IList<Product>)result.ViewData.Model;
displayedProducts.Count.ShouldEqual(2);
and the method definition for ShouldEqual is...
public static void ShouldEqual<T>(this T actualValue, T expectedValue)
{
Assert.AreEqual(expectedValue, actualValue);
}
and the error is...
'int' does not contain a definition for 'ShouldEqual' and no extension method 'ShouldEqual' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)
but because I'm so new to all this, I don't see what I missing.
Does anyone see the problem?
Can someone explain to me how ShouldEqual is a member function of Count
Thanks so much for your help! If more code is needed, please let me know.

I recognize this... this is from the Steve Sanderson book?
I can reproduce this error if I comment out the using statement for the namespace where ShouldEqual is declared.
In the file where you have
var displayedProducts = (IList<Product>)result.ViewData.Model;
displayedProducts.Count.ShouldEqual(2);
can you check and make sure you have a using statement for the namespace that ShouldEqual is in?

ShouldEqual is compiler trick only. It's a stand alone function (static class (CLASSNAME) and method). The compiler replaces the call with...
CLASSNAME.ShouldEqual<int>(displayedProducts.Count, 2);
See here for more information : http://msdn.microsoft.com/en-us/library/bb383977.aspx

Related

Using flutter/dart vscode shows "method isn't defined" when i check for a specific mixin before calling it

This is my first time messing with dart and i'm stuck with this silly error. I'm 90% confident that the error lies on the vscode part of it, because there aren't errors showing when running the app.
Maybe I'm approaching the problem in the wrong way, I simply want to call a mixin function in objects that implements the mixin. In java for example it would be required to cast the component variable, but I couldn't get cast to work in this situation.
Code
Error
The method 'onPanUpdate' isn't defined for the class 'Component'.
Try correcting the name to the name of an existing method, or defining a method named 'onPanUpdate'.
Source Code
Repo link
Component class source
Maybe I'm approaching the problem in the wrong way, I simply want to call a mixin function in objects that implements the mixin. In java for example it would be required to cast the component variable, but I couldn't get cast to work in this situation.
For this to work it must be done with the following code:
for (var component in this.components) {
if (component is PanDetector) {
(component as PanDetector).onPanUpdate(details);
}
}
Especial thanks to #Moqbel

String is not a String

I can't grasp what's going on. There are two issues here.
"Can't convert type 'String' to expected type 'String'" //am I blind, I am checking this again and again... but those are both the same Strings. And this error disappear when I type in some hardcoded String.
Combination of errors at the top which clearly says: there can not be Strings used as Keys in Dictionaries and error at the bottom where UserDefaults clearly return type which is forbidden by compilator few lines above.
I don't even know how to ask clearly. But where to start? In second thing I can do some workarounds, some ugly hacks, but this "String" is not "String"... ehmm... maybe someone can point me to some place where someone had similar issue?
EDITED:
public class PersistentDictionary<String: Codable>
this is trouble. As someone pointed out I am creating new Generic Type which inherits from Codable. It's like . Not sure how that landed there - it was never my intention to make it generic, but...
In this very case it was declaration of class:
public class PersistentDictionary<String: Codable> {
//see comment which explain it in details

Swinject - Ambiguous reference to member

I am using Swinject in my Swift 3 app. When I tried
let container = Container()
container.register(NetworkModeling.self) { _ in Network() }
I get an error saying
Ambiguous reference to member 'register(_:name:factory:)'
What is wrong here?
I faced the same issue and i think compiler could be a bit more verbose in this case.
Anyway, my problem was on my side, not in Swinject
Check the following:
NetworkModeling and Network are visible in scope of your registration (they are public, or internal in the same module. remember, that swift3 introduced fileprivate and many other specifiers, so make sure your identifiers are visible to registeting code
Make sure that Network conforms to NetworkModeling. Being unable to see inheritance, swift compiler raises error about ambigous types for Swinject factory
Hope, this helps

Where is 'With' method defined. ReactiveUI

I saw here that we can use
(new TestScheduler()).With(sched =>{
// Code run in this block will have both RxApp.MainThreadScheduler
// and RxApp.TaskpoolScheduler assigned to the new TestScheduler.
});
But where is With method defined. I cant find any. Please help.
Microsoft.Reactive.Testing.TestScheduler does not contain a definition for With and no extension method With accepting a first argument of type Microsoft.Reactive.Testing.TestScheduler could be found
This is an extension method defined here and you need nuget package reactiveui-testing to have it available.

Strongly typed ViewModel contains unexpected null value

I have a case that is very similar to this, but following the advice in the answers does not solve my problem.
I have a ViewModel in an MVC 2 application that contains another class. I have a controller that contains a strongly typed create method:
[Authorize]
[HttpPost]
public ActionResult Create(AIViewModel ai)
{
}
When I look at the ModelState when I enter the Create method, the data indicates that the simple properties that are present within the AIViewModel class are bound correctly, while the complex type that is in there fails with the following error message:
"The parameter conversion from type 'System.String' to type 'xyz' failed because no type converter can convert between these types."
If I look at the value that it tries to bind, it has indeed the System.String type and value "Create". Anybody has a clue on what I could be doing wrong?
UPDATE: I have found the problem: The property is called Action, which somehow fools the the modelbinder. Renaming the property solved the issue.