17

I am making a class Customer that has the following data members and properties:

private string customerName;
private double[] totalPurchasesLastThreeDays; //array of 3 elements that will hold the totals of how much the customer purchased for the past three days i.e. element[0] = 100, element[1] = 50, element[2] = 250

public string CustomerName
{
get { return customerName; }
set { customerName = value; }
}

public double[] TotalPurchasesLastThreeDays
{
?
}

How do I define the get and set for the array data member?

Bob
  • 1,355
  • 5
  • 19
  • 38

9 Answers9

37

Do you want an indexer?

public double this[int i] {
  get { return totalPurchasesLastThreeDays[i]; }
  set { totalPurchasesLastThreeDays[i] = value; }
}

Because otherwise the question sounds a little weird, given that you already implemented a property in your code and are obviously capable of doing so.

Ashitaka
  • 19,028
  • 6
  • 54
  • 69
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 4
    Indexers have no name, because you index the object directly. So you should remove `Purchases` to make your code compiling. In fact, indexers work like this: `myCustomer[index]`, not in this way: `myCustomer.Purchases[index]` – digEmAll Mar 17 '12 at 17:10
  • Feel free to edit. I have to look up the syntax every time anyway. – Joey Mar 17 '12 at 18:05
  • So what if we have two or more arrays and need this for all of them? – Rafalon Aug 09 '18 at 13:30
16

You can use an auto-property:

public class Customer
{
    public string CustomerName { get; set; }

    public double[] TotalPurchasesLastThreeDays { get; set; }
}

Or if you want:

public class Customer
    {
        private double[] totalPurchasesLastThreeDays; 

        public string CustomerName { get; set; }

        public double[] TotalPurchasesLastThreeDays
        {
            get
            {
                return totalPurchasesLastThreeDays;
            }
            set
            {
                totalPurchasesLastThreeDays = value;
            }
        }
    }

And then in the constructor, you can set some default values:

public Customer()
{
    totalPurchasesLastThreeDays = new double[] { 100, 50, 250 };
}
Jason Evans
  • 28,906
  • 14
  • 90
  • 154
4

You might be asking yourself this question because you think that assigning to your array is different than a regular variable?

In that case, you have to realize that when you call TotalPurchasesLastThreeDays[3] = 14.0 you are actually using the getter and not the setter. The setter is used to change the array itself and not the values it contains. So coding the getter and setter isn't any different with an array than with any other variable type.

dee-see
  • 23,668
  • 5
  • 58
  • 91
3

If you want to allow this:

myCustomer.TotalPurchasesLastThreeDays[2] = 3.1415;
var foo = myCustomer.TotalPurchasesLastThreeDays[1];

but not this:

myCustomer.TotalPurchasesLastThreeDays = new Customer[]{ ... };

Define only the getter i.e. :

public double[] TotalPurchasesLastThreeDays 
{
   get { return this.totalPurchasesLastThreeDays; }
}

Instead, if you need to change the array instance from outside the class, simply follow the other answers.

digEmAll
  • 56,430
  • 9
  • 115
  • 140
2
public int this [int varialbeindex]
{
    get
    {
        return totalpurchaseslastdays[index];
    }
    set
    {
        totalpurchaseslastdays[index]=value;
    }
}
EstevaoLuis
  • 2,422
  • 7
  • 33
  • 40
nidhin
  • 21
  • 1
  • This feels really strange, I would be quite surprised if this code would compile – Rafalon Aug 09 '18 at 10:44
  • @Rafalon change `variableIndex` to `index` and it will compile fine. This is one of the few answers that protects the underlying array from being changed to other lengths, in case you want to enforce a fixed length of 3 that was mentioned int he question. – hultqvist Aug 09 '18 at 13:06
2

If you want the array to be settable from the outside anyway, the most convenient is to use an auto property and just remove the private field you already have:

public double[] TotalPurchasesLastThreeDays { get; set; }
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
2

One way would be this, (I'm assuming the point of this is so that the array can no longer be modified after setting):

public double[] TotalPurchasesLastThreeDays
{
    get {
        return totalPurchasesLastThreeDays;
    }
    set {
        totalPurchasesLastThreeDays = (double[])value.Clone();
    }
}

But... do you really want to do that? It may be more convenient and intuitive to just do the same thing as you would usually.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1
public double[] TotalPurchasesLastThreeDays
{
get
{
    return totalPurchasesLastThreeDays;
}
set
{
    totalPurchasesLastThreeDays = value;
}
}
ionden
  • 12,536
  • 1
  • 45
  • 37
1
public double[] TotalPurchasesLastThreeDays
{
 get

  {
    return totalPurchasesLastThreeDays;
  }

set

{
 totalPurchasesLastThreeDays=value;
}
}
Royi Namir
  • 144,742
  • 138
  • 468
  • 792