cannot initialize type with a collection initializer because it does not implement ienumerable

user3679123

I'm a fairly junior C# developer so please excuse me if this is very easy, but I am getting this error "invalid initializer member declarator" at this line of the code below:

foreach (DataRow p in data.Rows)
{
  DBTrack top = new DBTrack()
 /* ===> Error starts here */   {
     track.TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID"); 
     track.TrackName = SQLDataHelper.GetString(dataReader, "TrackName");
     track.ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName");
     track.AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate");
    };

 DBTrackData.Add(top);
}

Can someone explain what this means and how do I work around and achieve it to display the data?

Here's the full method for your inspection:

 public static List<DBTrack> GetAllTracksFromReaderDB(IDataReader dataReader)
 {
  if (DBTrackData == null)
  {
     DBTrack track = new DBTrack();
     System.Data.DataTable data = new System.Data.DataTable();
     List<DBTrack> daa = new List<DBTrack>();
     DBTrackData = new List<DBTrack>();

     foreach (DataRow p in data.Rows)
     {

     DBTrack top = new DBTrack()
     /* ===> Error starts here */   {
     track.TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID"); 
     track.TrackName = SQLDataHelper.GetString(dataReader, "TrackName");
     track.ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName");
     track.AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate");
     };

    DBTrackData.Add(top);

    }
  }
  return DBTrackData;

 }

EDIT:

public Guid TrackID { get; set; }
public string TrackName { get; set; }
public string ArtistName { get; set; }
public DateTime AddedDate { get; set; }

Any help would be great :) Thanks

Yuval Itzchakov

Your syntax is invalid for member initialization. You cant instansiate top and try to fill track properties.

It should look like this:

DBTrack top = new DBTrack
{
   TrackID = SQLDataHelper.GetGuid(dataReader, "TrackID"),
   TrackName = SQLDataHelper.GetString(dataReader, "TrackName"),
   ArtistName = SQLDataHelper.GetString(dataReader, "ArtistName"),
   AddedDate = SQLDataHelper.GetDateTime(dataReader, "AddedDate")
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot initialize type "x" with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

Cannot initialize type x with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable'

From Dev

cannot initialize type with a collection initializer

From Dev

'Cannot initialize type with a collection initializer' while initializing object of my class

From Dev

Cannot initialize QQueue with initializer list

From Dev

Cannot find an initializer for type

From Dev

Error setting Database Initializer: Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'

From Dev

IEnumerator does not implement interface IEnumerable

From Dev

Initialize get-only collection in object initializer from existing collection

From Dev

Cannot initialize type 'HttpClientHandler'

From Dev

Cannot invoke initializer for type 'UnsafeMutablePointer'

From Dev

Cannot implicitly convert type from System.Collection.Generic.IEnumerable.MyClass<Node> to MyClass<Node>

From Dev

cannot implicitly convert type system.collection.generic IEnumerable to models in group by linq c#

From Dev

Cannot implicitly convert type system.Data.EnumerableRowCollectio to System.collection.Generic.Ienumerable

From Dev

When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator

From Dev

Cannot return type IQueryable or IEnumerable

From Dev

Initialize the Attributes property of a WebControl object using collection initializer

From Dev

Cannot invoke initializer for type: with an argument list of type:

From Dev

Cannot implement an interface member because it is not public

From Dev

"..." cannot implement an interface member because it is not public

From Dev

IEnumerable Class Collection type conversion from List

From Dev

Does calling Count on IEnumerable iterate the whole collection?

From Dev

Does calling Count on IEnumerable iterate the whole collection?

From Dev

foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'

From Dev

Cannot serialize type 'TickDataDefinition.data' because it does not have any serializable fields nor properties

Related Related

  1. 1

    Cannot initialize type "x" with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  2. 2

    Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  3. 3

    Cannot initialize type x with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  4. 4

    Cannot initialize type 'PeopleModel' with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  5. 5

    Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  6. 6

    Cannot implement type with a collection initializer because it does not implement 'System.Collections.IEnumerable'

  7. 7

    cannot initialize type with a collection initializer

  8. 8

    'Cannot initialize type with a collection initializer' while initializing object of my class

  9. 9

    Cannot initialize QQueue with initializer list

  10. 10

    Cannot find an initializer for type

  11. 11

    Error setting Database Initializer: Type argument 'Configuration' does not inherit from or implement the constraint type 'DbContext'

  12. 12

    IEnumerator does not implement interface IEnumerable

  13. 13

    Initialize get-only collection in object initializer from existing collection

  14. 14

    Cannot initialize type 'HttpClientHandler'

  15. 15

    Cannot invoke initializer for type 'UnsafeMutablePointer'

  16. 16

    Cannot implicitly convert type from System.Collection.Generic.IEnumerable.MyClass<Node> to MyClass<Node>

  17. 17

    cannot implicitly convert type system.collection.generic IEnumerable to models in group by linq c#

  18. 18

    Cannot implicitly convert type system.Data.EnumerableRowCollectio to System.collection.Generic.Ienumerable

  19. 19

    When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator

  20. 20

    Cannot return type IQueryable or IEnumerable

  21. 21

    Initialize the Attributes property of a WebControl object using collection initializer

  22. 22

    Cannot invoke initializer for type: with an argument list of type:

  23. 23

    Cannot implement an interface member because it is not public

  24. 24

    "..." cannot implement an interface member because it is not public

  25. 25

    IEnumerable Class Collection type conversion from List

  26. 26

    Does calling Count on IEnumerable iterate the whole collection?

  27. 27

    Does calling Count on IEnumerable iterate the whole collection?

  28. 28

    foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator'

  29. 29

    Cannot serialize type 'TickDataDefinition.data' because it does not have any serializable fields nor properties

HotTag

Archive