How To Create A Generic Constructor - BaseClass.FromXml
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)
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
I trying create Application for the BMP Constructor, I already write the code in C# but I can't translate it into VB.NET, because its require pointer, and as far as i know, there are no pointer function in vb.net @@ so how I can translate this:
FileStream fs = new FileStream(OJSPathBox.Text, FileMode.Open); BinaryReader rdr = new BinaryReader(fs); int width = 555; // its just for example int height = 555; //its just for example
[code]....
ptr declaration is using Pointer and I throught, there are no unsafe in vb.net, how I can translate to vb.net language?
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.
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)
I have a class inside a class.I need to expose the properties of the 2nd class to other classes, therefore it is public.However, I do not wish other classes to be able to create instances of this 2nd class, it should only be instantitated from its parent class.I thought I could resolve this issue by making the constructor of the 2nd class private, but this even prevents the parent class from instantiating its child class! How can I work around this, do I have to make the constructor public and allow anybody to create an instance of the class?
We have migrated our Vb6 application to VB.NET using a third party tool. Now we are in a process of Refactoring and introducing object oriented concepts in the application.
In VB6, we were using structures in many places. As a part of introducing object oriented programming,
1. is it a good idea of changing all Structures to Classes? or Is there a concept of "Generic Structure" similar to Generic collections, Generic classes?
2. Can some one guide me any source containing guide lines or best practices for applications that are migrated to VB.NET from VB6 and implementing object oriented programmaing.
Private _myCollection As IList(Of T) Public Property MyProperty(Of T)() as IList(Of T) Get
[Code]....
Basically, I want to have a collection of items that may be of any type. Then, I'll be able to do something like this:
Dim myPropertyValue as <the type of some value> if (MyProperty.Contains(<some value>)) myPropertyValue = CType(MyProperty(<some value>), <the type of some value>)
How can I do this? Or is there a better way than using a generic type?
I am really trying to follow the DRY principle. I have a sub that looks like this?
Private Sub DoSupplyModel OutputLine("ITEM SUMMARIES") Dim ItemSumms As New SupplyModel.ItemSummaries(_currentSupplyModel, _excelRows) ItemSumms.FillRows()
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.
public class CategoryRepository: RepositoryBase<Category>, ICategoryRepository { public CategoryRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
[Code]...
Does anyone has an idea what i should change to let it work and let my UserRepository use the methods in RepositoryBase while implementing the IUserRepository?
I'm trying to create a generic ListView user control that can be used over and over again in different projects. I've got all the basics done but I want to be able to act on click events in the from not the user control. In other words if someone right clicks on the listview, for example, I want the form to know it and be able to act on it.
Under component services, a COM+ component is used by the company, right-clicking it and choosing 'Activation' tab will show the 'constructor string' that is used for DB server connection by all applications. How can I access it the simplest way possible?
I'm looking at the Interop UserControl mechanism that's part of the "Interop Forms Toolkit" version 2.0. (This you to build a .Net UserControl that can be published as a COM object for use on VB6 forms.)
I've started a new project using the "VB6 Interop UserControl" template, and what I see is a class definition that looks like this:
<ComClass(InteropUserControl.ClassId, InteropUserControl.InterfaceId, InteropUserControl.EventsId)> _ Public Class InteropUserControl
[Code]....
without any "Inherits" statement. But if I look in the Class Browser, I can see that this class (not surprisingly) inherits from the WinForms UserControl class. How can it be that the "Inherits UserControl" piece of the class declaration isn't visible anywhere?
Question 634559 also shows an InteropUserControl class declaration without any "inherits UserControl" statement. I must be missing something simple from my VB.Net knowledge. (I do most of my .Net work in C#.)
I have an interface that i have declared some of the basic internal functions, subs, properties, etc. This interface is then implemented into 3 pre-defined classes. These classes are end-use classes so they dont get derived or inherited elsewhere. I am trying to create a generic shared class that can operate on each of these 3 predefined classes and potentially more as long as they implement the interface.
I was trying to call a method using Reflection on a method that has a STRING parameter.However a STRING does not have a parameterless constructor.So instead of something like.>>
Option Strict On Option Explicit On Option Infer Off Public Class Form1
I'm sort of repeating the same steps. There's got to be a way to shorten it.I was able to shorten it before by making a generic function to create the radio buttons.This is the before the rewrite
Public Sub LoadPanels(ByVal tblClient As DataTable) Dim count As Integer = 20 Dim tooltip As New ToolTip Dim countName As Integer = 0
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.
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.
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]
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.
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).
I'm trying to create a generic IsEmpty extension but am having problems specifically with dictionaries: The functions I have created:
<Extension()> Function IsEmpty(null As DBNull) As Boolean Return True End Function[code].....
For my purposes, booleans and numerics are never "empty".The problem is that dictionaries are using the generic (Of T)(val As T) function instead of the (collection As ICollection) function.
i.e.
Dim dict As New Dictionary(Of String,String) Response.Write(dict.IsEmpty)
output is "False"
I tried using (Of K,V)(collection As ICollection(Of KeyValuePair(Of K,V))) to no avail.What am I doing wrong?
I have been using a pretty slick generic invoke method for UI updating from background threads. I forget where I copied it from (converted it to VB.NET from C#), but here it is: Public Sub InvokeControl(Of T As Control)(ByVal Control As t, ByVal Action As Action(Of t))