473,322 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

How do you create a method within a method?

Ok, so I am working in C# on a windows forms application

I have a class called Target and I create a method and want another method to be created inside like this:

Expand|Select|Wrap|Line Numbers
  1. public void move()
  2. {
  3.      public void right()
  4.      {
  5.           location.offset(5, 0);
  6.      }
  7. }
  8.  
so when I call it in the Form1 KeyDown event handler, it should look like this:

Expand|Select|Wrap|Line Numbers
  1. Target myTarget = new Target();
  2.  
  3. if (e.KeyData == Keys.Right)
  4. {
  5.      myTarget.move.right();
  6. }
  7.  
for some reason, it won't let me simply write it like I want to... is there some special way to nest methods?


PS- if you need more information, please say so
Aug 12 '10 #1

✓ answered by Joseph Martell

Properties in C# can return objects and they do not require the use of (). For example:

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2. {
  3.     private string _name;
  4.     public string Name
  5.     {
  6.         get
  7.         {
  8.             return _name;
  9.         }
  10.         set
  11.         {
  12.             _name = value;
  13.         }
  14.     }
  15. }
This would create a Name property for a class. It would return a string object when you were using it in code:

Expand|Select|Wrap|Line Numbers
  1. MyClass example = new MyClass();
  2.  
  3. example.Name = "Joe Smith";
  4. string part = example.Name.Substring(5);
Notice that I can then perform methods on the returned object, but I am using a method of an object, not a method defined within another method. As far as I know, that is the only way to produce your precise syntax in your OP.

Hope that helps.

6 2368
Joseph Martell
198 Expert 128KB
You can't nest methods. The "." operator is used to access members (methods, properties, etc.) of classes, structs, and interfaces. Methods cannot have sub-methods in the way you are talking about.

There are a lot of different options available to you though. If you look at your syntax:

Expand|Select|Wrap|Line Numbers
  1. myTarget.move.right();
it suggests a member object that handles moving your target object in various ways. In this case, to the right. You could pursue that route to maintain your desired syntax.
Aug 13 '10 #2
weaknessforcats
9,208 Expert Mod 8TB
This code:

Expand|Select|Wrap|Line Numbers
  1. myTarget.move().right(); 
will work provided the move() method returns an object by returning *this.

You can nest methods in C++:

Expand|Select|Wrap|Line Numbers
  1. cout << "Hello" << endl;
but the methods need to return a value of the correct type.
Aug 13 '10 #3
Joseph Martell
198 Expert 128KB
@weaknessforcats

You can nest methods in C++:
You cannot nest method definitions, right? If I'm wrong on this, then you are blowing my mind.

This code from the OP would be considered incorrect:
Expand|Select|Wrap|Line Numbers
  1. public void move()
  2. {
  3.      public void right()
  4.      {
  5.           location.offset(5, 0);
  6.      }
  7. }
because a function cannot be declared inside of another function (without getting into lambda functions), yes?
Aug 14 '10 #4
@Joseph: Ok, thank you for your help but I am a novice with coding and need further information.

Expand|Select|Wrap|Line Numbers
  1. myTarget.move.right();
  2.  
if the "move" part of the above code is not a method, then what is it called and how is it structured?

My CS teacher has emailed me back now and has confirmed that I need to use a parameter to set the desired direction, but I figure that this information will be useful anyway in future projects.
Aug 15 '10 #5
Joseph Martell
198 Expert 128KB
Properties in C# can return objects and they do not require the use of (). For example:

Expand|Select|Wrap|Line Numbers
  1. public class MyClass
  2. {
  3.     private string _name;
  4.     public string Name
  5.     {
  6.         get
  7.         {
  8.             return _name;
  9.         }
  10.         set
  11.         {
  12.             _name = value;
  13.         }
  14.     }
  15. }
This would create a Name property for a class. It would return a string object when you were using it in code:

Expand|Select|Wrap|Line Numbers
  1. MyClass example = new MyClass();
  2.  
  3. example.Name = "Joe Smith";
  4. string part = example.Name.Substring(5);
Notice that I can then perform methods on the returned object, but I am using a method of an object, not a method defined within another method. As far as I know, that is the only way to produce your precise syntax in your OP.

Hope that helps.
Aug 15 '10 #6
weaknessforcats
9,208 Expert Mod 8TB
@Joseph Martell
You cannot nest method definitions, right? If I'm wrong on this, then you are blowing my mind.
Everything is OK. By nesting you mean coding one method inside another. I mean't you could call functions by nesting the calls. We're both right.
Aug 15 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gautham Jayaraman | last post by:
Hi All, Please let me know, If anybody in the group knows how to create a new Custom Method Wizard for your class. In the Class View, for my class, I want to right click and add a my method...
3
by: uli | last post by:
Hi all, I've learned native C and I'm trying to write now in C++. I want to use a simple routine from the "Numerical Recipes in C" for using a Newton-Raphson Method within my c++ class. The...
5
by: Saper\(ek\) | last post by:
in java I could write a synchronized method like this public synchronized MyMethod() { blablabla } how (and if) can I do this in C#? --
7
by: Gaetan | last post by:
I would like to extend the capabilities of my application by calling a user method residing in a client provided assembly without having to recompile my application. Things would work like this:...
4
by: thdevdex | last post by:
Needless to say I'm new to .Net. I'm trying to set Session variables from within a method within a class. (Name "Session" is not declared) is the message I get while typing in the code. I...
8
by: digitalorganics | last post by:
What are the reason one would get this error: TypeError: Cannot create a consistent method resolution order (MRO) for bases object ?? I can provide the code if needed....
13
by: Håkan Johansson | last post by:
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() {
2
by: JH Trauntvein | last post by:
I recently tried an experiment to see if I could define a functor within a class method as shown here: #include <list> #include <algorithm> #include <iostream> class MyClass {
1
by: OneShed | last post by:
Hi, I am trying to solve one problem. I have one object and I create another different object from it (aggregation). How can I now call methods of first object within the second object? Here is...
7
by: =?ISO-8859-1?Q?Fernando_G=F3mez?= | last post by:
Hello all. I have this class with a virtual method and a constructor that calls this virtual method. A derived class overrides this virtual method, so I expected that when the base's constructor is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.