Is there a way to add multiple variables together without implicitly declaration - c#

I have 23 int variables that get assigned value upon form load, is there a shortcut to add them together without implicit addition.
I.E VarAns = Var1 + Var2 + Var3.... + Var 23.
MathsGrp1 = Convert.ToInt32(textBoxMathsGrp1.Text);
MathsGrp3 = Convert.ToInt32(textBoxMathsGrp3.Text);
MathsGrp2 = Convert.ToInt32(textBoxMathsGrp2.Text);
MathsGrp4 = Convert.ToInt32(textBoxMathsGrp4.Text);
EnglishGrp1 = Convert.ToInt32(textBoxEnglishGrp1.Text);
EnglishGrp2 = Convert.ToInt32(textBoxEnglishGrp3.Text);
EnglishGrp3 = Convert.ToInt32(textBoxEnglishGrp2.Text);
EnglishGrp4 = Convert.ToInt32(textBoxEnglishGrp4.Text);
Construction = Convert.ToInt32(textBoxConstruction.Text);
PSD = Convert.ToInt32(textBoxPSD.Text);
Careers = Convert.ToInt32(textBoxCareers.Text);
ASDAN = Convert.ToInt32(textBoxASDAN.Text);
Music = Convert.ToInt32(textBoxMusic.Text);
Spare = Convert.ToInt32(textBoxSpare.Text);
Art = Convert.ToInt32(textBoxArt.Text);
Science = Convert.ToInt32(textBoxScience.Text);
PEGrp1 = Convert.ToInt32(textBoxPEGrp1.Text);
PEGrp2 = Convert.ToInt32(textBoxPEGrp2.Text);
ICT = Convert.ToInt32(textBoxICT.Text);
HairDressing = Convert.ToInt32(textBoxHairDressing.Text);
CookingGrp1 = Convert.ToInt32(textBoxCookingGrp1.Text);
CookingGrp2 = Convert.ToInt32(textBoxCookingGrp2.Text);
CookingGrp3 = Convert.ToInt32(textBoxCookingGrp3.Text);
// int Check = insert Long list of variables here
P.S i know theres a better way to initilise and convert the textbox strings into integers but i want to keep it simple.

public void Function()
{
List<int> Collection = new List<int>();
Collection.Add(1);
Collection.Add(2);
Collection.Add(3);
Collection.Add(7);
Collection.Add(9);
Collection.Add(5);
Collection.Add(25);
foreach (int Elem in Collection)
{
int Result = 0;
Result = Result + Elem;
}
}

Related

How can I access multi-element List data stored in a public class?

My first question on SO:
I created this public class, so that I can store three elements in a list:
public class myMultiElementList
{
public string Role {get;set;}
public string Country {get;set;}
public int Commonality {get;set;}
}
In my main class, I then created a new list using this process:
var EmployeeRolesCountry = new List<myMultiElementList>();
var rc1 = new myMultiElementList();
rc1.Role = token.Trim();
rc1.Country = country.Trim();
rc1.Commonality = 1;
EmployeeRolesCountry.Add(rc1);
I've added data to EmployeeRolesCountry and have validated that has 472 lines. However, when I try to retrieve it as below, my ForEach loop only retrieves the final line added to the list, 472 times...
foreach (myMultiElementList tmpClass in EmployeeRolesCountry)
{
string d1Value = tmpClass.Role;
Console.WriteLine(d1Value);
string d2Value = tmpClass.Role;
Console.WriteLine(d2Value);
int d3Value = tmpClass.Commonality;
Console.WriteLine(d3Value);
}
This was the most promising of the potential solutions I found on here, so any pointers greatly appreciated.
EDIT: adding data to EmployeeRolesCountry
/*
Before this starts, data is taken in via a csvReader function and parsed
All of the process below is concerned with two fields in the csv
One is simply the Country. No processing necessary
The other is bio, and this itself needs to be parsed and cleansed several times to take roles out
To keep things making sense, I've taken much of the cleansing out
*/
private void File_upload_Click(object sender, EventArgs e)
{
int pos = 0;
var EmployeeRolesCountry = new List<myMultiElementList>();
var rc1 = new myMultiElementList();
int a = 0;
delimiter = ".";
string token;
foreach (var line in records.Take(100))
{
var fields = line.ToList();
string bio = fields[5];
string country = fields[4];
int role_count = Regex.Matches(bio, delimiter).Count;
a = bio.Length;
for (var i = 0; i < role_count; i++)
{
//here I take first role, by parsing on delimiter, then push back EmployeeRolesCountry with result
pos = bio.IndexOf('.');
if (pos != -1)
{
token = bio.Substring(0, pos);
string original_token = token;
rc1.Role = token.Trim();
rc1.Country = country.Trim();
rc1.Commonality = 1;
EmployeeRolesCountry.Add(rc1);
a = original_token.Length;
bio = bio.Remove(0, a + 1);
}
}
}
}
EDIT:
When grouped by multiple properties, this is how we iterate through the grouped items:
var employeesGroupedByRolwAndCountry = EmployeeRolesCountry.GroupBy(x => new { x.Role, x.Country });
employeesGroupedByRolwAndCountry.ToList().ForEach
(
(countryAndRole) =>
{
Console.WriteLine("Group {0}/{1}", countryAndRole.Key.Country, countryAndRole.Key.Role);
countryAndRole.ToList().ForEach
(
(multiElement) => Console.WriteLine(" : {0}", multiElement.Commonality)
);
}
);
__ ORIGINAL POST __
You are instantiating rc1 only once (outside the loop) and add the same instance to the list.
Please make sure that you do
var rc1 = new myMultiElementList();
inside the loop where you are adding the elements, and not outside.
All references are the same in your case:
var obj = new myObj();
for(i = 0; i < 5; i++)
{
obj.Prop1 = "Prop" + i;
list.Add(obj);
}
now the list has 5 elements, all pointing to the obj (the same instance, the same object in memory), and when you do
obj.Prop1 = "Prop" + 5
you update the same memory address, and all the pointers in the list points to the same instance so, you are not getting 472 copies of the LAST item, but getting the same instance 472 times.
The solution is simple. Create a new instance every time you add to your list:
for(i = 0; i < 5; i++)
{
var obj = new myObj();
obj.Prop1 = "Prop" + i;
list.Add(obj);
}
Hope this helps.

