I got several properties that are almost exactly the same, I'm trying to merge them down to just one property, but I'm not doing it right. Here are the over-redundant properties
Code:
public string[] GetShieldBucklerNamesList
{
get
{
string[] shields;
ArrayList ShieldsToAdd = new ArrayList();
foreach (KeyValuePair<string, Tuple<ShieldType, int, int>> shield in InvShields)
{
if (shield.Value.First == ShieldType.Buckler)
{
string shieldName = shield.Key;
ShieldsToAdd.Add(shieldName);
}
}
shields = ShieldsToAdd.ToArray(typeof(string)) as string[];
return shields;
}
}
public string[] GetShieldHeaterNamesList
{
get
{
string[] shields;
ArrayList ShieldsToAdd = new ArrayList();
foreach (KeyValuePair<string, Tuple<ShieldType, int, int>> shield in InvShields)
{
if (shield.Value.First == ShieldType.Heater)
{
string shieldName = shield.Key;
ShieldsToAdd.Add(shieldName);
}
}
shields = ShieldsToAdd.ToArray(typeof(string)) as string[];
return shields;
}
}
public string[] GetShieldKiteNamesList
{
get
{
string[] shields;
ArrayList ShieldsToAdd = new ArrayList();
foreach (KeyValuePair<string, Tuple<ShieldType, int, int>> shield in InvShields)
{
if (shield.Value.First == ShieldType.Kite)
{
string shieldName = shield.Key;
ShieldsToAdd.Add(shieldName);
}
}
shields = ShieldsToAdd.ToArray(typeof(string)) as string[];
return shields;
}
}
public string[] GetShieldRoundNamesList
{
get
{
string[] shields;
ArrayList ShieldsToAdd = new ArrayList();
foreach (KeyValuePair<string, Tuple<ShieldType, int, int>> shield in InvShields)
{
if (shield.Value.First == ShieldType.Round)
{
string shieldName = shield.Key;
ShieldsToAdd.Add(shieldName);
}
}
shields = ShieldsToAdd.ToArray(typeof(string)) as string[];
return shields;
}
}
here is how I am trying to slim it down... but Im getting a ; expected error.. Am i missing something simple?? I can't see it
Code:
public string[] GetShieldByTypeNamesList(ShieldType shieldType)
{
get
{
string[] shields;
ArrayList ShieldsToAdd = new ArrayList();
foreach (KeyValuePair<string, Tuple<ShieldType, int, int>> shield in InvShields)
{
if (shield.Value.First == shieldType)
{
string shieldName = shield.Key;
ShieldsToAdd.Add(shieldName);
}
}
shields = ShieldsToAdd.ToArray(typeof(string)) as string[];
return shields;
}
}
string[] GetShield( SheildType shieldType )
{
var shieldList = new <string>( );
foreach ( shield in InvShields )
{
if ( shield.Value.First == shieldType )
{
shieldList.Add( shield.Key );
}
}
return shieldList.ToArray( );
}
Then reuse it...
Code:
public string[] ShieldBucklerNames
{
get { return GetShield( ShieldType.Buckler ); }
}
public string[] ShieldHeaterNames
{
get { return GetShield( ShieldType.Heater ); }
}
...
TIP: Prefer to use List<> over the old fashioned ArrayList. The ArrayList class isn't typesafe which can lead to runtime errors. The List<> is typesafe so if you try to add a different type to it, you'll get a compiler error.
Naming Comments:
Properties don't need to include the prefix Get. (i.e GetShieldBucklerNamesList was renamed to ShieldBucklerNames)
Local variables should be camelCase. Since class properties are PascalCase, it's confusing if you name a local variable in PascalCase (because it implies the variable isn't local).
For example,
Code:
ArrayList ShieldsToAdd = new ArrayList( );
should be
Code:
ArrayList shieldsToAdd = new ArrayList( );
or (better)
Code:
var shieldsToAdd = new ArrayList( );
or (best)
Code:
var shieldList = new List<string>( );
Last edited by Arjay; November 18th, 2010 at 07:23 PM.
var myClassDict = new Dictionary< string, MyClass >( );
less typing, easier to read, etc.
Coming from a C++ background, I had a bit of trouble using 'var' when it first came out in C# 3.0 I thought it was like the var in VB or Javascript wherein it represents an object (like void* in C++). In others words, it's not typesafe (which can result in runtime errors).
The var in C# is different because it's typesafe so it helps reduce code size for local variables without any of the drawbacks.
yes but - you can't declare a var outside of a method, for instance it cannot be a field or property. Maybe it goes to garbage collection faster than something properly declared in a field??
var makes use of Option Infer... where at compile time the compiler will infer the type of the object based on what's being assigned to it. It can be used at any scope (as far as I am aware), but as arjay noted, it needs to be assigned a value at the same time.
Bookmarks