Click here to Skip to main content
15,917,177 members
Home / Discussions / C#
   

C#

 
GeneralRe: building a c# project Pin
waheed awan7-Sep-06 6:28
waheed awan7-Sep-06 6:28 
QuestionHow I can fill up the DataGridView with empy rows Pin
unikum6-Sep-06 18:07
unikum6-Sep-06 18:07 
QuestionCreating Installation file Pin
Niiiissssshhhhhuuuuu6-Sep-06 17:51
Niiiissssshhhhhuuuuu6-Sep-06 17:51 
AnswerRe: Creating Installation file Pin
Utkarshraj Atmaram6-Sep-06 18:55
Utkarshraj Atmaram6-Sep-06 18:55 
QuestionNested Collections Pin
xfitr26-Sep-06 17:05
xfitr26-Sep-06 17:05 
AnswerRe: Nested Collections Pin
MIHAI_MTZ6-Sep-06 21:01
MIHAI_MTZ6-Sep-06 21:01 
QuestionYour favourite/best C# 2.0 Book? Pin
James_1236-Sep-06 15:38
James_1236-Sep-06 15:38 
AnswerRe: Your favourite/best C# 2.0 Book? Pin
Andrei Ungureanu6-Sep-06 20:00
Andrei Ungureanu6-Sep-06 20:00 
QuestionHelp ThreadPools/Threads Pin
peterzorbas6-Sep-06 13:30
peterzorbas6-Sep-06 13:30 
QuestionCopy and Past shortcuts stop working when derived form shown non-modally. Pin
Vengeance6-Sep-06 12:50
Vengeance6-Sep-06 12:50 
Questiongeneric inherited class Pin
daveroberge6-Sep-06 9:33
daveroberge6-Sep-06 9:33 
AnswerRe: generic inherited class Pin
Nader Elshehabi6-Sep-06 10:17
Nader Elshehabi6-Sep-06 10:17 
GeneralRe: generic inherited class Pin
daveroberge6-Sep-06 10:46
daveroberge6-Sep-06 10:46 
QuestionRe: generic inherited class Pin
Nader Elshehabi6-Sep-06 11:30
Nader Elshehabi6-Sep-06 11:30 
AnswerRe: generic inherited class Pin
daveroberge6-Sep-06 14:04
daveroberge6-Sep-06 14:04 
AnswerRe: generic inherited class Pin
Nader Elshehabi7-Sep-06 1:59
Nader Elshehabi7-Sep-06 1:59 
Hello

Gotcha!! Now your problem is clear. Here it is: -Watch carefully for generic types-

1- First you declare an object of type I1<c1> you name it oTest2
2- Then you call GetG2()
3- In the method you declare another object of type g2<c2> with the name oG2
Are following me??

Now the problem is that the definition of class g2 is that you inherit it from class g1<c2> -notice the c2 here!!-. So in the base class g1 which has the interface I1 the generic type is c2!! So by default the generic type of I1 -which is the interface of g1 and has the same generic type- is c2, not c1!! That's why when you try to return oG2 and cast it to interface I1<c1> it doesn't work!! It implements interface I1<c2> not I1<c1>. I hope now you see your problem now.

Suggested solutions -among many other solutions of course-.
1- Return type I1<c2> instead
private I1<c2> GetG2()
{
g2<c2> oG2 = new g2<c2>();
oG2.Add(new c2());
return (I1<c2>)oG2;
}

I1<c2> oTest2 = GetG2();
This will preserve your inheritance structure.

OR
Change your inheritance to implement I1<c1> interface in G2

class g2<t> : g1<c2>, I1<c1> where t : c2
{
public override void Add(c2 a)
{
a.sVal2 = "testmodified2";
base.Add(a);
}

public void Add(c1 a)
{
//Do something
}
}

I hope this helps!!

Regards
GeneralRe: generic inherited class Pin
daveroberge7-Sep-06 3:07
daveroberge7-Sep-06 3:07 
GeneralRe: generic inherited class Pin
Nader Elshehabi7-Sep-06 9:04
Nader Elshehabi7-Sep-06 9:04 
GeneralRe: generic inherited class Pin
daveroberge7-Sep-06 10:53
daveroberge7-Sep-06 10:53 
GeneralRe: generic inherited class Pin
Nader Elshehabi7-Sep-06 11:26
Nader Elshehabi7-Sep-06 11:26 
GeneralRe: generic inherited class Pin
daveroberge7-Sep-06 14:31
daveroberge7-Sep-06 14:31 
AnswerRe: generic inherited class Pin
Ed.Poore6-Sep-06 12:01
Ed.Poore6-Sep-06 12:01 
GeneralRe: generic inherited class Pin
daveroberge6-Sep-06 14:09
daveroberge6-Sep-06 14:09 
GeneralRe: generic inherited class Pin
Ed.Poore6-Sep-06 18:38
Ed.Poore6-Sep-06 18:38 
GeneralRe: generic inherited class Pin
daveroberge7-Sep-06 2:54
daveroberge7-Sep-06 2:54 

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.