C# mimic associative array of unknown key-number (like in PHP)

Is there a possibility to create sth. like an associative array like in PHP?
I don't plan to create a game with some player-data, but I could easily explain this way what I want:
player["Name"] = "PName";
player["Custom"]["Gender"] = "Female";
player["Custom"]["Style"] = "S1";
player["Custom"]["Face"]["Main"] = "FM1";
player["Custom"]["Face"]["Eyes"] = "FE1";
player["Custom"]["Height"] = "180";
Also the length has to be dynamic, I don't how many keys there will be:
player["key1"]["key2"]=value
player["key1"]["key2"]["key3"]["key4"]...=value
What I need is sth. I could address like:
string name = player["Name"];
string gender = player["Custom"]["Gender"];
string style = player["Custom"]["Style"];
string faceMain = player["Custom"]["Face"]["Main"];
string faceEyes = player["Custom"]["Face"]["Eyes"];
string height = player["Custom"]["Height"];
Or in some way similar to this.
What I tried till now:
Dictionary<string, Hashtable> player = new Dictionary<string, Hashtable>();
player["custom"] = new Hashtable();
player["custom"]["Gender"] = "Female";
player["custom"]["Style"] = "S1";
But the problem starts here (only works with 2 keys):
player["custom"]["Face"] = new Hashtable();
player["Custom"]["Face"]["Main"] = "FM1";
C# is strongly typed so it seems not easy to replicate this exact behavior.
A "possibility" :
public class UglyThing<K,E>
{
private Dictionary<K, UglyThing<K, E>> dicdic = new Dictionary<K, UglyThing<K, E>>();
public UglyThing<K, E> this[K key]
{
get
{
if (!this.dicdic.ContainsKey(key)) { this.dicdic[key] = new UglyThing<K, E>(); }
return this.dicdic[key];
}
set
{
this.dicdic[key] = value;
}
}
public E Value { get; set; }
}
Usage :
var x = new UglyThing<string, int>();
x["a"].Value = 1;
x["b"].Value = 11;
x["a"]["b"].Value = 2;
x["a"]["b"]["c1"].Value = 3;
x["a"]["b"]["c2"].Value = 4;
System.Diagnostics.Debug.WriteLine(x["a"].Value); // 1
System.Diagnostics.Debug.WriteLine(x["b"].Value); // 11
System.Diagnostics.Debug.WriteLine(x["a"]["b"].Value); // 2
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c1"].Value); // 3
System.Diagnostics.Debug.WriteLine(x["a"]["b"]["c2"].Value); // 4

Pythagorean theorem and Autocad objects

I've been trying to use that Math.Sqrt method but I'm not getting very far. Are the ###'s maybe screwing me up? I'm getting unable to convert errors when trying to call the length/elevation variables using that method. Should I approach this from a different direction?
Full disclosure: newbie.
public partial class Form1 : Form
{
AcDb.ObjectId id = AcDb.ObjectId.Null;
public Form1()
{
InitializeComponent();
}
public void GetVertices()
{
AcAp.Document doc = AcAp.Application.DocumentManager.MdiActiveDocument;
AcDb.Database db = doc.Database;
AcEd.Editor ed = doc.Editor;
using (var doclock = doc.LockDocument())
{
using (var trans = db.TransactionManager.StartTransaction())
{
var options = new AcEd.PromptEntityOptions("\nSelect a Line:");
options.SetRejectMessage("That is not Line" + "\n");
options.AddAllowedClass(typeof(AcDb.Line), true);
var result = ed.GetEntity(options);
if (result.Status != AcEd.PromptStatus.OK)
return;
this.id = result.ObjectId;
var line = (AcDb.Line)trans.GetObject(this.id, AcDb.OpenMode.ForRead);
var vertexClass = AcTrx.RXClass.GetClass(typeof(AcDb.Line));
var length = line.Length.ToString("#.##");
tbLength.Text = length;
var northing = line.EndPoint.Y.ToString("#.##");
tbNorthing.Text = northing;
var easting = line.EndPoint.X.ToString("#.##");
tbEasting.Text = easting;
var elevation = line.EndPoint.Z.ToString("#.##");
tbElevation.Text = elevation;
var endpoint = line.EndPoint.ToString();
tbEndpoint.Text = endpoint;
var slope = Math.Sqrt(elevation/length) //something something something
}
}
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.Hide();
GetVertices();
this.Show();
}
}
elevation and length are strings. You cannot perform math on strings!
Try
double slope = Math.Sqrt(line.EndPoint.Z / line.Length);
Or change your code to:
double length = line.Length
tbLength.Text = length.ToString("#.##");
double northing = line.EndPoint.Y;
tbNorthing.Text = northing.ToString("#.##");
double easting = line.EndPoint.X;
tbEasting.Text = easting.ToString("#.##");
double elevation = line.EndPoint.Z;
tbElevation.Text = elevation.ToString("#.##");
double endpoint = line.EndPoint;
tbEndpoint.Text = endpoint.ToString();
double slope = Math.Sqrt(elevation/length);
And also I suggest you to use var only when it represents a real simplification and the type can easily be inferred or if the exact type is not of great interest as for LINQ query results with complicated IGouping<whatever> constructs possibly involving anonymous types.
var x = new Dictionary<string, List<Entity<Person>>>();
.... is a simplyfication and yet the type is visible as you can read it on the same line;
var i = 15;
is not a simplyfication over
int i = 15;
I would never use var as substitution for simple types like int, string, double etc.

Correct way to initialize object in array of objects so that i don't get null pointer references?

I am receiving a null pointer exception, and I narrowed it down to this small block of code. I think it is because when I set teamStats[iterator].median = Statistics.median that it can't because it isn't initialized yet. Though I don't know the proper way to initialize an array of objects or the memory ramifications that this code will cause.
Below is the majority of the code i am dealing with besides the class describing TeamStat, but I've used all of its members below and they are public double.
TeamStat[] teamStats = new TeamStat[DistCount+1];
int iterator = 0;
foreach (int i in TeamIDlist)
{
var p = userList.Where(x => x.TeamId.Equals(i)).Select(y => (double)y.BS).ToArray();
var statistics = new DescriptiveStatistics(p);
teamStats[iterator].median = Statistics.Median(p);
teamStats[iterator].largestElement = statistics.Maximum;
teamStats[iterator].smallestElement = statistics.Minimum;
teamStats[iterator].mean = statistics.Mean;
teamStats[iterator].variance = statistics.Variance;
teamStats[iterator].stdDev = statistics.StandardDeviation;
iterator++;
}
Update
Is this the correct way to do this:
TeamStat[] teamStats = new TeamStat[DistCount];
int iterator = 0;
foreach (int i in TeamIDlist)
{
//Added these two lines
TeamStat temp = new TeamStat();
teamStats[iterator] = temp;
var p = userList.Where(x => x.TeamId.Equals(i)).Select(y => (double)y.BS).ToArray();
var statistics = new DescriptiveStatistics(p);
teamStats[iterator].median = Statistics.Median(p);
teamStats[iterator].largestElement = statistics.Maximum;
teamStats[iterator].smallestElement = statistics.Minimum;
teamStats[iterator].mean = statistics.Mean;
teamStats[iterator].variance = statistics.Variance;
teamStats[iterator].stdDev = statistics.StandardDeviation;
iterator++;
}
Here
TeamStat[] teamStats = new TeamStat[DistCount+1];
you initialize the array. At this moment, the array contains DistCount + 1 null entries.
If you want the array to contain DistCount + 1 new TeamStat entries, you need to initialize them in a loop:
TeamStat[] teamStats = new TeamStat[DistCount+1];
for (var i = 0; i < DistCount + 1; i++)
teamStats[i] = new TeamStat();
Your code has 2 issues
1) Each object in Array needs to be initialized before accseing its properties
teamStats[iterator] = new TeamStat();
2) You need to make sure you initizliaed array with same/more capacity as you are trying to assign values to.(TeamIDlist < DistCount+1), otheriwse it will throw index out of range exception.

