Click here to Skip to main content
15,893,161 members
Home / Discussions / C#
   

C#

 
GeneralRe: Hello.. Pin
mjawadkhatri10-Jul-09 1:08
mjawadkhatri10-Jul-09 1:08 
GeneralRe: Hello.. Pin
J4amieC10-Jul-09 1:15
J4amieC10-Jul-09 1:15 
GeneralRe: Hello.. Pin
mjawadkhatri10-Jul-09 1:23
mjawadkhatri10-Jul-09 1:23 
GeneralRe: Hello.. Pin
Henry Minute10-Jul-09 1:28
Henry Minute10-Jul-09 1:28 
GeneralRe: Hello.. Pin
dan!sh 10-Jul-09 1:18
professional dan!sh 10-Jul-09 1:18 
GeneralRe: Hello.. Pin
Hum Dum10-Jul-09 1:20
Hum Dum10-Jul-09 1:20 
GeneralRe: Hello.. Pin
Vasudevan Deepak Kumar10-Jul-09 2:02
Vasudevan Deepak Kumar10-Jul-09 2:02 
QuestionGenerics and Arrays Help Please! Pin
James612810-Jul-09 0:49
James612810-Jul-09 0:49 
Hi All

Here is a cut down version of the code that I am working on and having problems with
The first Code Block works fine

public  class MyBase<T>
{
    public string MyName;
    public object MyValue;

    public T ValueTyped
    {
        get {return (T)MyValue; }
        set { MyValue = (object)value; }
    }

    public MyBase(string NewName, T DefaultValue)
    {	
        MyName = NewName;
        ValueTyped = DefaultValue;
    }	
}


public class MyString : MyBase<string>
{
    public int MyMaxLength = 50;
    public MyString(string NewName, string DefaultValue, int MaxLength)
      : base(NewName, DefaultValue) 
    {
        MyMaxLength = MaxLength;
    }
}

public class MyInt : MyBase<int>
{
    public int MyMaxValue = 100;
    public MyInt(string NewName, int DefaultValue, int MaxValue)
      : base(NewName, DefaultValue) 
    {
        MyMaxValue = MaxValue;
    }
}

public class Testing
{
    public void test()
    {
        MyString _string = new MyString("Name","",20);
        MyInt _int = new MyInt("Size",10,72);
    }
}


Up to here all is OK. The next Code block falls over. I am try to create an array that can contain MyString and MyInt classes using the base class MyBase so that it can be used like in class testing2.test

public class MyBaseArray
{
    List<MyBase> ListAll = new List<MyBase>;

    public void AddMyBase(MyBase Item)
    {
        ListAll.Add(Item);
    }

    public MyBase Find(string name)
    {
        foreach (MyBase _MyBase in ListAll)
        {
            if (_MyBase.MyName == name)
            {
                return _MyBase;
            }
        }
        throw new Exception("Item Not in list");
    }
}

public class testing2
{
    public void test()
    {
        MyBaseArray MyBases = new MyBaseArray();
        MyBases.AddMyBase((MyBase)new MyString("Name1","Fred",20));
        MyBases.AddMyBase((MyBase)new MyInt("Size1",10,72));
        MyBases.AddMyBase((MyBase)new MyString("Name2","George",20));
        MyBases.AddMyBase((MyBase)new MyInt("Size2",8,72));

        MessageBox.show(MyBases.Find("Name2").ValueTyped);
        int FontSize = MyBases.Find("Size2").ValueTyped
    }
}



The Idea works If I remove the Generics from MyBase and make ValueTyped return an Object.

The question, is it posible to have a array of MyBase where I can use .ValueTyped to return a string for MyString and an int for MyInt.
without have to do any type casing?

MessageBox.show(((MyString)MyBases.Find("Name2")).ValueTyped)
int FontSize = ((MyInt)MyBases.Find("Size2")).ValueTyped
or
MessageBox.show((string)MyBases.Find("Name2").ValueTyped)
int FontSize = (int)MyBases.Find("Size2")).ValueTyped


The original code above is far more readable.

Generics did seem like the way to go until I hit this problem.

Thanks any help.
James
AnswerRe: Generics and Arrays Help Please! Pin
DoctorMick10-Jul-09 0:55
DoctorMick10-Jul-09 0:55 
GeneralRe: Generics and Arrays Help Please! Pin
James612810-Jul-09 1:16
James612810-Jul-09 1:16 
Questionhow do i use sendkeys.send Pin
Vivek Vijayan10-Jul-09 0:45
Vivek Vijayan10-Jul-09 0:45 
AnswerRe: how do i use sendkeys.send Pin
dan!sh 10-Jul-09 1:00
professional dan!sh 10-Jul-09 1:00 
QuestionPlace cursor at the end of combo box text Pin
pp.p10-Jul-09 0:35
pp.p10-Jul-09 0:35 
AnswerRe: Place cursor at the end of combo box text Pin
Nagy Vilmos10-Jul-09 0:43
professionalNagy Vilmos10-Jul-09 0:43 
QuestionSOAP Messages - Intercepting and redirecting the HTTP call to a remoting server Pin
MrEyes10-Jul-09 0:21
MrEyes10-Jul-09 0:21 
AnswerRe: SOAP Messages - Intercepting and redirecting the HTTP call to a remoting server Pin
Member 440849712-Jan-10 2:02
Member 440849712-Jan-10 2:02 
QuestionHow to find specific HtmlElement on a web page if we know some (X, Y) co-ordinates on the IE window? Pin
svt gdwl10-Jul-09 0:17
svt gdwl10-Jul-09 0:17 
QuestionInvoke Problem Pin
gehbitte9-Jul-09 23:56
gehbitte9-Jul-09 23:56 
AnswerRe: Invoke Problem Pin
Luc Pattyn10-Jul-09 0:14
sitebuilderLuc Pattyn10-Jul-09 0:14 
GeneralRe: Invoke Problem Pin
gehbitte10-Jul-09 0:20
gehbitte10-Jul-09 0:20 
GeneralRe: Invoke Problem Pin
gehbitte10-Jul-09 0:28
gehbitte10-Jul-09 0:28 
GeneralRe: Invoke Problem Pin
Luc Pattyn10-Jul-09 0:46
sitebuilderLuc Pattyn10-Jul-09 0:46 
Questioncode to invoke querty keyboard keys in windows applicattion Pin
Vivek Vijayan9-Jul-09 23:52
Vivek Vijayan9-Jul-09 23:52 
AnswerRe: code to invoke querty keyboard keys in windows applicattion Pin
dan!sh 9-Jul-09 23:55
professional dan!sh 9-Jul-09 23:55 
GeneralRe: code to invoke querty keyboard keys in windows applicattion Pin
Vivek Vijayan10-Jul-09 0:15
Vivek Vijayan10-Jul-09 0:15 

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.