Make A Delegate To A Constructor?
Jul 24, 2009Is it possible to make a Delegate to a Constructor? For example:
Class Bar
...
End Class[code]......
Is it possible to make a Delegate to a Constructor? For example:
Class Bar
...
End Class[code]......
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.
While creating instance of a Delegetae there comes a intellicence AddressOf , Sub and Function .
AddressOf signifies the address of the function or Sub , what does Sub or Function signifies ?
I got 2 classes in a form, and I would like to pass a value from a textbox to the other class, so I decided to use Deligate.
Below is the code.
Public Delegate Sub delPassSize(ByVal text As TextBox)
'To get size from Class Calculate(Class 2)
Public Sub GetSize(ByVal txtForm1 As TextBox)
[code]....
and the error message is 'cls.Getsize is a delegate type and requires a single 'addressof' expression as the only argument to the constructor.'
I got this Error. below is my code.
GenerateTheList is function.Need help
Private Sub buttGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles buttGenerate.Click
Dim thread1 As New Thread(New ThreadStart(GenerateTheList))
thread1.Start()
End Sub
from the documentation we have this: Multicast Delegate: Represents a multicast delegate; that is, a delegate that can have more than one element in its invocation list.
so am i right to say that Multicast delegate is no different from a normal delegate other than the fact that it has arguments. so System.Action is a 'normal' delegate whereas System.Action(T)(Byval obj as T) is a multicast delegate?
Private _bgWorker As BackgroundWorker = Nothing
Private _bgWorkerMessage As String = String.Empty
Private _bgPercentComplete As Integer = 0
[code]......
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]....
I've created a Delegate that I intend to call Async.
[Code]...
Code:
Public Class SendPings
Shared Sub New()
AddHandler Post.Saved, AddressOf Post_Saved[code].....
I get a error" Method 'Private Shared Sub Ping(item As BlogEngine.Core.IPublishable, itemUrl As System.Uri)' does not have a signature compatible with delegate 'Delegate Sub WaitCallback(state As Object)'.
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?
View 3 RepliesIm trying to make a basic client and listener chat room but im having problems with method invoker.
Error: Error1Method 'Private Sub CliInfo(CliMess As String)' does not have a signature compatible with delegate 'Delegate Sub MethodInvoker()'
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text[code]......
I am trying to write a VB.NET alternative to a C# anonymous function.I wish to call Threading.SynchronizationContext.Current.Send which expects a delegate of type Threading.SendOrPostCallback to be passed to it. The background is here, but because I wish to both pass in a string to MessageBox.Show and also capture the DialogResult I need to define another delegate within. I am struggling with the VB.NET syntax, both from the traditional delegate style, and lambda functions.My go at the traditional syntax is below, but I have gut feeling it should be much simpler than this:
Private Sub CollectMesssageBoxResultFromUserAsDelegate(ByVal messageToShow As String, ByRef wasCanceled As Boolean)
wasCanceled = False
[code].....
When is it run? Does it run for each object to which I apply it, or just once? Can it do anything, or its actions are restricted?
View 3 RepliesA project has a class (CAR Class) with the folloiwng details and the CARADMIN cLASS with the following details: The cardAdmin class has 2 constructor Public Sub New() & Public Sub New(ByVal rec As Rectangle) I dont know how to code the latter constructor..
cAR Class has this method/properties:
Public Class Car
Inherits HVSprite
Private fSoundFile As String
[code].....
I have a defualt constructor that has a object of a Database connectivity class:[code]therefore in theory i have to call the defualt constructor of Question from overloaded constructor. [code]How every in realitty is it possible to call the defautl constructor from the overloaded constructor
View 5 RepliesDo not know how to convert Me.Base to C#Imports Microsoft.VisualBasic
Public Class ReplaceHTML
Inherits System.IO.Stream
Private Base As System.IO.Stream
[code].....
how to pass value other than from constructor.
In my code....value are usually passed through constructor.
ie everything is declared in constructor and while executing, these values are defined in new object.
I am rewriting a C# class in VB. The C# class has a static constructor in it:
static Class()
{
...
}
which, through the Call Stack, I found is called by the main window's InitializeComponent() method.Everywhere I've looked has told me that the corresponding static constructor in VB is just:Shared Sub New()
but this method is never invoked. Am I creating my VB static constructor right? Is it likely something else that has nothing to do with my static constructor?
While reading this threadthe code sample there came from MSDN, which was used to demonstrate the use of a delegate. I know what Delegates are. And I know how they work when it comes to things like crossing threads and even sinks.... how ever, the sample given is a simplecontrived example, and I'm wondering why/when would I use a delegate in this situation? Or in any situation? In my head, it seems it would have been easier/cleaner to call the function pointed to directly...
View 18 RepliesA former co-worker wrote a class in C# that was to be used as an attribute. I'm having to convert his work to VB (yeah, it's pointless and way more work than necessary, but management thinks it's a good idea). Unfortunately I'm having an issue because one of the main properties of the class is an ArrayList which is set with a ParamArray in the constructor. The class looks like this (note I've tried to abstract out the problem, so this is a simplified version of the class only)
<AttributeUsage(AttributeTargets.Field Or AttributeTargets.[Property])> _
Public Class LabelAttribute
Inherits Attribute
Public Sub New()
[CODE]........
In the first one, there is just the single label being added. No Problem. In the second one there are 2 different labels being added. Again, no problem. The third one, however, is a problem because it's assuming that the "Description", which is supposed to be the description is instead being interpreted as another label.
This wasn't a problem in C# because it could be set as follows:
[CODE].................
Of course this doesn't work in VB because optional parameters aren't referred to by name as they are in C#. As a stopgap I've changed the constructor to allow only a single label like this
CODE]..................
But that undermines some of what this class is meant to do. Does anyone know how, or even if I can get the same basic functionality to work in VB?
Think there must be a way to make the existing design work. Imagine this class was built into a library that I could not modify. Would I be forever forbidden from setting Description because of this design?
Why VB.NET (vs C#) does not "inherit" the constructors with parameters?
Public Class StopLine
Inherits Microsoft.VisualBasic.PowerPacks.LineShape
Public Sub New()
MyBase.New()
[code]....
I've been searching for a while now and can't find a way to add a constructor to the Rectangle Structure.
Is this even possible without inheriting from the class?
Im having trouble with assigning a Rectangle to this constructor
fTrainAdmin = New TrainAdmin( )
I need to put following code in the Constructor of my startup form.
JohnKenedy.BusinessSQLEXPRInstaller _ins = new JohnKenedy.BusinessSQLEXPRInstaller(" _ <Installation Display Name>", "localhost", "<New database instance name>", _
"<new database name>", "<database password>", "<database backup filename>");
if (_ins.IsDone == false) _ins.ShowDialog();
if (_ins.IsRestart == true) {
Application.Exit();
this.Close();
return;
}
But I really do not know what the Constructor is and how to access it?
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.
I'm not sure if this is possible or not. I have a number of different classes that implement interface IBar, and have constructors that take a couple of values. Rather than create a bunch of almost identical method, is it possible to have a generic method that will create the appropriate constructor?
private function GetFoo(Of T)(byval p1, byval p2) as List(Of IBar)
dim list as new List(Of IBar)
dim foo as T
' a loop here for different values of x
foo = new T(x,p1)
list.Add(foo)
' end of loop
return list
end function
I get:
'New' cannot be used on a type parameter that does not have a 'New' constraint.
I searched quickly and not find an explicit answer at the following question:Why VB.NET (vs C#) does not "inherit" the constructors with parameters?
Public Class StopLine
Inherits Microsoft.VisualBasic.PowerPacks.LineShape
Public Sub New()
[code].....
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?
How come when I create new instances of a form and show it, it loads the form twice?[code]...
View 2 Replies