473,806 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function within method (Simple question?)

Coming from Delphi, I've tried to declare a function within a method, but
can't get the compiler to "swollow it". Is it at all possible?

That is:

SomeMethod()
{

LocalFunction()
{
//Statements;
}

LocalFunction() ;
//Statements;
LocalFunction() ;
}

Thank you!

Regards Håkan Johansson
Jan 10 '07 #1
13 1576
Håkan,

Are you using C# 2.0? If so then the following would compile.

void SomeMethod()
{
ThreadStart method = delegate()
{
// Statements
}

method.DynamicI nvoke(null);
// Statements
method.DynamicI nvoke(null);
}

Brian

Håkan Johansson wrote:
Coming from Delphi, I've tried to declare a function within a method, but
can't get the compiler to "swollow it". Is it at all possible?

That is:

SomeMethod()
{

LocalFunction()
{
//Statements;
}

LocalFunction() ;
//Statements;
LocalFunction() ;
}

Thank you!

Regards Håkan Johansson
Jan 10 '07 #2
Mark R. Dawson wrote:
Hi Håkan,
in C# you cannot declare a method inside another method.
Mark,

You can in 2.0. You just can't name them. That's why they're called
anonymous methods :)

Brian

Jan 10 '07 #3
Are you using C# 2.0? If so then the following would compile.
>
void SomeMethod()
{
ThreadStart method = delegate()
{
// Statements
}
method.DynamicI nvoke(null);
// Statements
method.DynamicI nvoke(null);
}
Why use DynamicInvoke? This is fine:

void SomeMethod()
{
String.Threadin g.ThreadStart localFunction = delegate
{
// Statements
}

localFunction() ;
localFunction() ;
}

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 10 '07 #4
Håkan Johansson <ha*********@no spamhallde.sewr ote:
Coming from Delphi, I've tried to declare a function within a method, but
can't get the compiler to "swollow it". Is it at all possible?
As others have said, the only similar thing is anonymous methods. See
http://www.pobox.com/~skeet/csharp/c...delegates.html

However, unless I needed delegate behaviour, I'd normally just create
another private method... any reason you want to create it within the
method?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 10 '07 #5
Dustin Campbell wrote:
Why use DynamicInvoke? This is fine:

void SomeMethod()
{
String.Threadin g.ThreadStart localFunction = delegate
{
// Statements
}

localFunction() ;
localFunction() ;
}
Because it is was an order of magnitude more complicated than your
suggestion. Clearly, any self respecting programmer would use my
suggestion, if for nothing else, because it portrays a sense of
superiority and a command of the .NET Framework. :)

Jan 10 '07 #6
Jon Skeet wrote:
Håkan Johansson <ha*********@no spamhallde.sewr ote:
Coming from Delphi, I've tried to declare a function within a method, but
can't get the compiler to "swollow it". Is it at all possible?

As others have said, the only similar thing is anonymous methods. See
http://www.pobox.com/~skeet/csharp/c...delegates.html

However, unless I needed delegate behaviour, I'd normally just create
another private method... any reason you want to create it within the
method?
One reason can be so it can access locals declared in the scope of the
enclosing method. Anonymous delegates have this behaviour too - via
closure / capturing.

-- Barry

--
http://barrkel.blogspot.com/
Jan 10 '07 #7
>Why use DynamicInvoke? This is fine:
>>
void SomeMethod()
{
String.Threadi ng.ThreadStart localFunction = delegate
{
// Statements
}
localFunction( );
localFunction( );
}
Because it is was an order of magnitude more complicated than your
suggestion. Clearly, any self respecting programmer would use my
suggestion, if for nothing else, because it portrays a sense of
superiority and a command of the .NET Framework. :)
ROFL

Best Regards,
Dustin Campbell
Developer Express Inc.
Jan 10 '07 #8
Barry Kelly <ba***********@ gmail.comwrote:
However, unless I needed delegate behaviour, I'd normally just create
another private method... any reason you want to create it within the
method?

One reason can be so it can access locals declared in the scope of the
enclosing method. Anonymous delegates have this behaviour too - via
closure / capturing.
True, true. Unless I actually needed to pass a delegate somewhere (or
call BeginInvoke or something) I would usually make that state explicit
somewhere.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 10 '07 #9
IIRC, it is another method is *another class with an unknown name. Not that
it matters, but it is a bit more then sugar imo.

--
William Stacey [C# MVP]

"Mark R. Dawson" <Ma*********@di scussions.micro soft.comwrote in message
news:80******** *************** ***********@mic rosoft.com...
| :-) I guess you can use anonymous methods, good call, but it is syntactic
| sugar for really creating another method in the class.
|
| Mark.
| --
| http://www.markdawson.org
| http://themightycoder.spaces.live.com
|
|
| "Brian Gideon" wrote:
|
| Mark R. Dawson wrote:
| Hi Håkan,
| in C# you cannot declare a method inside another method.
|
| >
| Mark,
| >
| You can in 2.0. You just can't name them. That's why they're called
| anonymous methods :)
| >
| Brian
| >
| >
Jan 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14960
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
1778
by: J. Hall | last post by:
Hi dudes, Got a simple webpage, with three numeric text input boxes, the idea being that the user is asked to insert percentages of their business around the world... UK, Europe, Other Obviously this mustn't exceed 100% and so I OnChange I simply want to check that all three boxes have a value, and if so sum them up and alert the user
4
3636
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's useful to help me to solve some basic problem which I may not perceive before. I appreciate your help, sincerely.
28
4345
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
6
2055
by: SanPy | last post by:
The subject of this message might be a little cryptic, so here's an example of what I mean: def foo(): """doc string of foo""" print foo.__doc__ doc string of foo What I want to know is whether it is possible to call __doc__ against
25
595
by: hifrnds007 | last post by:
how the function poiners avoids the usage of switch cases?
10
1897
by: colin | last post by:
Hi, I profile my code and find its spending a lot of time doing implicit conversions from similar structures. the conversions are mainly things like this class Point { implicit conversion to vector3; //this conversion just returns positon Vector3 position;
4
2499
by: =?Utf-8?B?UmljaEI=?= | last post by:
I am trying to create a project using the ASP.NET AJAX accordion control. I would like to dynamically add panes to the control with a form template added when the pane is added. I have tried unsuccessfully in creating the whole pane as a user control and have succeeded in adding the pane and then dynamically adding the content which is a user control to the pane, dynamically within the page. However I would like to have a single pane...
53
8430
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script language="javascript" type="text/javascript">
0
9599
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7650
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.