Re: Cannot apply indexing with [] to an expression of type with ge

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



I found the following paragraph in
http://msdn2.microsoft.com/ru-ru/library/c6cyy67b(VS.80).aspx Differences
Between C++ Templates and C# Generics. It explains why I was having problems
using the approach that works in C++. A friend told me I could use a generic
template to make me my function work for both types. I'm going to take a
crack at that.

C++ allows code that might not be valid for all type parameters in the
template, which is then checked for the specific type used as the type
parameter. C# requires code in a class to be written in such a way that it
will work with any type that satisfies the constraints. For example, in C++
it is possible to write a function that uses the arithmetic operators + and -
on objects of the type parameter, which will produce an error at the time of
instantiation of the template with a type that does not support these
operators. C# disallows this; the only language constructs allowed are those
that can be deduced from the constraints.

"Mike Buckingham" wrote:

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



.



Relevant Pages

  • Re: Cannot apply indexing with [] to an expression of type with ge
    ... template, which is then checked for the specific type used as the type ... will work with any type that satisfies the constraints. ... The compiler is basically just cutting and pasting ... generics exist at both compile-time and run-time. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Cannot apply indexing with [] to an expression of type with ge
    ... I said generic template but I meant generic interface. ... will work with any type that satisfies the constraints. ... compiler doesn't seem to be able to figure it out. ... think you can provide a constraint, thus using generics is not a good idea. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: The "()" operator revisited.
    ... > the compiler could re-instantiate its body. ... either the constraints have to ... Rather than addressing what you said, let me describe how generics ... formals, when the generic is compiled, when the generic instantiation is ...
    (comp.lang.ada)
  • Re: Cannot apply indexing with [] to an expression of type with ge
    ... I was able to use constraints to make it work ... compiler doesn't seem to be able to figure it out. ... 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 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delphi to Visual Studio Conversion Project
    ... Templates are in fact really compiled at the moment they are used. ... what the constraints specify, i.e. you can only cast the type to ... When you compile the generics, all the capabilites are already tested ... compiler checked before use. ...
    (borland.public.delphi.non-technical)