Click here to Skip to main content
15,899,475 members
Home / Discussions / C#
   

C#

 
GeneralRe: Abstract Static and Inheritance Pin
Chris Trelawny-Ross31-Aug-10 5:35
Chris Trelawny-Ross31-Aug-10 5:35 
I wonder if having Car, Van, Truck (etc.?) as distinct C# types is the root of the problem.

What would be the pros & cons of having a simple Vehicle class (and, probably, you would no longer need an ItemBase class)?

Here's how I might implement Vehicle:
public enum VehicleType { Car, Van, Truck }
 

class Vehicle
	{
	public VehicleType VehicleType { get; set; }
 
	static private readonly Dictionary<VehicleType, List<string>> _availableUpgrades = new Dictionary<VehicleType, List<string>>();
 
	public static List<string> GetAvailableUpgrades(VehicleType vehicleType)
		{
		if (!_availableUpgrades.ContainsKey(vehicleType))
			throw new ArgumentException("Vehicle type " + vehicleType + " not recognized in upgrades collection.");
		return _availableUpgrades[vehicleType];
		}
	// ...
	}


If you absolutely must have distinct classes for each vehicle type, perhaps this would work:

	class  ItemBase
		{
		static private readonly Dictionary<Type, List<string>> _availableUpgrades = new Dictionary<Type, List<string>>();
 
		public List<string> GetAvailableUpgrades()
			{
			Type myType = GetType();
			if (!_availableUpgrades.ContainsKey(myType))
				throw new ArgumentException("Vehicle type " + myType.Name + " not recognized in upgrades collection.");
			return _availableUpgrades[myType];
			}
		}
 
	class Car : ItemBase
		{
		// ...
		}

AnswerRe: Abstract Static and Inheritance Pin
Ennis Ray Lynch, Jr.31-Aug-10 6:16
Ennis Ray Lynch, Jr.31-Aug-10 6:16 
QuestionC# - Easy - Music Player & Next Song [modified] Pin
Martiinus30-Aug-10 13:52
Martiinus30-Aug-10 13:52 
AnswerRe: C# - Easy - Music Player & Next Song Pin
Luc Pattyn30-Aug-10 15:00
sitebuilderLuc Pattyn30-Aug-10 15:00 
AnswerRe: C# - Easy - Music Player & Next Song Pin
Martiinus30-Aug-10 15:27
Martiinus30-Aug-10 15:27 
AnswerRe: C# - Easy - Music Player & Next Song Pin
Luc Pattyn30-Aug-10 15:48
sitebuilderLuc Pattyn30-Aug-10 15:48 
QuestionPassing a C# variable to included javascript file Pin
Sonar8730-Aug-10 12:41
Sonar8730-Aug-10 12:41 
AnswerRe: Passing a C# variable to included javascript file Pin
Not Active30-Aug-10 15:22
mentorNot Active30-Aug-10 15:22 
GeneralRe: Passing a C# variable to included javascript file Pin
Sonar8730-Aug-10 15:55
Sonar8730-Aug-10 15:55 
GeneralRe: Passing a C# variable to included javascript file Pin
Not Active30-Aug-10 16:07
mentorNot Active30-Aug-10 16:07 
GeneralRe: Passing a C# variable to included javascript file Pin
Sonar8730-Aug-10 17:12
Sonar8730-Aug-10 17:12 
GeneralRe: Passing a C# variable to included javascript file Pin
Not Active31-Aug-10 0:26
mentorNot Active31-Aug-10 0:26 
GeneralRe: Passing a C# variable to included javascript file Pin
Sonar8731-Aug-10 9:39
Sonar8731-Aug-10 9:39 
QuestionParsing a string Pin
jenya730-Aug-10 4:16
jenya730-Aug-10 4:16 
AnswerRe: Parsing a string Pin
Ennis Ray Lynch, Jr.30-Aug-10 4:18
Ennis Ray Lynch, Jr.30-Aug-10 4:18 
GeneralRe: Parsing a string Pin
jenya730-Aug-10 4:23
jenya730-Aug-10 4:23 
GeneralRe: Parsing a string Pin
Ennis Ray Lynch, Jr.30-Aug-10 4:25
Ennis Ray Lynch, Jr.30-Aug-10 4:25 
GeneralRe: Parsing a string Pin
jenya730-Aug-10 4:26
jenya730-Aug-10 4:26 
GeneralRe: Parsing a string Pin
jenya730-Aug-10 4:30
jenya730-Aug-10 4:30 
GeneralRe: Parsing a string Pin
Ian Shlasko30-Aug-10 4:43
Ian Shlasko30-Aug-10 4:43 
GeneralRe: Parsing a string Pin
OriginalGriff30-Aug-10 5:40
mveOriginalGriff30-Aug-10 5:40 
AnswerRe: Parsing a string Pin
Not Active30-Aug-10 5:17
mentorNot Active30-Aug-10 5:17 
AnswerRe: Parsing a string Pin
PIEBALDconsult30-Aug-10 16:51
mvePIEBALDconsult30-Aug-10 16:51 
GeneralDynamic Arrays Pin
Bassam Abdul-Baki30-Aug-10 3:26
professionalBassam Abdul-Baki30-Aug-10 3:26 
GeneralRe: Dynamic Arrays Pin
Ennis Ray Lynch, Jr.30-Aug-10 3:50
Ennis Ray Lynch, Jr.30-Aug-10 3:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.