Re: Cannot apply indexing with [] to an expression of type with ge
- From: Mike Buckingham <MikeBuckingham@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 18 Jan 2007 13:27:01 -0800
Hi Stoitcho,
Thank you for the information. I was able to use constraints to make it work
but uses this approach defeats the purpose of using a generic/template. I am
trying to avoid specifing the type. In C++ the compiler check the type you
passed it and if the type you passed it had the methods you needed it
compiled. In my code the type I am calling the method with is rigth the
compiler doesn't seem to be able to figure it out.
If the objects I was using had a common base class or interface that defined
the methods I am using, I would not need to use a template I'd use that type.
Templates are supposed to be used when you don't have a common base class or
interface.
Mike
"Stoitcho Goutsev (100)" wrote:
Mike,.
Unless you provide the compiler with more information about the type that
the generic uses the compiler won't let you use any member on this type that
is not declared on the level of the Object type because the compiler cannot
guarantee that this member is going to be supported by the actual type at
runtime. More information is provided to the compiler in a form of
constraints. I'd suggest you to read through
http://msdn2.microsoft.com/en-us/library/d5x73970.aspx
In your case DataRow and IDataReder have no common ancestor, so I don't
think you can provide a constraint, thus using generics is not a good idea.
What I can suggest is to create a wrapper class that will handle different
types of sources probably in a different way internally, but externaly it
will provide unified interface (set of methods, indexers and proeprties) for
reading the data. This class will have as many constructor overloads as data
sources are supported. Once you have this wrapper class the method for
consuming the data won't need to be generic as long as the caller provides
instance of the wrapper class initialized with proper data source.
--
HTH
Stoitcho Goutsev (100)
"Mike Buckingham" <MikeBuckingham@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:2FFA41F3-C146-4AFF-9747-5FEFF6C29C20@xxxxxxxxxxxxxxxx
I tried switching the [] calls to item() and that did not work either. I am
wondering if this a limitation of the MS generics implementation. I hope
it
isn't becuase I'd like to make this code work.
Mike
"Mike Buckingham" wrote:
I am getting the above error on types that have indexers. Here is my
code.
This how I call the method:
ValidateFolder<DataRow>(FolderNumber, FolderTitle, ref recRangeBegining,
ref recRangeEnd, alphaRangeFloor, table.Rows[1]);
This method definition.
private void ValidateFolder<dataSourceType>(string FolderNumber,
string FolderTitle, ref SqlDateTime recRangeBegining, ref SqlDateTime
recRangeEnd, string alphaRangeFloor, dataSourceType dataSource)
{
CompareFolderValues<string>(dataSource["Folder_Number"].ToString().Trim(),
FolderNumber, "Folder_Number");
CompareFolderValues<string>(dataSource["Folder_Title"].ToString().Trim(),
FolderTitle, "Folder_Title");
CompareFolderValues<SqlDateTime>((SqlDateTime)dataSource["Folder_Date_Range1"],
recRangeBegining, "Folder_Date_Range1");
CompareFolderValues<SqlDateTime>((SqlDateTime)dataSource["Folder_Date_Range2"],
recRangeEnd, "Folder_Date_Range2");
CompareFolderValues<string>(dataSource["Folder_Alpha_Range1"].ToString().Trim(),
alphaRangeFloor, "Folder_Alpha_Range1");
}
If I hard code the type, it works fine but I want to use the same code
for
DataRows and DataReaders.
Any help would be appreciated.
Thanks,
Mike
- Follow-Ups:
- Re: Cannot apply indexing with [] to an expression of type with ge
- From: Mike Buckingham
- Re: Cannot apply indexing with [] to an expression of type with ge
- Prev by Date: Re: Compile Multiple DLLs into a single DLL
- Next by Date: Re: importing csv to datagrid, adding column data
- Previous by thread: Re: PrinterSettings.InstalledPrinters
- Next by thread: Re: Cannot apply indexing with [] to an expression of type with ge
- Index(es):
Relevant Pages
|