.net - Create And Pass Custom Function As A Delegate?

Aug 5, 2009

I have a function Process(data, f) where data is a normal parameter and f is a function needed to process the data (passed as a delegate to Process)

The signature of f is int f(a,b,c), with a, b, c supplied by Process when it calls the delegate.

Up until here, standard delegate usage.

Now, I have a special but common case where I would like to call Process with a constant f function. So I would like to write SimpleProcess(data, k) so that the caller does not need to create a delegate in this case, just pass the constant k (the only information needed).

So I need to dynamically create a constant function, g(a,b,c) which takes 3 parameters (which are ignored) and always returns k. This has to be done within the SimpleProcess function, so that I can then simply call Process(data, g) from within that function, without the need to rewrite the whole Process function for this special case.

View 3 Replies


ADVERTISEMENT

Create A Delegate And Use The Function In Program Called DoStuff()?

Jan 4, 2012

For a class, I have to create a delegate and use the function in my program called DoStuff(). There is an error tht says "Method 'Public Sub DoStuff(stringVal As String)' does not have a signature compatible with delegate 'Delegate Sub MyDel()'.

[Code]...

View 2 Replies

Pass Generic List To A Function That Has A Custom Defined List

Apr 7, 2011

it seems always at this time I cant figure out how to do something maybe it has something to do with lunchtime.

Im having trouble using a method that is defined by an outside company

This is a link to their integration guide:

[URL]

This is their documentation for the class Im trying to use

OrderQueryWebService Class

This class is for SID in particular and is made for use with the notification methods. This is for the Order Query Web Service specifically, allowing clients to return the status of transactions for their merchant, whenever they want to.

Methods
/ <summary>
/ Pass through the variables required and receive a list of Transaction
objects showing transaction status

[Code].....

The MerchantCode, the MerchantUsername, the MerchantPassword are all good the List is a custom type called SetComHash.Transaction

Everytime I try adding a string to the list such as the transactionNumber and some other values it gives me a type error:

"Value of type string cannot be converted to 'SetcomHash.Transaction'

View 13 Replies

C# - Pass A Property As A Delegate?

Jul 30, 2010

This is a theoretical question, I've already got a solution to my problem that took me down a different path, but I think the question is still potentially interesting.Can I pass object properties as delegates in the same way I can with methods? For instance:

Let's say I've got a data reader loaded up with data, and each field's value needs to be passed into properties of differing types having been checked for DBNull. If attempting to get a single field, I might write something like:

if(!rdr["field1"].Equals(DBNull.Value)) myClass.Property1 = rdr["field1"];

But if I've got say 100 fields, that becomes unwieldy very quickly. There's a couple of ways that a call to do this might look nice:

myClass.Property = GetDefaultOrValue<string>(rdr["field1"]); //Which incidentally is the route I took

Which might also look nice as an extension method:

myClass.Property = rdr["field1"].GetDefaultOrValue<string>();

Or:

SetPropertyFromDbValue<string>(myClass.Property1, rdr["field1"]); //Which is the one that I'm interested in on this theoretical level

In the second instance, the property would need to be passed as a delegate in order to set it.

View 7 Replies

Pass Multiple Arguments To A Delegate Method From A List(Of T).ForEach Call?

Sep 6, 2011

Is it possible to pass multiple arguments to a delegate method from a list(Of T).ForEach call?So, I'd like to pass a listbox object to the delegate routine below:

Sub test()
Dim names As New List(Of String)
names.Add("Bruce")
names.Add("Alfred")

[code]....

View 4 Replies

How To Pass A Function To A Function Is Functors/function Objects Avaiable In VB2010

Oct 12, 2011

I want to make an numerical integration method with takes in an analytic function and integrate it over a specific interval. For the numerical integration procedure I want to use some procedures in nr.com. The problem is that these are programmed in C++ and they uses functors to pass a function to the integration method. How can I do this in VB 2010?

I want to initialize the function (i.e. set a=1,b=0 for function y(x)=a*x+b) and then pass the function to the integration method. Then when the integration method call the function it only calls the function with one parameter (i.e. x since a,b is already set)

