2

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

9
  • How is DBTrack defined? Jun 10, 2014 at 13:03
  • thanks - it's defined as public Jun 10, 2014 at 13:05
  • No, not the access modifier, how is the class defined? show us the class so we can see its properties. Jun 10, 2014 at 13:07
  • thanks - would this help? public static List<DBTrack> DBTrackData = null; sorry if I misunderstood it :) Jun 10, 2014 at 13:07
  • 1
    change semicolons to just commas ; => ,
    – user2140173
    Jun 10, 2014 at 13:11

1 Answer 1

4

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")
};
5
  • thanks for that, it resolved my issue. :) I minor question, how can I amend your code without the dataReader parameter.. is it possible to do so? thanks again for your help it's working :) Jun 10, 2014 at 13:19
  • You mean how you can copy it without the dataReader parameter? just copy it normally and delete the parameter Jun 10, 2014 at 13:21
  • thanks - I tried that initially, but I get the following error no overload for method takes 1 argument i.e. TrackID = NSMSQLDataHelper.GetGuid("TrackID"),.. only if it's possible. If not then it's ok ;) thanks again for your help Jun 10, 2014 at 13:26
  • Oh, i see what you mean now. Well, you cant drop the argument if the GetGuid method needs to accept it as a parameter. Jun 10, 2014 at 13:28
  • ahh I c what you mean! - thanks for that and for all your help :) Jun 10, 2014 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.