"does not contain a definition...and no extension method.." error

41,752

Solution 1

The problem is this part: cart.biff. cart is of type Dictionary<int, SpoofClass>, not of type SpoofClass.

I can only guess what you are trying to do, but the following code compiles:

Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
int i=0;
foreach (var item in dbContext.DBView)
{
    cart.Add(i, new SpoofClass { biff = item.biff });
    ++i;
}

Solution 2

You need to access the Value of the Dictionary for a given key. Something along these lines.

foreach(var item in dbContext.DBView)
{
    foreach(var key in cart.Keys)
    {
        cart[key].biff = item.biff;
    }
}
Share:
41,752
AmbiguousX
Author by

AmbiguousX

I am a beginner programmer, currently working on a bachelor's degree in Computer Science with a Management Minor. I don't really have an "expertise" in any particular language(s), being as I am still a beginner. However, of the languages, I do have the most experience with C++, C#, and (even though it is not a very "up-to-date" language) Ada. I love learning new things and solving various problems/puzzles.

Updated on August 25, 2020

Comments

  • AmbiguousX
    AmbiguousX almost 4 years

    I have the following error message:

    'System.Collections.Generic.Dictionary<int,SpoofClass>' does not
    contain a definition for 'biff' and no extension method 'biff'
    accepting a first argument of type
    'System.Collections.Generic.Dictionary<int,SpoofClass>' could be found
    (are you missing a using directive or an assembly reference?)
    

    I checked SO for this and I found this question which seemed to have a similar (if not identical) problem as I am having. However, I tried the solution provided in the accepted answer and it still isn't coming up with anything. It's acting like I am missing a using statement, but I am almost positive I have all the using's I need.

    Here is some of the code that is producing the errors:

    using locationOfSpoofClass;
    ...
    
    Dictionary<int, SpoofClass> cart = new Dictionary<int, SpoofClass>();
    foreach (var item in dbContext.DBView)
    {
        cart.biff = item.biff;
        ...
    }
    

    SpoofClass file:

    namespace locationOfSpoofClass
    {
        public class SpoofClass
        {
            public int biff { get; set; }
            ...
        }
    }
    

    Sorry if my renaming of variables and whatnot is confusing. If it is unreadable, or too hard to follow, or if other information is pertinent to the solution, please let me know. Thanks!

  • Ben Robinson
    Ben Robinson almost 13 years
    That code would not compile as cart[key] has a type of SpoofClass whereas item.biff is an int.
  • Daniel Hilgarth
    Daniel Hilgarth almost 13 years
    @Dylan: Now your code compiles but it makes no sense :) After the loops all entries in the dictionary will have the biff value of the last item in the DBView.
  • Ben Robinson
    Ben Robinson almost 13 years
    @Daniel, whilst you are correct it sort of looks like that is what the OP wants to do ;-)