Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
AnswerRe: Naming Rules Pin
Pete O'Hanlon30-Aug-10 23:30
mvePete O'Hanlon30-Aug-10 23:30 
AnswerRe: Naming Rules Pin
Eddy Vluggen31-Aug-10 0:08
professionalEddy Vluggen31-Aug-10 0:08 
AnswerRe: Naming Rules Pin
Kythen1-Sep-10 8:30
Kythen1-Sep-10 8:30 
Questioncan't get float value Pin
zhiyuan1630-Aug-10 22:22
zhiyuan1630-Aug-10 22:22 
AnswerRe: can't get float value Pin
Bigdeak30-Aug-10 23:00
Bigdeak30-Aug-10 23:00 
AnswerRe: can't get float value Pin
phil.o31-Aug-10 0:34
professionalphil.o31-Aug-10 0:34 
AnswerRe: can't get float value Pin
Luc Pattyn31-Aug-10 2:23
sitebuilderLuc Pattyn31-Aug-10 2:23 
QuestionAbstract Static and Inheritance Pin
Matthew Klein30-Aug-10 15:28
Matthew Klein30-Aug-10 15:28 
I’m apparently trying to do something stupid, because the C# help file says that I can’t, and that usually means my design is all jacked up.
Here’s a version of what I’m trying to do:
I have a base class, ItemBase, and I want to give it the method
public class ItemBase
{
    public abstract static List<string> GetAvaliableUpgrades();
}

Then, I have three classes
public class Car : ItemBase { }
public class Van : ItemBase { }
public class Truck : ItemBase { }

(they implement GetAvaliableUpgrades())

And finally, I have a control
public class MyItemDisplay<T> : UserControl where T : ItemBase
{
    public void SetUpgrades()
    {
        foreach (string s in T.GetAvaliableUpgrades())
            listBox1.Items.Add(s);
    }
}


Now I know that I can’t do that in C#, but I’m wondering if there is another way to implement this type of process. Since the Upgrades List is a property of the class itself and not unique to each instance, I want to make the GetAvaliableUpgrades() method static, and I don’t want to repeat myself, so I want the SetUpgrades() method in the Control to work on any ItemBase.

I know I can make something similar work by doing:
public class ItemBase
{
    protected abstract List<string> GetMyUpgrades();

    public static List<string> GetAvaliableUpgrades<T>() where T : ItemBase, new()
    {
        T item = new T();
        return item.GetMyUpgrades();
    }
}

public class MyItemDisplay<T> : UserControl where T : ItemBase
{
    public void SetUpgrades()
    {
        foreach (string s in ItemBase.GetAvaliableUpgrades<T>())
            listBox1.Items.Add(s);
    }
}

But that feels sloppy and incorrect, since I’ve got to make an instance of a class to get a property about that class. Is there some kind of design pattern I can research that will allow me to get the kind of functionality that I’m looking for in C#? Or is there another way to allow me to have one control that works on a family of classes, without having to copy-paste a bunch of code? Thank you all in advance for your time!

Matt
AnswerRe: Abstract Static and Inheritance Pin
PIEBALDconsult30-Aug-10 16:50
mvePIEBALDconsult30-Aug-10 16:50 
GeneralRe: Abstract Static and Inheritance Pin
Matthew Klein31-Aug-10 4:38
Matthew Klein31-Aug-10 4:38 
GeneralRe: Abstract Static and Inheritance Pin
Chris Trelawny-Ross31-Aug-10 5:35
Chris Trelawny-Ross31-Aug-10 5:35 
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 

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.