What is the best way to do this in VB2010?I want to make a general integration method where I can pass any single valued function and integration limits.

I have just started using VB, and from what I have found so far it seems like the tools you have is

- to us a delegate for the function
- to use a lambda expression for the function
- send a pointer/adressOf
- to create a function class/structure and submit this to the function

As for now I am most inclined to create a function-class. But I am not really sure how.F.ex. I make different classes for each "uniqe function" I want to integrate, but how can I pass them to the integration function when I need to specify the argument type in the integration-function-call?This seems like a basic problem which applies to many Math operations, so I think it would be very useful to clarify this.

View 2 Replies

.net - Using Action(Of T) Over A Custom Delegate Syntax For Readability?

Jan 7, 2011

Since .net 4.0, is the new ettiquette to use syntax such as:

Private Sub Main()
MyMethod(AddressOf AnAction)
End Sub
Private Sub MyMethod(ByVal toDo As Action(Of String, Integer, Boolean))
toDo.Invoke("Tom", 1, True)
End Sub
Private Sub AnAction(ByVal p1 As String, ByVal p2 As Integer, ByVal p3 As Boolean)
End Sub

[Code]...

This goes the same with Tuples/Func too. It is nice to write but feels a bit sloppy in places. Why would anyone want Tuple(Of T, T2, T3, T4) over a nice class type? My actual piece of code has these nested, so it passes the Action along in a chain of methods.

Sorry for the VB example, AddressOf is just VBs way of saying new Action(AnAction). Also I just wrote this into the browser so it might not compile either.How to people feel about reading and using these compiler generic actions as opposed to proper special entities crafted for their exact requirements in the solution?

View 1 Replies

Convert C# Delegate Function?

Oct 2, 2011

Here there's an old question about this code.

