|
mschuckmann wrote: The wayI understood the where statement is that I'm telling it to accept objects derived from type Base or that have the same interface as type Base.
That is correct; and it also applies to SomeMethod() in my example.
mschuckmann wrote: within a Generic you lose all static type information
That is not correct: you dont loose all information, both the Generic method and the
SomeMethod method accept Base objects or Base derivatives, and these objects keep their characteristics.
It is only the way you define your classes (thru the virtual/override/new keywords)
that decides whether you get the functionality of the base method or the more specialized
method. That has nothing to do with generics.
C# offers two ways to get the specialized method: by using the specialized type (Child.Name),
or by using virtual methods.
mschuckmann wrote: a generic is no better than a class or method that takes a common ancestor class
That is correct if you use where T : type.
But generics can be used without type constraint (acting somewhat like C++ templates).
And they can be used with more complex constraints; read up on "where" in MSDN for that.
|
|
|
|
|
Luc Pattyn wrote: That is not correct: you dont loose all information, both the Generic method and the
SomeMethod method accept Base objects or Base derivatives, and these objects keep their characteristics.
The objects themselves may keep their characteristics, because after all they are objects, but they no longer behave like themselves the with respect to non-virtual methods and this is counter intuitive to anyone who has done extensive template programing.
Luc Pattyn wrote: It is only the way you define your classes (thru the virtual/override/new keywords)
that decides whether you get the functionality of the base method or the more specialized
method. That has nothing to do with generics.
But the symantics of how they've implimented the generic feature makes the object behave differently with in the code of the generic (effectively making it lose it's type within the Generic), what is the point of providing the generic with specific type information (i.e. printName<child>(c) ) on the type I'm passing it if it's just going to ignore it and staticely the object to the lowest common denominator implimentation for non-virtual methods. In my opinion this hugely limits the functionality of generics when compared to templates and I find it hard to believe that anyone actually thinks this is good behavior.
Luc Pattyn wrote: That is correct if you use where T : type.
But generics can be used without type constraint (acting somewhat like C++ templates).
And they can be used with more complex constraints; read up on "where" in MSDN for that.
If you leave out the where constraint you've essentially specified a generic with a constraint of object and you can't call any methods except those declared by object. Therefore my example code will not compile without the where constraint. So sure you can impliment generics without type constraints but don't expect to do anything interesting in your generic code.
C++ templates effectively impliment constraints but much more flexibly and without any new confusing and limiting syntax. They do this by verifying any type they are instantiated with impliment (or claim to impliment) any methods that are is used by the template at compile time.
Thanks again
Matt S.
|
|
|
|
|
mschuckmann wrote: If you leave out the where constraint you've essentially specified a generic with a constraint of object and you can't call any methods except those declared by object. Therefore my example code will not compile without the where constraint. So sure you can impliment generics without type constraints but don't expect to do anything interesting in your generic code.
I do agree to your point of view, that the limitation of generics in regard to derived classes are unexpected. As a workaround you can use an interface, that defines the required methods, as the constraint. However, you must explicitly specify the interface for each class:
interface Named
{
void Name();
};
class Base : Named
{
public void Name()
{
Console.WriteLine("I'm a Base class");
}
};
class Middle : Base, Named
{
public new void Name()
{
Console.Write("I'm a Middle class");
}
};
class Child : Middle, Named
{
public new void Name()
{
Console.Write("I'm a Child class");
}
};
class Program
{
static void PrintName<T>( T instance ) where T : Named
{
instance.Name();
}
static void Main( string[] args )
{
Child c = new Child();
PrintName( c );
Console.ReadLine();
}
}
Regards,
Tim
|
|
|
|
|
I am trying to Bind data to DropDownLists and a CheckBox on differents Views of the MultiView. However I get an error whenever I try to Bind the Data. I did it in the Code Behind but cannot figure out what event in what object I should be putting the code in. Can anyone assist?
~Candi
|
|
|
|
|
Hope the Article by verinder bindra here[^] in CP will help you.
Regards,
Satips.
|
|
|
|
|
I have to insert 173 values from a DataTable into 173 existing fields in a SQL table. The values are of all different types. Any suggestions on doing this without taking a very very long way? Thank you!
Vicky
|
|
|
|
|
kissa49 wrote: Any suggestions on doing this without taking a very very long way?
By writing a simple C# program to do it?
|
|
|
|
|
right. i can just do an insert statement, except i have 173 fields and i have to specify each name and cast each data type
Vicky
|
|
|
|
|
Do you have a specific problem you need solving because we'll need more information if we're to help you?
|
|
|
|
|
Find your DBA, and ask him to write you a stored procedure for the insert on his massively dumb table, 173 columns is insane!
|
|
|
|
|
Yes, its a general table that gets normalized later. I have a DataTable containing the data that needs to go into this table. i was trying to do SQLBulkCopy, but that wouldnt work before the fields in the SQL table are of different data types and what goes in are Strings. So... I was trying to do insert SQL statements from the code.
Vicky
|
|
|
|
|
What's the source of the data? How does it get into the DataTable?
|
|
|
|
|
I write it into the table from a txt file
Vicky
|
|
|
|
|
When I have to do something like that I use bcp , the SQL Server command-line utility for bulk copy, to load the raw data into a (temporary) table and I have a trigger on the table that handles converting and moving the data to the actual destination(s).
Be sure to specify -h"FIRE_TRIGGERS" when running bcp .
|
|
|
|
|
perhaps you would like to share some details on that trigger?
Thanks.
Vicky
|
|
|
|
|
Sure, I'll work up an example if you like.
-- modified at 20:12 Tuesday 3rd July, 2007
And is it CSV or some other format?
|
|
|
|
|
The data is already in a DataTable in strings. I need to transfer that data into fields into an existing SQL table. The fields are all set to different data types so I have to cast the strings to whatever the fields are.
Vicky
|
|
|
|
|
hi guys, I need help to read a block of data from a text file.
I m using StreamReader object and its method ReadBlock, which is supposed to write certain number of characters into a char[] buffer, but when I debug the code actually the buffer is not being written, all the values are empty.
Please help.
|
|
|
|
|
Show the code, including var declarations and initialization, plus any errors/exceptions.
Does it work at all ? up to a point ? or what ?
|
|
|
|
|
How to pass user defined oracle type as argument to a stored procedure using c#. anyone pls help.
|
|
|
|
|
Dear All,
We have a requirement in our MDI application wherein we have to load the child form with all the master data (from DB) only once. Basically after the form is loaded the first time, the subsequent instances need to load very quickly and not query the DB again.
I am not sure how we can achieve this in MDI. Can you give your valuable suggestion?
Please let me know incase you have additional queries.
Thanks and regards,
ATUL
|
|
|
|
|
Implement a singleton pattern for your db class and cache the db query
only two letters away from being an asset
|
|
|
|
|
If your using DataSet's to retrive the data then you can cache them. The next time the app needs the data it just grabs the cached DataSet.
|
|
|
|
|
Does anyone have a good tutorial or reference as to how to implement a website search function? I used to doing this in coldfusion with verity search and just wanted to know how to go about doing so in .net C#. Thanks in advance to all that reply.
|
|
|
|
|