How To Match Generic Types, Without The Type Parameter In A IF-ELSE Statement
Jan 23, 2009
I have bee trying to figure out, how to match generic types, without the type parameter in a IF-ELSE statement - heres what i mean:
<P>IF TypeOf(Obj) Is GenericTypeExample(Of ???Any???) Then</P>
<P>End if</P>
View 7 Replies
ADVERTISEMENT
Mar 31, 2011
I have a table that has a "date" column and an "amount" column. I also have a form with two DateTimePickers ("dtp1" and "dtp2"), a button and a label. The label should show the total amount calculated from the rows whose "date" value is between the dates picked by the user with the DateTimePicker controls.
I keep getting an error with my SQL statement, and I don't know what to try. My code is as follows:
Private Sub getData(ByRef total As Double)
Dim connection As OleDbConnection
connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\proyectogestion.mdb")
[Code]....
View 4 Replies
Dec 14, 2011
I have a generic Class I'm using to hold information loaded from a database.I have a method which takes a DataRow as an argument, uses the object's known column name and extracts the data from the DataRow, such that:Dim loadData As T = CType(myDataRow("myColumnName"), T))works as my default assignment in most cases.Unfortunately, due to some horrifying design constraints, some of my columns may be null, and may also be taken from enumerations.This means that when <T> is Nullable(Of SomeEnumeration) the above code does not work because I can't cast 0 directly to SomeEnumeration.Zero.Is there some way to check whether <T> is Nullable(Of [Enum])? Or some way to write a method which allows Integers to be cast to Nullable(Of [Enum])?I feel like I'm forgetting something that would allow me to write one of the other of these, but my weak google-fu is turning up nothing.
EDIT: Okay, thanks to dasblinkenlight's answer below, I can detect when this circumstance is occurring, but what I need to do now is to take a type <T> which I know is Nullable(Of SomeClass), get a type reference to SomeClass and then create a new object of type Nullable(Of SomeClass) and assign that to LoadData.My problem was that I had a lot of difficulty in finding any function which would accept baseType as an actual Type.Parse accepted baseType as a parameter, I knew baseType was an [Enum] type because of dasblinkenlight's code, so I was, in this instance, able to code a solution. It's a solution which is very specific to my problem (i.e., T is Nullable(of SomeEnumeration)), but it's a solution nonetheless.
View 2 Replies
Mar 3, 2011
I have the following VB.NET function, for example:
Public Function MyFunction (Of TData) (ByVal InParam As Integer) As TData
End Sub
How do I, in a function, determine if TData is a NULLable Type?
View 3 Replies
Feb 14, 2011
say I have two POCOs I'm using in EF code first
Public class C1
property F1 as integer
End Class
Public class C2
property F2 as String
End Class
I want to have a function that will create a list either of C1 or C2, to be used in some generic operation, such that
[Code]...
View 1 Replies
Feb 21, 2011
Anyone know a good workaround for the lack of an enum generic constraint?[cod]e...
Is is possible to limit the generic type parameter [I don't know if that's the right name] to an Enum?[code]...
View 4 Replies
Jun 25, 2010
(Not really sure if I phrased the question correctly...) I want to create a lambda expression that would take an Object, attempt to convert it to a passed-in Type, and print to the console whether it was successful or not. At a glance, the lambda expression may seem a pretty silly way to accomplish this task, but I'd really like to know what I'm doing wrong, so I can better grow my skill set.
VS gives me a designer error about the second "T" in the expression below, telling me it isn't defined) This is where I left off:
[Code]...
View 1 Replies
Apr 13, 2012
Consider:
Public MustInherit Class Column
Public ReadOnly Property ReturnSomethingUseful() As Object
Get
'return something useful
[code]....
But this gives the following error:
Public Overrides Function ParseValue(sValue As String) As Boolean'
cannot override 'Public Overridable Function ParseValue(sValue As String) As Object'
because they differ by their return types.
I accept that you can't do this, but I'd like to be able to preserve the semantics of what I'm. trying to do, which is to have an untyped version that deals with Object, but a typed version in derived classes that knows about the specific type T.
View 2 Replies
Aug 14, 2010
I have a Journal that records entries for different types: Journal(Of ParentT)
[Code]....
View 2 Replies
May 3, 2011
I have an Entity I'm pulling three columns from Title (nvarchar(256)), Popularity (int), and Type (int). I'm then trying to use QueryExtender on a radiobuttonlist to allow the end user to filter out all but specific results but I keep getting an "Argument Types do not match" error. Here is the actual code:
<asp:QueryExtender ID="QueryExtender1" runat="server" TargetControlID="EntityDataSource1">
<asp:SearchExpression DataFields="Type" SearchType="StartsWith">
<asp:ControlParameter ControlID="rblTypes" PropertyName="SelectedValue" />
</asp:SearchExpression>
</asp:QueryExtender>
[Code] .....
View 1 Replies
Jun 30, 2011
I am trying to create a list of a generic type in vb.net 2.0 framework.
This is the generic type definition:
Public Class GenericParamMap(Of T)
Public Sub New(ByVal pParamName As String, ByVal pPropValue As T)
mParamName = pParamName
[Code]....
The compiler does not allow a "T" in the method's parameter because it's not defined, but I'm not sure how or where to define it. I thought it was okay to have a generic method definition.
View 2 Replies
Feb 8, 2012
I am trying to write a generic method, to avoid code duplication, which will create or activate a Form as an MDI children, based on its type. But I have to lines in error (see comments).
[Code]...
View 4 Replies
Apr 20, 2010
Public Function CastToT(Of T)(ByVal GenericType(Of Object) data) As GenericType(Of T)Return DirectCast(data, GenericType(Of T))End Function
The above clearly does not work. Is there any way to perform this cast if I know that all objects inside data are in fact of Type T?
View 2 Replies
May 4, 2011
Is there any way to define a Generic in VB.NET which will accept only Singles or Doubles? I tried the following things, based on what I've found online, but none compile:
Dim target As Nullable(Of {Single, Double})
Dim target As Nullable(Of T As {Single, Double})
Dim target As Nullable(Of T {Single, Double})
Dim target As Nullable(Of Single, Double)
Dim target As Nullable(Of T As Single, Double)
I want to specify that target can either be a Single? or a Double? only.
View 1 Replies
Dec 2, 2010
I'm trying register presenters with Windsor using the convention based method but trying to do this in VB.NET, but the problem is it does not want to compile this statement:
Dim type = GetType(AbstractPresenter(Of))
I am getting : Too few type arguments to AbstractPresenter(Of TView, TPresenter)
Which I don't understand because this is a valid statement according to question. Also showing valid in other C# to VB.NET converters when converting typeof(AbstractPresenter<>).
View 1 Replies
Nov 15, 2010
Maybe I am just not reading the MSDN documentation correctly, but given a function that takes in one string parameter and returns type T, how can this be specifed as a shared function using Func()?
MSDN says Func(Of In T1, Out TResult), but all of their examples use the same data type, i.e., Func(Of String, String). I want to do Func(Of In String, Out T), where T is arbitrary (but I can constrain it if necessary by a base class). I want it shared/static at the class level, yet the encapsulating class will itself not be a generic class. It seems in this specific scenario, it's impossible to do what I want because the compiler would have no way of knowing what Type T is at runtime.
So is it possible to do generics on delegates or anonymous lambda expressions in VB.net (not C#)?
View 1 Replies
Oct 23, 2009
I am using VB.Net. I have an object class called clsA(of T as clsB). Depending on what T is, I want to do different things. Let's say that clsC and clsD both inherit clsB and therefore can be used for T.
If I have an instance of clsA(of clsC), how can I get the inside type (i.e. clsC) using reflection?
View 4 Replies
May 10, 2011
Well this is maybe a little different than other array based conversion questions, but I'm dealing with forced typing concerns due to interface implementation and thus would like a few opinions on the process. I've basically redesigned the Collection suite for .Net because I don't like the extreme simplicity of the default Collections and dictionaries, as well, when I want the more complex concepts I don't want to have rewrite the same collection over an over again.
[Code]...
View 2 Replies
Nov 29, 2010
Is there an exhaustive list of all of the "base" (not used in an object-oriented sense but more in a common sense) generic types in the 4.0 .NET Framework? I have found this list that I often send newer/mid-level devs to so they can understand how non-generic types map to generic types, but this is by no means exhaustive. I'm looking for something that also includes things such as KeyValuePair<>, Tuple<>, and other basic generics that may not be very-well known. Interfaces such as IObservable<> would be nice but not necessarily required.
View 6 Replies
Mar 5, 2010
Is there a way to use a generic that only accepts integer types?
I have a Clamp(value, min, max) function, and it would be nice if it could accept ints, uints, floats, shorts, ect without having to write an individual function for each type.
View 1 Replies
May 25, 2011
NOTE: I am mixing VB and C# - snippets come from different librariesI have a VB class definition as follows:
Public Class Session(Of TConnectionBuilder As {DbConnectionStringBuilder, New}, _
TConnection As {DbConnection, New}, _
TDataReader As {DbDataReader})
[code].....
View 3 Replies
Aug 28, 2009
I'm trying to write a class that will be in charge of persisting application options. Since the options need to be persisted the values that I'm sent must be serialisable.
Initially I thought I've be able to write a method with a signature like this[code]...
View 4 Replies
Mar 9, 2011
I want to have a "template" function that can receive different parameter and a type parameter, like:[code]But Vb told me that tupeList is not defined... is there a way I can do that?
View 2 Replies
Jan 19, 2012
addressing the need for getting the bytes of an object. But I am wondering if there is an approach to calling BitConverter.GetBytes on a generic type where I know the type is a primitive (Int32, UInt16, etc).
Public Sub Foobar(Of T as Structure)()
Dim x as T 'Assume T is declared as Int32
Dim y() as Byte
y = System.BitConverter.GetBytes(x)
End Sub
The above will throw your usual error:
Overload resolution failed because no accessible 'GetBytes' can be called with these arguments:
'Public Shared Function GetBytes(value As Double) As Byte()': Value of type 'T' cannot be converted to 'Double'.
'Public Shared Function GetBytes(value As Single) As Byte()': Value of type 'T' cannot be converted to 'Single'.
[code]....
One solution I think would work is a large Select Case calling GetType(), but that is horrendously slow (because of boxing) and looks ugly. I would think that since I call my higher level class with a primitive data type for T, that the compiler would be smart enough to figure it out, but I assume I am not providing enough information for it to derive what T's underlying value is at compile time for the invoked instances.
View 6 Replies
Jun 4, 2009
Suppose I want to write a function like the following (as usual, a trivial example for illustrative purposes):
Public Function calcSqSum(Of T)(ByVal list As IEnumerable(Of T)) As T
Dim sumSq As T
For Each item As T In list
[Code]....
As you can probably guess, this function causes an error because a generic object is not guaranteed to implement the + operator. As far as I know, though, any numerical type (Integer, Double, Decimal, etc.) will.
Is there a way to write a (quasi-)generic function that can accept any numerical type, without having to explicitly overload the function for every such type yourself?
Alternatively, I suppose an equally acceptable solution would be to somehow check if a type implements the '+' operator (or any operator generally associated with numerical types and used by the function).
View 4 Replies
May 5, 2009
I'm compiling a VB.Net 2.0 app (created in VS2008) using msbuild, and now I've added a generic return type, it's giving me the following:
Warning: Type library exporter encountered a generic type instance in a signature. Generic code may not be exported to COM.
Having just spent ages removing all of the previous warnings, I don't really want to add a new one. Any idea how to get rid of it (aside from not using generics)?I don't know what details I'd put in the attribute, or what number to put in the project-level ignore list.
View 1 Replies
Sep 12, 2011
I have a question about ASP.Net, visual basic I have 2 LINQ query's, the first one works, the second one doesnt, produces a
"Unable to cast object of type 'System.Data.Objects.ObjectQuery'1[SelmaV2.Products]' to type 'System.Collections.Generic.List'1[System.String]'.
[Code]....
View 1 Replies
Feb 14, 2012
In trying to add a bit of usage variety to a generic class I'm working on, I ran into this issue with trying to cast an object into an interface instance where the interface is defined inside the generic class.
[Code]...
View 1 Replies
Mar 30, 2010
1) VB Allows non-type template parameters2) VB supports explicit specialization 3) VB allows the type parameters to be used as the base class for the generic type4) VB allows a generic type parameter itself to to be a generic 5) VB enforces that all codes are valid for all types of parametrs
View 1 Replies
Nov 17, 2011
If I have the following structure:[code]How can I select it in a conditional like this? [code]I know that myUsers.Contains(.ID = "1") is totally wrong, but I am curious how to do something like that?Is it possible? Is this a job for LINQ?
View 3 Replies