samedi 27 décembre 2014

Do I write these using statements inside or outside the class?


Vote count:

0




I have a class that I wrote that contains different methods that perform calculations on data pulled from my database. Each method pulls information from the database and performs calculations on it and then saves it to a variable in the class that I can access when I initialize the class. I have two pseudo code methods below and I'm trying to figure out the following:



  1. Which way should I utilize the using statements if every method pulls data and then performs calculations on it?

  2. Am I using the most efficient way to store the data?


This is what I do when I initialize the class



public class Calculations
{
public string Symbol { get; set; }
public string Market { get; set; }
public List<decimal> stockPctReturns { get; set; }
public List<decimal> marketPctReturns { get; set; }
public enum returnType { Market, Stock };
public decimal avgAnnualMarketGrowth { get; set; }
public List<decimal> stockPctGains { get; set; }
public List<decimal> stockPctLosses { get; set; }
public List<decimal> positiveMoneyFlow { get; set; }
public List<decimal> negativeMoneyFlow { get; set; }
public decimal rsi { get; set; }
public decimal mfi { get; set; }
public decimal williamsR { get; set; }
public decimal sma20 { get; set; }
public decimal sma50 { get; set; }
public decimal sma200 { get; set; }
public const decimal riskFree10Yr = 2.58M;

public Calculations(string symbol, string market)
{
// initialize everything in the class
Symbol = symbol;
Market = market;
}
}


Do it this way:



public void fillPctReturns()
{
stockPctReturns = new List<decimal>();
marketPctReturns = new List<decimal>();

using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
using (DailyGlobalDataTable dailyGlobalTable = new DailyGlobalDataTable())
using (DailyAmexDataDataTable dailyAmexTable = new DailyAmexDataDataTable())
{
dailyAmexAdapter.Fill(dailyAmexTable);
dailyGlobalAdapter.Fill(dailyGlobalTable);
var amexQuery = from c in dailyGlobalTable.AsEnumerable()
where c.Date >= DateTime.Now.Subtract(TimeSpan.FromDays(60))
orderby c.Date descending
join d in dailyAmexTable.AsEnumerable() on c.Date equals d.Date
select new StockMarketCompare { stockClose = d.AdjustedClose, marketClose = c.AdjustedClose };

List<StockMarketCompare> amexResult = amexQuery.ToList();
// perform calculations here and save to stockPctReturns and marketPctReturns
}
}


Or should I do it this way:



using (DailyGlobalDataTableAdapter dailyGlobalAdapter = new DailyGlobalDataTableAdapter())
using (DailyAmexDataTableAdapter dailyAmexAdapter = new DailyAmexDataTableAdapter())
{
Calculations calc = new Calculations();
calc.Symbol = "GOOG";
calc.Market = "nyse";
dailyAmexAdapter.Fill(calc.dailyAmexTable);
dailyGlobalAdapter.Fill(calc.dailyGlobalTable);
calc.fillPctReturns();
}


// this example would have public properties in the calculations class for the datatables and as you can see I set them like the above


Hopefully you can tell what I'm trying to do.



asked 45 secs ago







Do I write these using statements inside or outside the class?

Aucun commentaire:

Enregistrer un commentaire