Click here to Skip to main content
14,547,127 members
Rate this:
Please Sign up or sign in to vote.
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:
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:

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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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

public enum TrainingStatus
      {
          Paid,
          Unpaid,
          Reserved
      }


Try the below.

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;
        }
    }
   
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 :-)
Bruno2049 26-May-11 15:26pm
   
ok
Bruno2049 26-May-11 20:04pm
   
I'm doing the work, not for me but for someone else and would rather use the codes that I know best, which are arrays, but do not know if the method is equal to that used in the listings.

Could you get me this question?

and if not too much to ask, could you tell me how do I present this information to the training of a respective customer in the main function?

even now, thank you for having responded to my question soon :)
Rob Branaghan 27-May-11 4:06am
   
Well basically using arrays is like owning a Ford. Using lists is like owning a Bentley!
Lists offer more power (in my eyes, and if someone believes otherwise so be it).
yesotaso 27-May-11 5:25am
   
First of all, like Rob said both Array and List can carry you to the destination, but Arrays require little more maintainance :) It is always good to use whatever you are familiar, but also far more better to widen familiarity circle. Anyway among many other, most striking difference is you can expand list on the fly... For the presentation Rob already given the code for that.
Rob Branaghan 27-May-11 5:26am
   
:) Thank you :)
Bruno2049 27-May-11 6:35am
   
ok, thank you :)
Rob Branaghan 27-May-11 6:42am
   
Your welcome.
Good luck :)
Rate this:
Please Sign up or sign in to vote.

Solution 2

I think public variable declaration in the class is not a good coding practice so here i'm using Properties[^] and Interfaces.[^]

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;
           }
       }
     
   }


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));
        }
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100