1

is there a way to put these into either a 1 D array or a 2 D array. ? i have produced code and it looks a bit untidy as well as long can this be shortened?

 double worstPrice = 6.47;
 double bestPrice = 0.99;
 double CivetCatPrice =29.14;
 double whenPrice = 10.50;
 double everythingPrice = 319.56;
 int bestStock = 3238;
 int worstStock = 8;
 int civetCatstock = 3;
 int whenStock = 37;
 int everythingStock = 2;
asked Apr 29, 2016 at 13:23
2
  • 2
    What does this code have to do with arrays? It looks like what you want is a class, not an array. But exactly what that class conceptually represents and how to define it is really up to you. Commented Apr 29, 2016 at 13:28
  • 1
    I think your code looks fine: The variable name describes the content of the variable. I don't see any immediate need to change that. Commented Apr 29, 2016 at 13:34

3 Answers 3

1

You can make an array for each doubles and ints like this

double[] priceData = new double[]{ 6.47, 0.99, 29.14, 10.50, 319.56 };
int[] stockData = new int[]{ 3238, 8, 3, 37, 2 };

Alternatively you can use a dictionary if you wish for them to keep their names

Dictionary<string, double> priceDict = new Dictionary<string, double>();
priceDict.Add("worstPrice", 6.47);
//And so on for each double
Dictionary<string, int> stockDict = new Dictionary<string, int>();
priceDict.Add("bestStock", 3238);
//And so on for each int

The values in these can be called like so

double worstMinusBestPrices = priceData[0] - priceData[1]; //For arrays
double worstMinusBestPrices = priceDict["worstPrice"] - priceDict["bestPrice"] //For dictionaries
answered Apr 29, 2016 at 13:26

4 Comments

ah i see, many thanks only just started on arrays trying to get my head around declaring them.
Note that an array is a terrible replacement for a class. It removes all semantic meaning of the data and relies on consuming code to know that meaning. See the refactoring pattern "Replace Array With Object": refactoring.com/catalog/replaceArrayWithObject.html
@David hence I added the dictionary, as the meaning is lost otherwise :)
@AlfieGoodacre: The dictionary is almost as bad, really. "Stringly typing" property names and requiring that all properties be of the same type is hardly a replacement for a class either.
1

You could implement a custom class which holds these values as proprties with meaningful names. Then your code will be much more readable, maintainable and robust.

For example (you don't need all of these classes, it should just give you an idea):

public abstract class Animal
{
 public Animal(string animalName)
 {
 this.Name = animalName;
 }
 //insert properties and methods which all aimals share here
 public string Name { get; set; }
}
public class CibetCat : Animal
{
 public CibetCat() : base("CibetCat")
 {
 }
 //insert properties and methods which all CibetCats share here
}

Now your class that holds the price and stock informations as well as the reference to the animal itself(CibetCat in your example):

public class AnimalStock // or AnimalPrice or whatever
{
 public AnimalStock(Animal animal)
 {
 this.Animal = animal;
 }
 public AnimalStock(Animal animal, decimal worstPrice, decimal bestPrice, int bestStock, int worstStock)
 {
 this.Animal = animal;
 this.Worstprice = worstPrice;
 this.BestPrice = bestPrice;
 this.BestStock = bestStock;
 this.WorstStock = worstStock;
 }
 public Animal Animal { get; set; }
 public decimal Worstprice { get; set; }
 public decimal BestPrice { get; set; }
 public int BestStock { get; set; }
 public int WorstStock { get; set; }
 // ...
}

Lot of code but not complex. Now You can write this simple and readable code:

Animal cibetCat = new CibetCat();
AnimalStock stock = new AnimalStock(cibetCat);
stock.BestPrice = 0.99m;
stock.Worstprice = 6.47m;
stock.BestStock = 3238;
// ...

Later you can access all these properties(or it's methods) from a single instance.

Console.WriteLine("Animal's best-price is: {0}", stock.BestPrice); // etc
answered Apr 29, 2016 at 13:46

Comments

0

As Alfie pointed out, you could use a dictionary - but you're then referencing things by a string identifier, that you have to remember.

Another way would be to use a class or struct. There are of course many ways to do this, but some include:

public class Things
{
 public double worstPrice = 6.47;
 public double bestPrice = 0.99;
 public double CivetCatPrice =29.14;
 public double whenPrice = 10.50;
 public double everythingPrice = 319.56;
 public int bestStock = 3238;
 public int worstStock = 8;
 public int civetCatstock = 3;
 public int whenStock = 37;
 public int everythingStock = 2;
}

Another way would be:

public class Things
{
 public double WorstPrice { get; readonly set; }
 public double BestPrice = { get; readonly set; }
 // etc
 public Things(double worstPrice, double bestPrice) // etc
 {
 WorstPrice = worstPrice;
 BestPrice = bestPrice;
 }
}

There are pros and cons to both approaches. Another potential is to use a collection of a class/struct to group things and aggregate them in meaningful ways.

Like:

public class Thing
{
 public string ThingLabel { get; readonly set; }
 public double ThingPrice { get; readonly set; }
 public int ThingQuantity { get; readonly set; }
 // the value of your stock, calculated automatically based on other properties
 public double ThingValue { get { ThingPrice * ThingQuantity; } }
 public Thing(string thingLabel, double thingPrice, int thingQuantity)
 {
 ThingLabel = thingLabel;
 // etc
 }
}
public void DoStuff()
{
 List<Thing> list = new List<Thing>();
 Thing thing = new Thing("Civet cat", 500, 10);
 list.Add(thing);
 list.Add(new Thing("Sea flap flap", 100, 5);
 list.Add(new Thing("Nope Rope", 25, 4);
 Console.WriteLine("The value of {0}'s stock is: {1}", thing.ThingLabel, thing.Value);
}

and yet another way is to use a base class and create sub classes of your different types. The possibilities are nearly endless! You just have to decide which way works best for you now, you later, and your potential team.

answered Apr 29, 2016 at 13:46

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.