xmpp.OnLogin += delegate(object o)
{
xmpp.Send(

[code]....

View 2 Replies

Function Calling Using Delegate?

Feb 2, 2011

I have one master page and then content page on the master page and also have one UC on the master page, now i want to filter data from usercontrol on and populate on content page using delegate.

I have achieved this in C# but i am not able to do this in VB.NET

Because in VB.NET i am not able to overload event of the uc.

View 3 Replies

Dynamically Change The Function Of A Delegate In .NET?

Jun 18, 2009

Suppose I have a class that represents a product to be priced using one of a number of different pricing strategies. This pricing occurs hundreds of times per second, so to eliminate repetitive if/else statements I am instead using a delegate to launch the appropriate strategy, like so:

Private Delegate Sub PricingModel(ByVal params As PricingParameters)
Private myPricingModel As PricingModel
Private myPricingParameters As PricingParameters[code].....

But this doesn't look right to me (though it seems to work). Is there an accepted/better way of doing this? Or is this the standard way? Or is this just simply a doomed approach from the start?

View 2 Replies

Function Have A Non Compatiable Signature With This Delegate?

Oct 26, 2011

The compiler is telling me that these signatures are incompatiable, but they seem fine to me.[code].....

View 2 Replies

Passing Parameters To Delegate Function?

Nov 21, 2010

I need to convert the following C# code to VB.NET:

public bool TryGetRewriteMatch(UrlContext url, out RedirectMatch match)
{
// NOTE: Normally this would use regular expression matches, cached database

[Code].....

However, I need to reference Url and ProductID in the function and I don't know how to pass them as parameters or get their values from the original function.

View 4 Replies

C# - Registering A Delegate Function With An ISO C++ Callback (on Mono)

Apr 8, 2010

I am exposing some ISO C++ data types to .Net (mono on Linux to be precise).For the purpose of brevity, I shall refer to C# in my question, although my question relates to all of the .Net languages (with C# and VB.Net being my target languages). So far, I have worked out how to expose the ISO C++ data types in C# class(es) for use in mono -The only problem so far is how to deal with the C++ callbacks. In .Net languages (C# and Vb.Net), I believe 'delegates' are the callback equivalent.

Sticking with C# for now, can anyone recommend a way that I can register the C# delegate functions with my ISO C++ code.The ISO C++ code is a notification library, and I want to be able to "push" the notifications to the mono framework (i.e. C# delegates in this case).My underlying assumption is that the mechanism/steps to implement this would be the same for the .Net languages - I'll just have to code the actual delegates in the .Net language of choice - is that assumption correct?Last but not the least, is the question of thread saftey. The underlying ISO C++ code that I am exposing to .Net (mono to be more specific), is both re-ntrant and thread safe - do I have to do anything "extra" to call .Net delegate from my ISO C++ code?

View 1 Replies

Delegate Function .net : Error Says Expression Expected

Aug 13, 2010

Having problem understanding Delegate error. Error says expression expected, How do I fix?Here is the offending line (#259)....

mylist.ForEach(Delegate Function(P As linkItem) As System.Char[] )

Here is the entire code.

<script language="VB" runat="server">
Function sectionTitle(ByRef f As String)
'Open a file for reading

[code]....

View 3 Replies

Multicast Delegate: Represents A Multicast Delegate; That Is, A Delegate That Can Have More Than One Element In Its Invocation List?

Jan 30, 2010

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?

View 5 Replies

Invoke Parameterized Function Delegate From Background To UI Thread?

Oct 4, 2010

What is the Invoke syntax to marshall data from a background thread receive callback method to a function on the UI Thread?(Form1).

Public Class Form1
MyDelegate = New DisplayData(AddressOf DisplayData)
Private Sub Form1_Load()
MyDelegate = New DisplayData(AddressOf DisplayData)

[code]....

View 12 Replies

.net - Performance Implications Of Implementing A Generic Type And A Delegate In A Function?

Aug 4, 2010

I wrote a serializer (to Byte Array) for dictionaries that have a string key, but an object of some sort as its value.I've never implemented a generic type in a function or used a delegate before, so I'm a bit concerned about this being significantly slower than writing a serialization function for a specific type of Dictionary (Dictionary(Of String, MyClass) for example)Should this code be significantly slower due to the use of the generic type or the delegate?

[code]...

It works, and I could loop it and compare it to a more static Dictionary serializer, but I'm more concerned about when I start using this for a lot of different String/Object dictionary combinations, and it'll take me a long time to write a bunch of static dictionary serializers (that's what I'm hoping to avoid in the first place)edit: simplified intro text

View 1 Replies

Nested Function Does Not Have The Same Signature As Delegate 'System.Func(Of Object)'

Apr 5, 2012

I have a section of code that deletes a lot of columns, one at at time, based on conditions that arise. Currently it does this:

objrpt.objXLSheet.Columns(col) _
.EntireColumn.Delete(Excel.XlDeleteShiftDirection.xlShiftToLeft)

There are so many of these, that I'm getting tired of looking at these lines so I want to curry the references to the Excel stuff.I tried:

Dim DelCol As Func(Of Integer) = Function(col As Integer) _
objrpt.objXLSheet.Columns(col) _
.EntireColumn.Delete(Excel.XlDeleteShiftDirection.xlShiftToLeft)

but that won't compile, giving me the error: "Nested function does not have the same signature as delegate 'System.Func(Of Integer)'. Ah forget it. I see that I'm trying to call a method from a function. I guess that won't work.Wait, I'm now trying it another way. I first defined a function:

Private Function DeleteColumn(ByRef objrpt As ExcelReport, ByVal col As Integer) As Object
DeleteColumn = Nothing
Dim objrng As Excel.Range

[code]....

Now the error message is a little different:

"Nested function does not have the same signature as delegate 'System.Func(Of Object)'.

View 14 Replies

Pass Function (Of TElement / TKey) As A Function Parameter

May 28, 2012

I want to make a GetAllContacts method which takes a sort parameter of type Func(Of Contact, TKey) which is the same type that the OrderBy method for an IEnumerable(Of Contact) takes.[code]"Too many arguments to extension method 'Public Function ElementAtOrDefault(index As Integer) As Contact' defined in 'System.Linq.Enumerable'." on the second parameter.

View 2 Replies

Pass A Delegate Into A Constructor Of An Abstract Class From A Descendant's Constructor?

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

C# - Create A Delegate For A .NET Property?

Apr 7, 2009

I am trying to create a delegate(as a test) for: Public Overridable ReadOnly Property PropertyName()As String My intuitive attempt was declaring the delegate like this:

[code]...

So the question is, how do I make a delegate for a property?

View 6 Replies

Pass A Value From A Sub Procedure To A Function Procedure .... Pass The Whole Subprocedure To The Function Procedure Argument?

Mar 30, 2012

Im a student doing an assignment, how do i pass the value from a sub procedure to a function procedure....i want to pass the value from decSubtotal to a function procedure named CalculateDiscount; check out my code--

[Code]...

View 1 Replies

Delegate - BeginInvoke - EndInvoke - Clean Up Multiple Async Threat Calls To The Same Delegate?

Feb 9, 2010

I've created a Delegate that I intend to call Async.

[Code]...

View 2 Replies

Get A Error" Method 'Private Shared Sub Ping Does Not Have A Signature Compatible With Delegate 'Delegate Sub ?

Jun 1, 2010

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)'.

View 4 Replies

Create Property Setter Delegate Via DynamicMethod?

May 28, 2010

I'm building an abstract class that handles some common operations for all of my data access objects. The class will utilize generics, so I can't make direct calls to constructors, business object properties, etc. Reflection is way too slow for what I want to accomplish, so I am using a DynamicMethod, ILGenerator, and Delegate caching instead (sample will follow).

The method I'm currently working on is a Private Shared method that populates a list of business objects using records from a data table. I'm having a problem building the property setter via IL. I'm getting an object reference error, but I'm not sure how to fix it. I believe the error comes from the "target" being Nothing, but the parameter I'm passing as the target is not Nothing at the time of invocation. Here are some snippets of what I've got so far:

'class declaration -- TList is strongly-typed list of T
Public MustInherit Class DataAccessBase(Of T, TList)
...
'delegate declaration

[code]...

I'm assuming the obj is not being passed correctly as the target, thus causing the target to be Nothing.

View 4 Replies

.Net CodeDom - Create Delegate With AddressOf Operator Equivalent?

Apr 25, 2011

Is there a way to create a delegate instance in .Net using CodeDom? I want to generate something which looks like the following:Dim myDelegate As someDelegateType = New someDelegateType(AddressOf implementingMethod)Below is more info on the context...Original Question:I am using CodeDom from the .Net framework (v3.5 if it matters) to generate a class. One of the classes defines a delegate method which in VB.Net looks like:Public Delegate Function filterByIdDelegate(ByVal obj As Object, ByVal id As Integer) As BooleanI then have a method which will provide the implementation:Private Function filterById(ByVal obj As Object, ByVal id As Integer) As Boolean Return (obj.ID = id)nd FunctionHere's the problem; how do you create an instance of the delegate (using the equivalent of AddressOf for VB.Net)? I am currently doing this (<filterByIdFunctionName> is a string holding the name of the delegate function, `' is the name of the delegate field):

Dim getFunction = New System.CodeDom.CodeMemberMethod()
With getFunction
'Declare delegate instance

[code].....

View 1 Replies

Create A Custom Messagebox, With Varying Number Of Buttons With Custom .Text Descriptions?

Feb 6, 2009

This is what I would like to achieve:To create a custom messagebox, with varying number of buttons with custom .Text descriptions, and other features. I intended to have a property array that would be redim-ed and have values (.Text values) set by the calling class:

[code]...

View 7 Replies

Specify A Custom Icon For Display In The Toolbox Window When Create Own Custom Control?

Dec 29, 2011

how to specify a custom icon for display in the toolbox window when you create your own custom control? Something other than the dreaded "gearbox" icon.

View 2 Replies

Create Expression Where Delegate Type Is Unknown At Compile Time?

Sep 21, 2011

I have a code below to make collection that bind to a gridview able to sort by clicking on the column header. The problem here is "IPerson" is unknown at compile time. I want the delegate type able to decide by getting from gridview datasource.[code]....

View 1 Replies

Create A Custom File Association ( A Custom Extension ) In VB?

Dec 19, 2010

know how to create a custom file association ( a custom extension ) in VB .Net and how to open it on the application

View 1 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved