Return An (Anonymous Type With A Function) From A Function?

Mar 3, 2011

Just so it's known, this question is mostly academic, even though I tried to use the concept in a real-world solution. I realize the example is contrived, but I believe the concept is valid.I want to write some fluent code like this:

[code]...

I realize that I can't force an anonymous type into a specific type (like implementing an interface or some other class), and I don't want the overhead of defining a specific class just to match my desired fluent name with the actual method name. So I was able to make the code work like this:

copy(my_first_file).to.Invoke(my_second_file)So there is no IntelliSense or type awareness there, and I have to include the Invoke in order to have the method run. How can I get more type safety and exclude the Invoke method, under these constraints: Anonymous Type returned from Method No additional classes or interfaces Preferably, I do not want to pass in another parameter to the copy() method that tells what type to return, unless copy becomes a generic method (but I think that means defining another class/interface, which I don't want to do)

View 3 Replies


ADVERTISEMENT

C# - Pass An Anonymous Type As A Parameter To A Function?

Aug 1, 2011

After reading this post i realized that i cannot pass an anonymous type as a parameter to a function. So what other options do i have?

Perhaps, passing a Query.ToList as parameter would help, or am i re-inventing the wheel?

Update: I have the following query, which i would like to pass to a function:

Sub Test
Dim Query = (From c In DB Select New With { .ElementName = c.Name })
DoSomething(Query)

[Code].....

View 3 Replies

Function Without Return Type?

Dec 23, 2010

In my project (which I inherited from someone) there are a lot of functions like:

Public Function DoSomething(ByVal s As String)
' Do something to public properties
End Function

And they are called like this:

DoSomething(s)

So the return value is ignored (which is object, as I see in the docs). Is it safe to change all these functions to Subs? Could I break something which isn't so obvious?

View 2 Replies

Differentiate Return Type Of A Function?

Mar 27, 2012

I know a little about using a user specified type IE IIf(Of ReturnType)(ByVal expression as Boolean, ByVal TruePart As ReturnType, ByVal FalsePart As ReturnType) As ReturnType...Which works, however I've been trying to get this to work, but can't..

Protected Function InverseSign(Of ReturnType)(ByVal value As Double) As ReturnType
Dim result As String
Select Case Math.Sign(value)
Case -1: result = "+" & Math.Abs(value).ToString()

[code]....

Using try cast just gives me the error saying ReturnType has no class-constraints? whatever that means..But my goal here is to be able to return either a string or double.. However every method I've tried to convert my result string to the ReturnType doesn't work.I've also tried simply overloading the function, but I can't because they only differ by return type. So that didn't work either.

View 1 Replies

C# - Make A Function With Return Type Generic?

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

Change Return Type Of A Function On Error?

Jan 17, 2011

if i got a function with a try cath block inside and the function should return an integer.how would i do it that the function can return me a the error message?public function test() as Integer

[Code]...

View 5 Replies

Asp.net - Declare Return Type Of The Function When There Are Two Possible Returned Types

Jan 25, 2010

I have the following statement, I want to turn it into a Public Shared Function :

If isEmployee Then
Dim employeeInstance As New Employee
employeeInstance = GetEmployeeInstanceByUserId(userId)

[Code]....

Public Shared Function GetWorkerInstance(Byval isEmployee as Boolean) As ...(not sure what to write here)...

There two possible return Type. I'm not sure what I should declare for the function return type.

View 2 Replies

Loose Typing - Created A Function With No Return Type

Jun 4, 2009

I was playing with vb 2008 express and i created a function with no return type. Basically what it does is i specify a table name and a column name and it returns the value within but it doesn't type it until it gets to the calling variables type. So i have on function that i can use across all my tables.

Eg:
dim mydate as date = getvalue("tblSRs", "datelogged")
msgbox (mydate)
dim desc as String = getvalue("tblSRs", "description")
msgbox(desc)

Both return the correct values. were you always able to leave the typecasting till the end and whats the major downfall?

View 3 Replies

Custom Function Return Type + Input As Variable Datatype?

Dec 1, 2010

I'm wondering whether its possible to have a function's return type and input arguments as a variable.

For example this function:Private Function MyFunction(ByVal argument1 As VariableType) As VariableType

[Code]...

View 6 Replies

Reflection - Function Witch Return Type Is The Class Itself In Inherited Classes?

Dec 10, 2011

I have a parent class that is also a factory. For example:

Public Class Factory
Public Function clone() as Factory
' Some logic here

[code].....

View 2 Replies

C# - Function Or Interface Marked As Restricted Or The Function Uses An Automation Type Not Supported In Visual Basic?

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

.net - Extension Method For Anonymous Function?

Jul 13, 2011

I'm trying to create an extension method that returns an IEqualityComparer based on a lambda function. Heres the extension method:

<Extension()>
Public Function Comparer(Of T)(Func As Func(Of T, T, Boolean)) As IEqualityComparer(Of T)
Return New GenericComparer(Of T)(Func)
End Function

[Code]...

View 1 Replies

Create A New Thread Using An Anonymous Function?

Apr 16, 2012

I am trying to create a new thread using an anonymous function but I keep getting errors. Here is my code:

New Thread(Function()
// Do something here
End Function).Start

Here are the errors I get:

New - Syntax Error

End Function - 'End Function' must be preceded by a matching 'Function'.

View 3 Replies

Multithreading - .NET Anonymous Function To New Thread?

Apr 29, 2011

I have this code:

Public Sub Submit(
ByVal reasonId As Integer,
ByVal email As String,
ByVal message As String
)
'[Argument validation code here]
EmailController.sendMail(reasonId, email, message)
End Sub

I want to spin this off in a new thread so Submit() returns right after creating and start the thread.In C#, I could do something like this:new Thread(() => { EmailController.sendMail(reasonId, email, message)}).Start();

View 1 Replies

Asp.net - Why Won't The Anonymous Function Fire On Grid.prerender

Apr 23, 2010

In my gridview I have fields for inserting a new record in the footer.In my objectdatasource selecting event if no records came back I bind a single mock row to force the footer to show so they can still add records. Since the row does not contain real data I hide the row.

...
If result.ItemCount = 0 Then
result = mockRow
AddHandler mygridview.PreRender, AddressOf HideRow

[code]....

This works fine. However, I'd like to condense it like this:

...
If result.ItemCount = 0 Then
result = mockRow
AddHandler mygridview.PreRender, Function() mygridview.Rows(0).Visible = False

[code]....

This compiles fine, but the row doesn't get hidden. why my anonymous function isn't getting hit?

View 3 Replies

.net - Linq, VB - Anonymous Type Cannot Be Converted To Anonymous Type?

Jul 9, 2009

I'm a Linq noobie, maybe someone can point me in the right direction. What's wrong here? These anonymous types seem to have the same signatures.

[Code]...

View 2 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

Function Syntax - Call A Function With 2 Arguments When The Function Is Only Defined For One

Apr 1, 2010

I'm trying to undersatnd the syntax of calling a funciton and it seem confusing when I'm using a web service in ASP.net. Maybe this question should be in an ASP forum, but it is a VB question. This simple web service allows you to type in your name and it response with an alert box with you name.

My question is, How can you call a function with 2 arguments when the function is only defined for one. I understand that the second argument is actually a method that handling the respons, but how can you interchange function arguments for methods and how do you know that there are methods for

Here's my call:

<script type="text/javascript">

function HelloWorld()
{

var yourName = $get('txtYourName').value;

[CODE]...

View 7 Replies

.NET Function Return Value?

Jun 15, 2011

Possible Duplicate: VB.NET Function Return If I have a function that returns a boolean, what is the difference between:Return False and Function = False

View 2 Replies

Function Does Not Return Nothing?

Feb 12, 2010

take a look at the following Code:

Private Sub btnDebug_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDebug.Click
Dim d As Date

[Code]....

The Funtion ZZNull always returns Nothing, so IsNothing(d) in the calling method should evaluate to True.

But it does not !

When you run these lines, then you will see that IsNothing(d) evaluates to False

View 7 Replies

Get A Function's Return Value?

Jan 14, 2012

In the documentation of FileInfo.Create Method it says:

'Declaration
Public Function Create As FileStream
'Usage

[code].....

View 7 Replies

How To Use A Function's Return Value

Mar 8, 2009

So the program I am writing validates the controls on the page on button_click.

I want to write a function which checks which step the program is at (0-7) then return something depending on whether or not it validated.

My plan was to return the control which was not valid.

Private Function ValidateInputs() As Control
Select Case wizardStep
Case 0

[Code].....

1. could I return the names off all the controls which did not validate?

2. should I be returning a control type or would something like a string be better?

View 3 Replies

Return More Than One Value From A Function?

Mar 1, 2012

One over time hours and one week's pay @10 hourly pay. I wanted to display those two in two listboxes 1 and 2.

Public Class Form1
Dim overTimeHours As Double
Dim weekPay As Double

[Code].....

View 4 Replies

Return More Than One Value From The Function?

Jun 2, 2009

how return four value from a function. In general function will return single value but here i am going to return four different values. If possible please tell me how to store the function return values.

[Code]...

View 4 Replies

Return NaN From A Function?

Oct 5, 2010

I've created my own version of HexToDec() to properly handle the negative flag. I saw other versions online that used "Not(value)" to do the Pos/Neg inversion, but that does not generate the proper value... No, it's not the most elegant my any means. But I couldn't find anything online that actually worked.

Long story short, I want to return NaN in the case that the function is passed a string that is not a valid hex string...

How do I do that? Everything I've tried generates a compiler error... (assign return value to double.nan, assign return value to non-numeric, etc)

Existing function is below:

Function HexToDec(ByVal hexStr As String, Optional ByVal signed As Boolean = False) As Long
Dim lngFinal As Long

[Code].....

View 11 Replies

Return Value Of A Function?

Apr 14, 2009

I get the below warning [code]...

To solve it I put a return statement "Return 1" in the code.

View 2 Replies

.net - WCF Function Won't Return Stream?

May 11, 2011

So I have a function which returns a pdf stream. The calling procedure gets the stream and writes it to disk Normal VB code is as follows:

[Code]...

View 2 Replies

CLR - Function/Operator Without Return Value?

Sep 4, 2009

I've create a new project, and set treat all warnings as errors in the compile tab.All the other warnings seem to be treated as errors, except the one which I'm interested in "Function/Operator without return value".I have a simple function within a class, which does not show an error if I do not add a return statement to that function. I have read in many places that VB.Net returns a "hidden" default value if the returns statement is forgotten.My software went out to production where an overloaded function was missing a return statement, which wasn't highlighted due to the above.

View 20 Replies

Function Doesn't Return A Value?

Feb 18, 2012

[code]i want to change password for the users [code]

View 14 Replies

Function To Return Array?

Aug 5, 2011

Before adding rows to a table I am evaluating the SSN number. These will later be flagged for correction so I am setting a datatable column to true or false if it passed muster or not.

I wrote a function to check the number and make the determination plus pad it with zeros and hyphens where they should be. My problem is I want to return the results in an array. I am getting system.string[] as the return value.[code]...

View 14 Replies







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