Telling A Generic Function That It Will Use An Interface With A Constructor?
Jan 2, 2011
i've been trying to toy around with generics in vb.net in an example project. At the moment, it looks like this:
I have an interface called IRow (and a class that implements it as a Datarow).
A second interface is ICanGetByRow, which looks like this:
Public Interface ICanGetByRow(Of T)
Function GetByRow(ByVal Row As IRow) As T
End Interface
The function simply takes an IRow and converts it to T. Thats easy enough. Now, for easier access, i want to implement a function in the IRow interface which takes the row and converts it into said ICanGetByRow.
My interface was enhanced by the following function
Function GetObj(Of T As ICanGetByRow(Of T))() As T
You can probably see the problem. If i implemented it like this:
Public Function GetObj(Of T As ICanGetByRow(Of T))() As T Implements IRow.GetObj
Dim foo As New T
foo.GetByRow(Me)
Return foo
End Function
i wouldn't be allowed to construct a new T, and when i tried to make it work by telling the generic function that my interface has a constructor, he wouldn't let me invoke GetByRow anymore.
Public Function GetObj(Of T As New, ICanGetByRow)() As T Implements IRow.GetObj
Dim foo As New T
foo.GetByRow(Me)
Return foo
End Function
how to tell a generic function that it will get a ICanGetByRow(of T) which has a constructor
View 1 Replies
ADVERTISEMENT
Jun 8, 2009
I have a following code which works fine
MsgBox(AddSomething(Of String)("Hello", "World"))
Public Function AddSomething(Of T)(ByVal FirstValue As T, ByVal SecondValue As T) As String
Return FirstValue.ToString + SecondValue.ToString
[code].....
View 3 Replies
Nov 25, 2011
I am trying to construct a generic interface class with generic functions. My goal was to use this to implement multiple worker classes for database interaction that have the same basic functionality. Each class will deal with different object for example, category, product or supplier but unless the the functions in the interface are generic that this won't work.This is the interface code that I have but I don't know if I have done it correctly. [code]
View 2 Replies
Mar 19, 2010
How can I create a generic constructor? (ie. BaseClass.FromXml(<param>). I'm not sure how to describe this but I'm trying to create a base class that contains a shared (factory) function called FromXml. I want this function to instantiate an object of the proper type and then fill it via an XmlDocument.
For example, let's say I have something like this: [Code]
I'd like to be able to do something like this:
Dim myObject As CustomObject = CustomObject.FromXml(source)
Is this possible?
View 2 Replies
Mar 27, 2009
I know that you cannot specify a constructor in an interface in .Net, but why can we not?
It would be really useful for my current project to be able to specify that an 'engine' must be passed in with the constructor, but as I cant, I have to suffice with an XML comment on the class.
View 7 Replies
Jan 21, 2010
I can do this without problem.
Class A
End Class
Class B : Inherit A
End Class
Dim Obj1 As A = New B
View 4 Replies
Nov 29, 2011
In our current project I am using a generic interface iService which is inherited by all other service interfaces. For instance IService is inherited by ILogService.
The ILogService interface is then implemented by LogService as below:
Public Interface IService(Of T)
Sub Save(ByVal T As IEntity)
Sub Remove(ByVal T As IEntity)
[Code]....
How can I update the method signature so T is displayed as Log?
View 2 Replies
May 25, 2010
I'm trying to create a base interface for a class of mine that uses generics. However, I cannot figure out how to declare a property of the interface when its type won't be defined until the class is initialized.
To clarify, consider List(of T), which according to msdn, implements IList (among other things). Note that IList is not the same as its generic counterpart IList(Of T). So that mean any List(of T) can be converted to IList, which simply returns an object for its items. That's what I want to do with MyClass(of T), be able to cast it as IMyClass regardless of what T is.
But when I try such as:
Interface IMyClass
Prop A as Object
Class MyClass(of T) Implements IMyClass
Prop A as T Implements IMyClass.A
I get a signature error for the last line, even though object is broader than T. What am I doing wrong, or how exactly did Microsoft manage to make List(of T) implement IList?
View 9 Replies
Nov 17, 2009
I want to define a generic interface which will be implemented by an abstract Generic Class. Basically this generic class is a collection class of any class. Interfaces are in a separate project saved as FileReconciliation. Here are the interface definitions
Interfaces
Imports System.Collections
Public Interface ICollectionCommon(Of T As Class)
Inherits IEnumerable
Function Exists(ByVal oKey As Object) As Boolean
[Code] .....
View 3 Replies
Oct 19, 2010
What does this error mean in VB6? Function or interface marked as restricted, or the function uses an Automation type not supported in Visual Basic.
I keep getting it when i call a particular method of a dll that comes with windows xp and beyond (in system32 called upnp.dll)
View 2 Replies
Jun 24, 2011
I have a problem when trying to save a picture box image ( I am using vb2008 express edition)When i load my windows form I create a folder called images and the current date (eg images 24062011) using the following code.
Code:
Dim fold As String = "images" & " " & Format(Now(), "ddMMyyyy")
If My.Computer.FileSystem.DirectoryExists("C:usersmickdesktop" & fold) Then
MsgBox(" file exists")
[code].....
this works fine and creates the folder on my desktop. I then load an image into a picturebox and try to save it using the following code which is where the problem starts.
Code:
Dim filename As String = Format(Now(), "ddMMyyyy")
Dim imagename As String = "image"
Dim fileext As String = Drawing.Imaging.ImageFormat.Bmp.ToString
[code].....
so my problem is why does the 1st statement give me the error as surely as I have declared that fold = the folder name then that path should be correct, as the program is set to save the image automatically, not using the savefiledialogue I don't want to have to manually type in the folder name every time I use the program.
View 6 Replies
Sep 17, 2008
I am trying to create bitmap with my text and transformation. I am sure that there is no error in my code. Because this code is worked previously. Here is my code.
[Code]...
View 2 Replies
Feb 15, 2010
I have an abstract class which requires a delegate to function. I pass the delegate into the constructor. Now that I have a non default constructor I need to call the abstract class's constructors from the concrete class which means that I need to use MyBase.New(...). I have included a quick example below.
Public MustInherit Class BaseClass
Public Delegate Sub WorkMethod()
Private _Work As WorkMethod
[code]....
I have tried to do this but I keep getting the following error: "Implicit reference to object under construction is not valid when calling another constructor".Can I not do what I am trying to do above? I initially had the delegate setup in its own setter method. But then I am creating a deceptive API because it does require a point to a method to work properly.
View 3 Replies
Nov 2, 2009
what's the rationale behind this limitation: Constructor call is valid only as the first statement in an instance constructor i want to pass an argument to my constructor which validates this argument and calls mybase.new according to this argument but it doesn't let me
example:
Public Class prob
Inherits System.ApplicationException
Public Sub New(ByVal problem As String, ByRef inner_exception As Exception)
[code]....
View 16 Replies
May 31, 2011
Anyone want to try converting this to VB?
[Code]...
It is supposed to sort any type of object. Taken from here
View 1 Replies
Jun 2, 2010
Question: I want to call a generic function, defined as:
Public Shared Function DeserializeFromXML(Of T)(Optional ByRef strFileNameAndPath As String = Nothing) As T
Now when I call it, I wanted to do it with any of the variants below:
Dim x As New XMLserialization.cConfiguration
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of x)()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(GetType(x))()
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of GetType(x))()
But it doesn't work.I find it very annoying and unreadable having to type
x = XMLserialization.XMLserializeLDAPconfig.DeserializeFromXML(Of XMLserialization.cConfiguration)()
Is there a way to call a generic function by getting the type from the instance ?
View 5 Replies
Jun 7, 2012
I have a grid view with a lot of columns, all need to be sorted on. I have seen a few snippets out there, but I cannot get any of them to work with my example. Here is what I have so far.
Protected Sub gvSearch_OnSorting(ByVal sender As Object, _
ByVal e As GridViewSortEventArgs)
If Not Session("sort") Is Nothing Then
[code].....
View 1 Replies
Oct 12, 2011
written a generic "LaunchForm" function? For all the menu items I have that open a form, I would like to write one function that will launch the form as opposed to writing the same code several times.
View 2 Replies
Sep 16, 2011
I have an abstract class in VB.NET. I want all classes that inherit from this class to return whatever value makes sense. For example, it could be an Decimal, Integer, String. How can I delcare the function in the abstract base class to allow for this? Is this even possible?
[Code]...
View 2 Replies
Oct 14, 2010
I have created a couple of simple functions in VB.NET that simply return a control of a certain type that I already know, such as HtmlInputHidden, Label, etc. That means that each of the functions is created only for that special purpose. What I'd like to do is combine all those functions into one function using generics. The common things shared by each of the functions is a control Id and a control type.What I have got so far is:
Public Function GetControl(Of T)(ByVal ctrlId As String) As T
Dim ctrl As Control = Me.FindControl(ctrlId)
If (Not ctrl Is Nothing) Then
[code]....
But the line " GetControl = CType(ctrl, T)" is giving me a compile error:
Value of type 'System.Web.UI.Control' cannot be converted to 'T'
This is in .NET Framework 2.0.
View 2 Replies
Apr 1, 2009
I am attempting to create a generic function that the students in my introductory VB .NET course can use to search a single dimension array of a structure.[code]...
My question is: Is there a way to reference the individual structure fields in the array from inside the function? I was trying to make it generic so that the student could simply pass their array into the function - their structure may be named differently than mine and the field names may be different.
I suspect there are other ways to deal with this situation, but I was trying to keep it to just a simple single-dimension array of a structure. I don't think it is possible to do what I want, but I wondered what others thought.
View 5 Replies
Aug 24, 2010
I am trying to write a generic function that will check each database parameter to see if it is null, and if so, return DBNull; if not, return the object.So here is my function:
Public Shared Function CheckForNull(ByVal obj As Object) As Object
If obj <> Nothing Then Return obj Else Return DBNull.Value
End Function
[code].....
View 1 Replies
Sep 2, 2009
I must be doing something wrong here (because really, what are the chances of me tripping over another bug in the Vb.net compiler?)I have a static generic function in .net 2.0 Vb code, I thought it was time to "upgrade" it to be an extension method, but the compiler complains with Extension method 'AddIfUnqiue' has type constraints that can never be satisfied.
Here's a trivial example that displays the same problem. The old static version (which works fine) followed by the extension method
Public Class MyStaticClass
Public Shared Sub AddIfUnqiue(Of T, L As {List(Of T)})(ByVal this As L, ByVal item As T)
If this.IndexOf(item) < 0 Then
[code].....
View 1 Replies
Feb 16, 2011
I can create classes that use generics, however I was wondering if it is possible to apply this to a shared method rather than rely on the caller casting to the correct type.This is the method:
Public Shared Function DeserializeObject(ByVal serializedXml As String, ByVal givenType As System.Type) As Object
Dim serializer As New XmlSerializer(givenType)
Return serializer.Deserialize(New IO.StringReader(serializedXml))
End Function
I'm pretty sure this can't be done, but thought I'd check (if so Extra points will be awarded if someone can technically explain why the compiler can't do this)..
View 3 Replies
Oct 26, 2011
My question here is, what are the negative effects of using a generic function such as this? Calling this function does work, and in a test console module, it compiles perfectly fine. I do know this is not a strongly typed function, and is %100 bad practice. But it works perfectly. The purpose of a function like this, would be to handle string input that needs to be inserted in a particular format depending on the type. I also read some other questions on this here on stackoverflow, and suggestions pointed to using (Of T) functions, and variations like that. Why not do it this way? or is there another simple way of accomplishing this without creating a whole nothing class or larger amounts of code.[code]
View 1 Replies
Jan 18, 2011
I like to turn Option Strict On I cant figure out how to write the generic version of this function?
Public
Shared
Sub UpdateItem(ByRef
oldValue As
[code]....
View 8 Replies
Apr 29, 2010
Currently I have written a function to deserialize XML as seen below.How do I change it so I don't have to replace the type every time I want to serialize another object type ? The current object type is cToolConfig. How do I make this function generic ?
Public Shared Function DeserializeFromXML(ByRef strFileNameAndPath As String) As XMLhandler.XMLserialization.cToolConfig
Dim deserializer As New System.Xml.Serialization.XmlSerializer(GetType(cToolConfig))
Dim srEncodingReader As IO.StreamReader = New IO.StreamReader(strFileNameAndPath, System.Text.Encoding.UTF8)
Dim ThisFacility As cToolConfig
[code]....
View 1 Replies
Jan 13, 2011
I would like to write a generic function that would search a List(Of T) for all elements of type TFilter and return a List(Of TFilter) which comprises those elements.
I've tried this:
Public Function FilterList(Of T, TFilter)(ByVal ListToFilter As List(Of T)) As List(Of TFilter)
Return ListToFilter.FindAll(Function(z) z.GetType.Equals(GetType(TFilter))).ConvertAll(New Converter(Of T, TFilter)(Function(z) CType(z, TFilter)))
End Function
But, it gives the following error:
Value of type 'T' cannot be converted to 'TFilter'.
View 2 Replies
Jun 4, 2012
What's the best way to handle this situation: I have this generic function
Function FieldValue(Of T)(row As DataRow,fieldName As String) As T
Return If(row.IsNull(fieldName),Nothing,CType(row(fieldName),T))
End Function
In the special case where the field value is null and T is String, I want to return String.Empty instead of Nothing.
View 1 Replies
Feb 23, 2010
I have a generic repository that I communicate with my object context with. My object context has function imports that I want to be able to call through my generic repository. Currently I call them through an "Execute" function, but this is not ideal because I have to pass in my function name and parameters as strings which can cause run-time exceptions. My function imports map to complex types.
Is there a way to do something similar to:
Dim rep As IRepository(Of ComplexType)
Dim type As ComplexType = rep.Find(where:=Function(t) t.FunctionImport(parm, parm)).First()
Here is my generic repository as is:
[Code]...
View 1 Replies