Does anyone have c# code to use betfair api?

I am creating a c# windows app to display current sports market rates using betfair exchange webservice, I used the
getmarketpricescompressed()
method which returns a price string that looks like this:
106093239~GBP~ACTIVE~0~1~~true~5.0~1343114432333~~N:7337~1~6992.56~2.16~~~false~~~~|2.16~1036.19~L~1~2.14~97.18~L~2~2.12~5.0~L~3~|2.18~467.36~B~1~2.2~34.12~B~2~2.22~162.03~B~3~:414464~2~102181.96~1.86~~~false~~~~|1.85~2900.33~L~1~1.84~1831.59~L~2~1.83~1593.73~L~3~|1.86~58.83~B~1~1.87~1171.77~B~2~1.88~169.15~B~3~
i don't know how to properly unpack this string, for now i am using this code:
GetMarketPricesCompressedReq price_req1 = new GetMarketPricesCompressedReq();
price_req1.header = header2;
price_req1.marketId = marketid_temp;
price_req1.currencyCode = "GBP";
GetMarketPricesCompressedResp price_resp = new GetMarketPricesCompressedResp();
price_resp = bfg2.getMarketPricesCompressed(price_req1);
//MessageBox.Show(price_resp.errorCode.ToString());
//richTextBox1.Text = "";
//richTextBox1.Text = price_resp.marketPrices;
string prices = price_resp.marketPrices;
richTextBox1.Text = price_resp.marketPrices;
string[] ab1 = prices.Split('|');
string[] temp = ab1[1].Split('~');
textBox3.Text = temp[0];
textBox4.Text = temp[4];
textBox5.Text = temp[8];
temp = ab1[2].Split('~');
textBox6.Text = temp[0];
textBox7.Text = temp[4];
textBox8.Text = temp[8];
temp = ab1[3].Split('~');
textBox9.Text = temp[0];
textBox10.Text = temp[4];
textBox11.Text = temp[8];
temp = ab1[4].Split('~');
textBox12.Text = temp[0];
textBox13.Text = temp[4];
textBox14.Text = temp[8];
if (ab1.Length >5)
{
temp = ab1[5].Split('~');
textBox15.Text = temp[0];
textBox16.Text = temp[4];
textBox17.Text = temp[8];
temp = ab1[6].Split('~');
textBox18.Text = temp[0];
textBox19.Text = temp[4];
textBox20.Text = temp[8];
}
It works fine for a few matches, but i observed the string changes for a few other matches and it thus generates exceptions,
Can any1 help me with a proper code to unpack this string, i've googled it and found a vb code, which was not very usefull,
and btw, i want to arrange the data in something like this:
Consider the following:
a|b|c|d
In this, you have four chunks. But it doesn't necessarily have to be only four. It can be two, it can be six.
Try going at it by iterating the string-array you get as a result of the
string[] ab1 = prices.Split('|');
Something like
foreach(var stringChunk in ab1)
{
string[] temp = stringChunk.Split('~');
// use the strings here as you please
}
Consider using a more dynamic approach in presenting your data as well, using a row-based, repeating control instead of static textboxes. =)
The full documentation for the compressed data is available here: https://docs.developer.betfair.com/betfair/#!page=00008360-MC.00008307-MC
Just guessing, I think the data is organised like this
first delimited by : then | we get
106093239~GBP~ACTIVE~0~1~~true~5.0~1343114432333~~N
7337~1~6992.56~2.16~~~false~~~~
2.16~1036.19~L~1~2.14~97.18~L~2~2.12~5.0~L~3~
2.18~467.36~B~1~2.2~34.12~B~2~2.22~162.03~B~3~
414464~2~102181.96~1.86~~~false~~~~
1.85~2900.33~L~1~1.84~1831.59~L~2~1.83~1593.73~L~3~
1.86~58.83~B~1~1.87~1171.77~B~2~1.88~169.15~B~3~
I think that first row is clearly a header, that first number being the market ID perhaps.
Likewise, the first part of each subsequent section is a row identifier. Delimited by ~ something like
(SelectionId)7337 (Order)1 (TotalMatched?)6992.56 (EvenPoint)2.16 (???)false
I think the price rows are delimited by ~ in tuples of 4, like this
(Price)2.16 (MatchAvailable)1036.19 (Type)L (Order)1
(Price)2.14 (MatchAvailable)0097.18 (Type)L (Order)2
(Price)2.12 (MatchAvailable)0005.00 (Type)L (Order)3
for example.
To test my guesses I'd have to compare with the BetFair rendering on thier web site.
I converted the vb code to c# myself, if anyone might find it useful, I am posting it here:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace stock
{
class UnpackMarketPricesCompressed : stock.BFExchangeService.MarketPrices
{
//The substitute code for "\:"
private const string ColonCode = "&%^#";
//Unpack the string
public UnpackMarketPricesCompressed(string MarketPrices)
{
string[] Mprices = null;
string[] Part = null;
string[] Field = null;
int n = 0;
Mprices = MarketPrices.Replace("\\:", ColonCode).Split(':');
//Split header and runner data
Field = Mprices[0].Replace("\\~", "-").Split('~');
//Split market data fields
marketId = Convert.ToInt32(Field[0]);
//Assign the market data
currencyCode = Field[1];
marketStatus = (stock.BFExchangeService.MarketStatusEnum)Enum.Parse(typeof(stock.BFExchangeService.MarketStatusEnum), Field[2], true);
delay = Convert.ToInt32(Field[3]);
numberOfWinners = Convert.ToInt32(Field[4]);
marketInfo = Field[5].Replace(ColonCode, ":");
discountAllowed = (Field[6].ToLower() == "true");
marketBaseRate = float.Parse(Field[7]);
lastRefresh = long.Parse(Field[8]);
removedRunners = Field[9].Replace(ColonCode, ":");
bspMarket = (Field[10] == "Y");
n = Mprices.Length - 1;
// ERROR: Not supported in C#: ReDimStatement
//For each runner
for (int i = 0; i <= n; i++)
{
Part = Mprices[i + 1].Split('|');
//Split runner string into 3 parts
Field = Part[0].Split('~');
//Split runner data fields
runnerPrices[i] = new stock.BFExchangeService.RunnerPrices();
var _with1 = runnerPrices[i];
//Assign the runner data
_with1.selectionId = Convert.ToInt32(Field[0]);
_with1.sortOrder = Convert.ToInt32(Field[1]);
_with1.totalAmountMatched = Convert.ToDouble(Field[2]);
_with1.lastPriceMatched = Convert.ToDouble(Field[3]);
_with1.handicap = Convert.ToDouble(Field[4]);
_with1.reductionFactor = Convert.ToDouble(Field[5]);
_with1.vacant = (Field[6].ToLower() == "true");
_with1.farBSP = Convert.ToDouble(Field[7]);
_with1.nearBSP = Convert.ToDouble(Field[8]);
_with1.actualBSP = Convert.ToDouble(Field[9]);
_with1.bestPricesToBack = Prices(Part[1]);
_with1.bestPricesToLay = Prices(Part[2]);
}
}
private stock.BFExchangeService.Price[] Prices(string PriceString)
{
string[] Field = null;
stock.BFExchangeService.Price[] Price = null;
int k = 0;
int m = 0;
Field = PriceString.Split('~');
//Split price fields
m = (Field.Length / 4) - 1;
//m = number of prices - 1
// ERROR: Not supported in C#: ReDimStatement
for (int i = 0; i <= m; i++)
{
Price[i] = new stock.BFExchangeService.Price();
var _with2 = Price[i];
_with2.price = Convert.ToInt32(Field[k + 0]);
//Assign price data
_with2.amountAvailable = Convert.ToInt32(Field[k + 1]);
_with2.betType = (stock.BFExchangeService.BetTypeEnum)Enum.Parse(typeof(stock.BFExchangeService.BetTypeEnum), Field[k + 2], true);
_with2.depth = Convert.ToInt32(Field[k + 3]);
k += 4;
}
return Price;
//Return the array of prices
}
}
}

Categories