J. A
J. A

Reputation: 31

c# need help working with arrays

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;

Upvotes: 1

Views: 83

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460018

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

Upvotes: 1

Kritner
Kritner

Reputation: 13765

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.

Upvotes: 0

Alfie Goodacre
Alfie Goodacre

Reputation: 2793

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

Upvotes: 1

Related Questions