Can a method be declared inside another method? 
Author Message
 Can a method be declared inside another method?

When a method is only used by one other method it would be nice to be able to
declare it within the scope of that method.  Is this allowed?

#region MyMethod()
protected void MyMethod()
{
        // whatever code

        protected void DoSomethingForMyMethod()
        {
                // whatever code
        }

Quote:
}

#endregion

Thanks for your help.



Mon, 09 May 2005 00:41:22 GMT  
 Can a method be declared inside another method?


Quote:
> When a method is only used by one other method it would be nice to be able
to
> declare it within the scope of that method.  Is this allowed?

> #region MyMethod()
> protected void MyMethod()
> {
> // whatever code

> protected void DoSomethingForMyMethod()
> {
> // whatever code
> }
> }
> #endregion

> Thanks for your help.

No.

Tom Shelton



Mon, 09 May 2005 00:50:45 GMT  
 Can a method be declared inside another method?
Nope.


Quote:
> When a method is only used by one other method it would be nice to be able
to
> declare it within the scope of that method.  Is this allowed?

> #region MyMethod()
> protected void MyMethod()
> {
> // whatever code

> protected void DoSomethingForMyMethod()
> {
> // whatever code
> }
> }
> #endregion

> Thanks for your help.



Mon, 09 May 2005 00:51:52 GMT  
 Can a method be declared inside another method?
As others have said, no this can't be done.

But, I challenge you for a second.  Why would you want to do this anyway?

If I understand your premise, the inner method is used only by its outter
shell method, and nowhere else.  If that is the case, then you have a piece
of code that by your own admission is not used elsewhere.  Methods can be
viewed as little, reusable code nuggets, veneers if you will to reusable
functions, exposed to the world for use.  If you have some code that is only
used within a method, then inline that code within the method; and if the
same code has to be used multiple times in that method (but again only in
that method) then wrap a loop construct around it.  Of course, you already
thought of these I am sure.

OO changes it all, my friend.  Would be nice to have the equiv of a helper
function that can be encapsulated within and only available within the scope
of a method (or property).  However, this isn't possible in any OO language
I know of.

Regards,
Tom Rogers


Quote:
> When a method is only used by one other method it would be nice to be able
to
> declare it within the scope of that method.  Is this allowed?

> #region MyMethod()
> protected void MyMethod()
> {
> // whatever code

> protected void DoSomethingForMyMethod()
> {
> // whatever code
> }
> }
> #endregion

> Thanks for your help.



Mon, 09 May 2005 02:04:21 GMT  
 Can a method be declared inside another method?


Quote:
> As others have said, no this can't be done.

> But, I challenge you for a second.  Why would you want to do this anyway?

> If I understand your premise, the inner method is used only by its outter
> shell method, and nowhere else.  If that is the case, then you have a
piece
> of code that by your own admission is not used elsewhere.  Methods can be
> viewed as little, reusable code nuggets, veneers if you will to reusable
> functions, exposed to the world for use.  If you have some code that is
only
> used within a method, then inline that code within the method; and if the
> same code has to be used multiple times in that method (but again only in
> that method) then wrap a loop construct around it.  Of course, you already
> thought of these I am sure.

> OO changes it all, my friend.  Would be nice to have the equiv of a helper
> function that can be encapsulated within and only available within the
scope
> of a method (or property).  However, this isn't possible in any OO
language
> I know of.

Object Pascal - think Delphi.  You can actually get away with this in C++ by
creating a class inline to the method.  It would look something like:

#include <iostream>
using namespace std;

void method();

int main()
{
    method();

Quote:
}

void method()
{
    class t
    {
        public:
            static void print()
            {
                cout << "Hello, World!" << endl;
            }
    };

    t::print();

Quote:
}

Maybe this would be a good thing to add to C# - inline types?

Tom Shelton



Mon, 09 May 2005 02:29:33 GMT  
 Can a method be declared inside another method?
Not familiar with Object Pascal, just regular old Pascal.  As for C++, it's
probably more of an idiosyncrasy of the precompiled, bolt on nature of that
implementation and its reliance on C that makes this possible and not
something that is intentional or even desired by Stroustrup when he
envisioned the language.

Still, you are right, an inline function that is scoped within its bracketed
curly braces would be nice...though I doubt I would ever use it.

Later,
Tom Rogers


Quote:



> > As others have said, no this can't be done.

> > But, I challenge you for a second.  Why would you want to do this
anyway?

> > If I understand your premise, the inner method is used only by its
outter
> > shell method, and nowhere else.  If that is the case, then you have a
> piece
> > of code that by your own admission is not used elsewhere.  Methods can
be
> > viewed as little, reusable code nuggets, veneers if you will to reusable
> > functions, exposed to the world for use.  If you have some code that is
> only
> > used within a method, then inline that code within the method; and if
the
> > same code has to be used multiple times in that method (but again only
in
> > that method) then wrap a loop construct around it.  Of course, you
already
> > thought of these I am sure.

> > OO changes it all, my friend.  Would be nice to have the equiv of a
helper
> > function that can be encapsulated within and only available within the
> scope
> > of a method (or property).  However, this isn't possible in any OO
> language
> > I know of.

> Object Pascal - think Delphi.  You can actually get away with this in C++
by
> creating a class inline to the method.  It would look something like:

> #include <iostream>
> using namespace std;

> void method();

> int main()
> {
>     method();
> }

> void method()
> {
>     class t
>     {
>         public:
>             static void print()
>             {
>                 cout << "Hello, World!" << endl;
>             }
>     };

>     t::print();
> }

> Maybe this would be a good thing to add to C# - inline types?

> Tom Shelton



Mon, 09 May 2005 03:20:49 GMT  
 Can a method be declared inside another method?

Quote:

> OO changes it all, my friend.  Would be nice to have the equiv of a
> helper function that can be encapsulated within and only available
> within the scope of a method (or property).  However, this isn't
> possible in any OO language I know of.

Python allows local methods(in addition to lambda functions). I have a
strong suspicion that Smalltalk does too.

And let's not forget that the next version of C# will contain anonymous
local methods.

--
Arild Fines



Mon, 09 May 2005 06:24:15 GMT  
 Can a method be declared inside another method?


Quote:

> > OO changes it all, my friend.  Would be nice to have the equiv of a
> > helper function that can be encapsulated within and only available
> > within the scope of a method (or property).  However, this isn't
> > possible in any OO language I know of.

> python allows local methods(in addition to lambda functions). I have a
> strong suspicion that Smalltalk does too.

> And let's not forget that the next version of C# will contain anonymous
> local methods.

That's true....  I forgot about that.  That would probably do exactly what
is desired here - to bad you have to wait a year or two...

Tom Shelton



Mon, 09 May 2005 15:10:45 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. How do you declare optiona method parameters in C#

2. Declaring statics variable in methods

3. Correct method of declaring an array argument

4. how to declare a method like this.

5. How declare these methods in vc++

6. Enable COM clients call methods inside COM events

7. Opening a modal dialog box twice inside a method

8. Tunnelling service methods into a single method

9. Why static method cannot be abstract method?

10. Dynamic Method call ( Invoke method ) failing in service when compiled as ReleaseMinDependancy

11. calling an interface method from another method in the same class

12. Calling a method from within a method, legal?

 

 
Powered by phpBB® Forum Software