I am struggling with DetermineSqMetres as what I'm really wanting to do is multiply the length and width which the user inputs in GetDimensions. I'm not quite sure how to call that result from GetDimensions.

I've also noticed, when I call DetermineSqMetres in Main, I've had to put a value in the parameters otherwise I keep getting "No overload for method 'DetermineSqMetres takes 0 arguments". So at this moment, I've put 0 and 0 in there because it only seems to name a number value. I realise this doesn't work, but I'm not quite sure how else to fix it.

Using this allows you to call the method GetDimensions(), which also calculates the area.

However, I am not sure if I would recommend this solution though, as it is recommended that each method should do one thing. Per example, suppose I want to use the data of GetDimensions for something else. Instead of actual returning the dimensions, it gives me the area instead.

Re: Calling Parts of Methods

Posted 23 August 2011 - 03:00 AM

Calling methods, determining the right signature (return type and parameters), etc. are basic concepts covered in the first couple chapters of any self-teaching book. That you are having trouble with this indicates, to me anyway, that you are trying to jump in and build a program before actually working through a book from cover to cover just doing the lessons. This is a bad way to go about learning to code.

Don't try to create a useful working program to fit a need of yours (or a for-pay contract) as your introduction to coding project. When you are learning to code you don't know enough to code a program, let alone know how to engineer the architecture of a program. It would be like saying "I don't know how to read sheet music, or play an instrument. I think I'll write a 3 act opera as my first learning experience."

We don't say this to be mean. We've seen lots of new coders take this approach and we know it doesn't work. Trying to design your own programs before you understand the basics of the code language you've chosen just leads to problems, frustrations, and 'swiss-cheese' education (lots of holes).

The problem with taking on large, complex tasks when you are new to coding is that

It will frustrate you to the point of quitting. We don't want you to quit. We want you to succeed and learning how to code and engineer software.

You don't know enough about coding to know where to start or in what direction to design your program

You risk learning via the 'Swiss cheese' method where you only learn certain bits and pieces for the one project but have huge holes in your education.

Finding answers to specific problems:
Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc. How to do a good search that will get you targeted answers.

Once you have that MSDN page that describes the function, you may not be familiar with out to actually get useful information from that page. There is a LOT of data there. So what is the important parts? Read this guide to reading the MSDN.

Don't think you are the first person to ask a question. Try a search on this site to see if someone has already brought up this problem. Also, as soon as you post your question go to the bottom of the updated page: You will see where the system has already analyzed your question and tried to find similar threads for the same question, so check those links out.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book. There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

Beginner:Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.

Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.

I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

This can't be stressed enough in today's world of cell phone messaging:Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, and so on like this guy:

dis not b d'hood dawg... You are sitting at a real keyboard with a real monitor, not a cell phone. You are not here texting your high school posse to come to your kegger after their shift at Taco Bell. You are here asking for help from senior coding professionals who graciously donate their valuable time to helping the next generation of coders with their chosen craft. Please try to show them, yourself and the industry some respect by writing at least at an eighth grade level. (IE: English not ebonics or SMS, real words, punctuation and so on). If you can't take your own problem/question seriously enough to write like an adult, then why would you expect anyone else to take it seriously?

Re: Calling Parts of Methods

Posted 23 August 2011 - 03:08 AM

Thank you for your reply. I completely understand this but I am actually working from a book. It gave an example to continue on from. I have done quite a few exercises before this one and I've been doing fine until now. I was just hoping someone could point me in the right direction..

I didn't think I was actually having trouble with return types.. I've only now come across a problem as I've just started to create methods. I had no problem doing everything in main. I'm just now trying to put it all into methods. Which from what I've read on numerous websites and books that methods are a good way to go for easy to read code.

Re: Calling Parts of Methods

Posted 23 August 2011 - 03:40 AM

Methods are a good use of programming as they reduce the amount of maintenance. Believe me, you do NOT want to correct copy paste codes, seeing dozen same lines. When it works it's ok, but when you need to correct it, it's a nightmare of the first class

Looking at your code and requirements, I'd say that I would create a method which returns an user input value, while printing out what kind of dimension parameter the user should enter. In this way you can keep it general.

Re: Calling Parts of Methods

Posted 23 August 2011 - 03:48 AM

Think about this... how would you code a program if you didn't have methods?

It would *have* to be one long main() - which is itself a method.
Meaning your entire program would have to be a linear process from start to finish, with no branches and no real ability to back up, or do different things based on user input.

Do you know any program that works that way? Your web browser... your email client... a calculator...

Every time you click a button, a method is called to handle that one event.
A method is called to paint a control.
A method is called when a form resizes, so you can reposition and resize the controls to use the available space.
A method is called when you pick a menu item.

Re: Calling Parts of Methods

Posted 23 August 2011 - 03:56 AM

I know that methods are important and that they are used for everything.. Which is why I'm trying to learn how to create them with this exercise.

I never said that I thought methods aren't important. I just said that I have completed this exercise before but as one method (Main). I am now trying to redo the exercise by creating multiple methods to simplify the code. Except I'm finding it a bit more complicated as I still don't quite understand how to call the results from other methods.

I understand that I can call the whole method within another method but that's not what I'm wanting to do. I'm wanting to just a certain part of the method if it's possible.

karabasf - Thank you for your help I'll think about what you said and give it a go!

Re: Calling Parts of Methods

Posted 23 August 2011 - 04:12 AM

If you only want to call part of one method, then you will need to extract that part into another entirely different method. If you only want to call a part of a method based on some conditions, well then that what if() {} else {} blocks are for.

Re: Calling Parts of Methods

Posted 23 August 2011 - 04:19 AM

The only way you can trigger just a certain part of a method is to put conditional statements (if statements) within the method then have some kind of parameter that will tell that statement which section to execute.

Also, Your variables within GetDimensions() and GetPrice() are scoped to only the method they are created in, so they will not be seen or usable once you finish calling GetDimensions(). What i would suggest is to use global variables or return a list<int> to get those values out.

Re: Calling Parts of Methods

Posted 23 August 2011 - 04:22 AM

Thank you JackOfAllTrades! Just to clarify, so if GetDimensions() asks the user to input length and width, and I would like to use the length and width for a calculation in DetermineSquareMetres(), I could use an if() else {}?

Re: Calling Parts of Methods

karabasf - Thank you for your help I'll think about what you said and give it a go!

Glad I could help, just try and give it a go

kimmi_baby, on 23 August 2011 - 01:31 PM, said:

This is probably a silly question.. but should I be putting the if statement in the method I would like to use part of (GetDimensions())? Or the method I'd like to add it to (DetermineSquareMetres())?

One thing to say: STOP!

What exactly are you trying to achieve with the conditional (if..else) statements? As I could see from your previous post, you would like to use the length and the width for your calculation. But why would you want to trigger it via an if statement? Think about that (and off course if you need such a statement at all) and you probably find out an answer by yourself.

Personally, I always see a method as a small operation, which is part of a bigger operation. Using your exercise as an example, you can define some steps, which a user needs to take before the whole operation is done.

1) Show instructions
2) Get dimensions
3) Calculate area

Now, step 2 can be divided up in several more steps (which all can be put either in one method or separate methods for re-usage)