-3

I have this code in my gymInfo class:

public class gymInfo
{
   public gymArray getAllGymsByStars()
   {
        gymDataTableAdapters.gymsTableAdapter gymsTableAdapter = new gymDataTableAdapters.gymsTableAdapter();
        DataTable gymsDataTable = gymsTableAdapter.getAllGymsByStars();
        gymArray gymArray = new gymArray();

        foreach(DataRow row in gymsDataTable.Rows)
        {
            gymArray.name = row["name"].ToString();        
        }
        return gymArray;
    }
}

The gymsDataTable contains 20 rows of names, now I have made a custom class, here is the code:

public class gymArray
{
  public string name { get; set; }
}

Which I expected the string name to get populated with 20 names (as their is 20 rows in the datatable) however when I debug on line return gymArray it only shows the name of the last row rather than showing a whole custom array of names.

Does anyone understand what I'm doing wrong?

MethodMan
  • 18,625
  • 6
  • 34
  • 52
mogorilla
  • 195
  • 2
  • 13

5 Answers5

2

You haven't actually created an array. You've got a single gymArray object, and you're just changing its name property repeatedly.

Also, you're not following C# conventions when naming things. Your code should be more like this:

public class GymInfo
{
   public List<Gym> GetAllGymsByStars()
   {
        gymDataTableAdapters.gymsTableAdapter gymsTableAdapter = new gymDataTableAdapters.gymsTableAdapter();
        DataTable gymsDataTable = gymsTableAdapter.getAllGymsByStars();    
        List<Gym> gyms = new List<Gym>();    
        foreach(DataRow row in gymsDataTable.Rows)
        {
            Gym gym = new Gym();
            gym.Name = row["name"].ToString();
            gyms.Add(gym);      
        }
        return gyms;
    }
}

public class Gym
{
     public string Name { get; set; }
}

Instead of a naming a single object an "array", I created a generic List of Gym objects and returned that from the GetAllGymsByStars method.

mason
  • 31,774
  • 10
  • 77
  • 121
0
public class gymArray
{
  public List<string> names { get; set; }
}

public gymArray getAllGymsByStars()
{
  gymDataTableAdapters.gymsTableAdapter gymsTableAdapter = new gymDataTableAdapters.gymsTableAdapter();
  DataTable gymsDataTable = gymsTableAdapter.getAllGymsByStars();
  gymArray gymArray = new gymArray();
  gymArray.names = new List<string>();

  foreach(DataRow row in gymsDataTable.Rows)
  {
     gymArray.names.Add(row["name"].ToString());        
  }
  return gymArray;
}
Hogan
  • 69,564
  • 10
  • 76
  • 117
0

I don't know how do you expect to store all the names as you each time overwrite the name property in the foreach loop

The loop is just doing for you something like:

gymArray.name = "some name";
gymArray.name = "other name";

Which, I guess, you are totally aware that the last value of name will be other name. You either append to the last value with + operator which will result in a big string of names, or change the name property to some collection and add new names. Maybe string is not your preferred type.

Royal Bg
  • 6,988
  • 1
  • 18
  • 24
0

I think you should implement as

List<gymArray> gymArray = new List<gymArray>();

and in the loop.

gymArray.Add(new gymAray { name : row["name"].ToString() });

0

Well, you are setting the property of the same object again and again.

Try this

public class gymInfo
{
   public gymArray[] getAllGymsByStars()
   {
       int count = 0;
        gymDataTableAdapters.gymsTableAdapter gymsTableAdapter = new gymDataTableAdapters.gymsTableAdapter();
        DataTable gymsDataTable = gymsTableAdapter.getAllGymsByStars();
        gymArray[] gymArray = new gymArray[gymsDataTable.Rows.Count];

        foreach(DataRow row in gymsDataTable.Rows)
        {
            gymArray[count] = new gymArray();
            gymArray[count].name = row["name"].ToString();
            count++;
        }
        return gymArray;
    }
}
Rahul Chakrabarty
  • 2,149
  • 7
  • 39
  • 70