Click here to Skip to main content
15,940,246 members
Please Sign up or sign in to vote.
4.00/5 (4 votes)
See more:
Hello everyone, I'm here embarrassed by a question in C # and is the next one I am developing a program to manage in a very simple one gym, but I need to store a two dimensional array information from the training that each client realizes the gym .

The information that I intend to keep are the training duration in minutes and the status of your payment, or 0 to keep this training has not been paid and save one if the training is paid.

I already have here the piece of code I've been doing to add workouts.

Class Tariff:
C#
namespace Test
{
    class Tariff : Training
    {
        public Training[,] Training = new Training[duration, status];

        public void get_duration(int g_d)
        {
            duration = g_d;
        }

        public void get_status(int g_s)
        {
            status = g_s;
        }

        public void Add_Training()
        {

        }
    }
}


Class Training:

C#
namespace Test
{
    public class Training
    {
        int duration;
        int status;

        public Training(int dur, int stat)
        {
            duration = dur;
            status = stat;
        }
    }
}


Someone solve this issue?
Thank you in to have the help they give me ;)
Posted
Comments
Rob Branaghan 26-May-11 11:48am    
I prefer to use Generic Lists these days with classes that reflect the object or item.

And is a hell of a lot easier to use
Yvan Rodrigues 26-May-11 19:09pm    
Don't put the cart before the horse. You don't want/need a multidimensional array here -- very 1987. The framework has many nice classes for managing collections such as arrays, lists, queues, stacks, etc so that you don't need to worry about how your objects are stored.

Spend your time on your business object and choose the most suitable collection from those available to you.

C#
public class Training
{
    public int duration;
    public int status;
    public Training()
    {
    }
    public Training(int dur, int stat)
    {
        duration = dur;
        status = stat;
    }
}



then you can do

C#
List<Training> Trainings = new List<Training>();
public void AddTraining(int dur, int stat)
{
   Trainings.Add(new Training(dur,stat));
}



You could change the status to be an enum such as

C#
public enum TrainingStatus
      {
          Paid,
          Unpaid,
          Reserved
      }


Try the below.

C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

   List<Training> Trainings = new List<Training>();
        public void AddTraining(int dur,  TrainingStatus stat)
        {
            Trainings.Add(new Training(dur, stat ));
        }

        private void button1_Click(object sender, EventArgs e)
        {
           AddTraining(27, TrainingStatus.Unpaid);
        }


    }
    public enum TrainingStatus
    {
        Paid = 0,
        Unpaid = 1,
        Reserve = 2
    }
    public class Training
    {
        public int duration;
        public TrainingStatus status;
        public Training()
        {
        }
        public Training(int dur, TrainingStatus stat)
        {
            duration = dur;
            status = stat;
        }
    }
 
Share this answer
 
v3
Comments
Bruno2049 26-May-11 12:05pm    
You can join the "paid" to the number 0 and "unpaid" to the number 1?

that is, when you say you do not want to pay just the workout, I save as 0 or 1 and then when the program save the workout, save as "paid" or "unpaid"?
Rob Branaghan 26-May-11 12:14pm    
Did that make sense for you to use the List<Training> as it is easier to add items to?
yesotaso 26-May-11 12:18pm    
I was thinking same "why 2D array" the problem itself isnt just sorting 2D array but the Tariff type itself is also fishy.
Rob Branaghan 26-May-11 12:20pm    
The inheritance of Training makes me wonder. But I think using the Generic List it will improve what they can do with the Data. (Cant get enough of Lists)
Kim Togo 26-May-11 13:35pm    
List<t> is kind the ultimate weapon of choice :-)
I think public variable declaration in the class is not a good coding practice so here i'm using Properties[^] and Interfaces.[^]

C#
public interface ITraining
   {
       int Duration
       {
           get;
           set;
       }
       Status TrainingStauts
       {
           get;
           set;
       }
   }


   public enum Status
   {
       Paid,
       Unpaid,
       Reserved
   }

   public class Training : ITraining
   {
       private int duration;
       private Status status;
       public Training(int dur, Status stat)
       {
           this.Duration = dur;
           this.TrainingStauts = stat;
       }
       

       public int Duration
       {
           get
           {
               return duration;
           }
           set
           {
               duration = value;
           }
       }
       public Status TrainingStauts
       {
           get
           {
               return status;
           }
            set
           {
               status = value;
           }
       }
     
   }


C#
private void button1_Click(object sender, EventArgs e)
        {
            addTraining(45, Status.Reserved);
            addTraining(24, Status.Paid);
        }

        private List<ITraining> trainingList = new List<ITraining>();
        private void addTraining(int duraion, Status stat)
        {
          trainingList.Add(new Training(duraion,stat));
        }
 
Share this answer
 
v2
Comments
parmar_punit 30-May-11 2:07am    
good answer, My 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900