Re: Generic Method return Type

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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;
}
}

}
}


.



Relevant Pages

  • Re: [PATCH] PXA255 LCD Driver
    ... into the standard modedb database. ... if I got all the needed data from the EDID about a display panel. ... ask yourself can I get display hardware information. ... Attempt to generate the struct fb_monspecs data. ...
    (Linux-Kernel)
  • Re: Structures with variable length array known at compile time
    ... items will be known at compile time. ... static-qualified array for the ith string in the display. ... struct menu { ... struct menu menu1 = { ...
    (comp.lang.c)
  • Re: Converting Binary
    ... > How can I display the binary values to decimal on the screen and later ... int main{ ... There's some sort ... "struct" at a time. ...
    (alt.comp.lang.learn.c-cpp)
  • [PATCH 1/4] NOMMU: Implement /proc/pid/maps for NOMMU
    ... A list of all the mappings in use by a process is visible through ... * display a list of all the VMAs the kernel knows about ... static int maps_open(struct inode *inode, ...
    (Linux-Kernel)
  • Re: System Fonts
    ... change system fonts from within a program. ... how would I display "From" in bold text? ... To have a bit of text in a string display in one or more of ...
    (comp.sys.hp48)