I want to define a generics method so the user can determine what type they
expect returned from the method. By examining the generics argument, I
would determine the operation that needs to be performed and do just that.
However, out of the two possible ways of doing this, neither seems to work.
I thought I could either...
1) overload the method just based on the generics argument
Public Function GetData (Of T As SqlDataReader) (ByRef command as
SqlCommand) As T
...
End Function
Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As T
...
End Function
....or...
2) determine the type without overloading and make sure to return the wanted
type
Public Function GetData (Of T) (ByRef command as SqlCommand) As T
Select Case True
Case TypeOf(T) Is GetType(SqlData Reader)
Return command.Execute Reader()
Case TypeOf(T) Is GetType(DataSet )
.....(other code)
oDataAdapter.Fi ll(oDataSet)
Return oDataSet
End Select
End Function
But, #1 doesn't work because changing the generics type won't overload the
function, and #2 doesn't work because I get errors along the lines of "Value
of type '...' cannot be converted to 'T'"
I understand #1 not working, but as long as I'm playing it safe with the
type conversions, why can't I get #2 to work? 4 1936
Might I suggest
Public Function GetDataSet(ByVa l command As SqlCommand) As DataSet
and
Public Function GetDataReader(B yVal command As SqlCommand) As
SqlDataReader
If you use a generic method in this situation the user will always
have to specify the type with the method anyways, so building the type
into the method name is just as easy to use.
In short, this situation does not sufficiently benefit from using
Generics.
If you really want to do your #2, you need to add a DirectCast call to
get around the implicit conversion error.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Mon, 4 Jun 2007 09:17:18 -0700, "Random" <ci*******@hotm ail.com>
wrote:
>I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be performed and do just that. However, out of the two possible ways of doing this, neither seems to work.
I thought I could either...
1) overload the method just based on the generics argument Public Function GetData (Of T As SqlDataReader) (ByRef command as SqlCommand) As T
... End Function
Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As T
... End Function
...or...
2) determine the type without overloading and make sure to return the wanted type Public Function GetData (Of T) (ByRef command as SqlCommand) As T
Select Case True
Case TypeOf(T) Is GetType(SqlData Reader)
Return command.Execute Reader()
Case TypeOf(T) Is GetType(DataSet )
.....(other code)
oDataAdapter.Fi ll(oDataSet)
Return oDataSet
End Select End Function
But, #1 doesn't work because changing the generics type won't overload the function, and #2 doesn't work because I get errors along the lines of "Value of type '...' cannot be converted to 'T'"
I understand #1 not working, but as long as I'm playing it safe with the type conversions, why can't I get #2 to work?
Thanks, Sam. Actually, being able to use the same method name will help
with the implementation part of what I'm doing.
And, I'd already tried both CType and DirectCast - still won't convert to
'T'.
I just tried a third way to fix this, and apparently if I specify Object as
the return type, I'm okay. Odd.
"Samuel R. Neff" <sa********@nom ail.comwrote in message
news:2f******** *************** *********@4ax.c om...
>
Might I suggest
Public Function GetDataSet(ByVa l command As SqlCommand) As DataSet
and
Public Function GetDataReader(B yVal command As SqlCommand) As
SqlDataReader
If you use a generic method in this situation the user will always
have to specify the type with the method anyways, so building the type
into the method name is just as easy to use.
In short, this situation does not sufficiently benefit from using
Generics.
If you really want to do your #2, you need to add a DirectCast call to
get around the implicit conversion error.
HTH,
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Mon, 4 Jun 2007 09:17:18 -0700, "Random" <ci*******@hotm ail.com>
wrote:
>>I want to define a generics method so the user can determine what type they expect returned from the method. By examining the generics argument, I would determine the operation that needs to be performed and do just that. However, out of the two possible ways of doing this, neither seems to work.
I thought I could either...
1) overload the method just based on the generics argument Public Function GetData (Of T As SqlDataReader) (ByRef command as SqlCommand) As T ... End Function
Public Function GetData (Of T As DataSet) (ByRef command as SqlCommand) As T ... End Function
...or...
2) determine the type without overloading and make sure to return the wanted type Public Function GetData (Of T) (ByRef command as SqlCommand) As T Select Case True Case TypeOf(T) Is GetType(SqlData Reader) Return command.Execute Reader() Case TypeOf(T) Is GetType(DataSet ) .....(other code) oDataAdapter.Fi ll(oDataSet) Return oDataSet End Select End Function
But, #1 doesn't work because changing the generics type won't overload the function, and #2 doesn't work because I get errors along the lines of "Value of type '...' cannot be converted to 'T'"
I understand #1 not working, but as long as I'm playing it safe with the type conversions, why can't I get #2 to work?
But if you specify Object as a return type then there's really no
point in using Generics....
Was the error different when you tried using DirectCast ?
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Mon, 4 Jun 2007 09:50:15 -0700, "Random" <ci*******@hotm ail.com>
wrote:
>Thanks, Sam. Actually, being able to use the same method name will help with the implementation part of what I'm doing.
And, I'd already tried both CType and DirectCast - still won't convert to 'T'.
I just tried a third way to fix this, and apparently if I specify Object as the return type, I'm okay. Odd.
Nope. No difference in error when using DirectCast.
By passing in the type (I also found out I could just pass in Type as a
parameter and that works, too), I can discover how my method needs to
perform.
"Samuel R. Neff" <sa********@nom ail.comwrote in message
news:lk******** *************** *********@4ax.c om...
>
But if you specify Object as a return type then there's really no
point in using Generics....
Was the error different when you tried using DirectCast ?
Sam
------------------------------------------------------------
We're hiring! B-Line Medical is seeking .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.
On Mon, 4 Jun 2007 09:50:15 -0700, "Random" <ci*******@hotm ail.com>
wrote:
>>Thanks, Sam. Actually, being able to use the same method name will help with the implementation part of what I'm doing.
And, I'd already tried both CType and DirectCast - still won't convert to 'T'.
I just tried a third way to fix this, and apparently if I specify Object as the return type, I'm okay. Odd. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: KC |
last post by:
Could some one explain to me the casting rules for sending generic lists, ex. List<Person>, to a function that accepts List<object>? I cannot get the following easy-cheesy app to work. I get the following error:
Argument '1': cannot convert from 'GenericsPOC.ArticleCollection' to 'System.Collections.Generic.IList<object>'
I tried casting...
|
by: Gene Vital |
last post by:
Hi all,
I need some help in understanding how to use Generics. I have a class
based on a user control that can be put on any Container at runtime, I
want to be able to call a method on the parent class without knowing the
type of the parent class, can this be done with C#?
I thought this was what Generics was supposed to be all about but...
|
by: Kris Jennings |
last post by:
Hi,
I am trying to create a new generic class and am having trouble casting
a generic type to a specific type. For example,
public class MyClass<Twhere T : MyItemClass, new()
{
public MyClass() { }
public void AppendItem()
|
by: Ajeet |
last post by:
hi
I am having some difficulty in casting using generics. These are the
classes.
public interface IProvider<PROF>
where PROF : IProviderProfile
{
//Some properties/methods
}
|
by: psyCK0 |
last post by:
Hi all!
I have a problem of casting generics to their base type. In the code
below I first define a BaseList class that can hold items of any type
that inherits from BaseItem. I then define a DerivedList that can hold
items of the type DerivedItem, which inherits from BaseList.
So far so good... But if I somehow get an object that I know is...
| |
by: rkausch |
last post by:
Hello everyone,
I'm writing because I'm frustrated with the implementation of C#'s
generics, and need a workaround. I come from a Java background, and
am currently writing a portion of an application that needs
implementations in both Java and C#. I have the Java side done, and
it works fantastic, and the C# side is nearly there. The...
|
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= |
last post by:
Hi all
I have some problems with Crystal Reports (Version 10.2, Runtime 2.0).
In Section3 I have added a OLE Object (Bitmap).
Now when I open the report in my code I would
like to set this OLE Object (load a picture from a given path).
Something like this I would expect:
_reports.crImage local_Report = new _reports.crImage();
|
by: Anders Borum |
last post by:
Hello,
I've worked on an API for quite some time and have (on several occasions)
tried to introduce generics at the core abstract level of business objects
(especially a hierarchical node). The current non-generic implementation is
functional, but not as clean as I would like. Although not sure, I believe
my problems stem from lacking...
|
by: Tony Johansson |
last post by:
Hello!
I have read that in practice, casting proved to be several times faster than
using a generic.
So the main reason to use generics is not that the performance is better
because that's not the case.
The only reason is that it's type-safe.
I must ask if anyone has made any significant performance improvement using
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |