Re: Generic Method return Type
- From: Burrows <burrows.stephen@xxxxxxxxx>
- Date: Fri, 03 Aug 2007 09:30:10 -0000
Thanks for the reply Ignacio Machin but found the anwaser is was the
way I was casting the return
#region Class Header
////////////////////////////////////////////////////////////////////////////////
//NameSpace :GenericTest
//Class Name :GenericReturnTest
////////////////////////////////////////////////////////////////////////////////
//Copyright :© Albion Software
//Date :03/08/2007
//Author :© SBurrows
//Purpose :A Class showing an example of how a Generic Method
// : with a Generic Return Type works.
////////////////////////////////////////////////////////////////////////////////
#endregion Class Header
using System;
using System.Collections.Generic;
namespace GenericTest
{
#region ComboInfo Struct
/// <summary>
/// A structure for the population of Comboboxes or Listboxes
/// </summary>
struct ComboInfo
{
string display;
int index;
/// <summary>
/// Gets or sets the display.
/// </summary>
/// <value>The display.</value>
public string Display
{
get
{
return display;
}
set
{
display = value;
}
}
/// <summary>
/// Gets or sets the index.
/// </summary>
/// <value>The index.</value>
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
#endregion
#region ComboInfoWithComments Struct
/// <summary>
/// A structure for the population of Comboboxes or Listboxes
/// </summary>
struct ComboInfoWithComments
{
string display;
int index;
string comments;
/// <summary>
/// Gets or sets the comments.
/// </summary>
/// <value>The comments.</value>
/// <Date>02/08/2007</Date>
/// <Author> SBurrows</Author>
public string Comments
{
get
{
return comments;
}
set
{
comments = value;
}
}
/// <summary>
/// Gets or sets the display.
/// </summary>
/// <value>The display.</value>
public string Display
{
get
{
return display;
}
set
{
display = value;
}
}
/// <summary>
/// Gets or sets the index.
/// </summary>
/// <value>The index.</value>
public int Index
{
get
{
return index;
}
set
{
index = value;
}
}
}
#endregion ComboInfoWithComments Struct
/// <summary>
/// Description of Test.
/// </summary>
public class GenericReturnTest
{
public GenericReturnTest()
{
//Call the test Method using ComboInfo as Type
List<ComboInfo> info = TestGen<ComboInfo>();
System.Windows.Forms.MessageBox.Show(info[0].Display);
//Call the test Method using ComboInfoWithComments as Type
List<ComboInfoWithComments> infoWithCom =
TestGen<ComboInfoWithComments>();
System.Windows.Forms.MessageBox.Show(infoWithCom [1].Display);
System.Windows.Forms.MessageBox.Show(infoWithCom [1].Comments);
}
/// <summary>
/// Method that returs a generic List
/// </summary>
/// <returns>A List<T></returns>
public List<T> TestGen<T>()
{
//Get Type of to be returned
Type tp= typeof(T);
if(tp == typeof(ComboInfo)){
List<ComboInfo> dataSourceList = new List<ComboInfo>();
ComboInfo info = new ComboInfo();
info.Display ="Stephen";
info .Index =66;
dataSourceList.Add(info);
info = new ComboInfo();
info.Display="Hello Form NO Comments";
info.Index=31;
dataSourceList.Add(info);
return dataSourceList as List<T>;
}else if (tp == typeof(ComboInfoWithComments)){
List<ComboInfoWithComments> dataSourceList = new
List<ComboInfoWithComments>();
ComboInfoWithComments info = new ComboInfoWithComments();
info.Display ="Generics";
info .Index =66;
info.Comments="Generics";
dataSourceList.Add(info);
info = new ComboInfoWithComments();
info.Display="Hello Form With Comments";
info.Index=31;
info.Comments="With Comments are working";
dataSourceList.Add(info);
return dataSourceList as List<T>;
}else{
return null;
}
}
}
}
.
- Follow-Ups:
- Re: Generic Method return Type
- From: Ignacio Machin \( .NET/ C# MVP \)
- Re: Generic Method return Type
- References:
- Generic Method return Type
- From: BombDrop
- Re: Generic Method return Type
- From: Ignacio Machin \( .NET/ C# MVP \)
- Generic Method return Type
- Prev by Date: Re: TabControl
- Next by Date: Re: how to use 'null' keyword when calling c# dlls in c++ projects?
- Previous by thread: Re: Generic Method return Type
- Next by thread: Re: Generic Method return Type
- Index(es):
Relevant Pages
|