6 Replies - 926 Views - Last Post: 07 October 2015 - 06:02 PM Rate Topic: -----

#1 Domin8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 15

C-Sharp - Writing Methods(I'm a noob)

Posted 04 October 2015 - 01:20 PM

Hey guys.

I'm 100% new to programming and I recently started learning C# at my uni using Visual Studio 2015, and I have some issues understanding methods fully. I've tried looking up info and looking at videos for better understanding but apparently I have a thick skull.

So, I understand what methods are and what they do, but the problem is making my own. I'm having a hard time understanding the difference between ref/out parameters when using the void datatype/keyword, and whether to use void with parameters or int/double/string or whatever.

Now, I've read about the difference between ref/out but I still haven't found someone who put it simplisticly, in a way that I can assert myself which one would be better for the relevant problem.

MY second problem; when writing a method using a return value(?), as in not using void - how do I know what datatype to use for the method itself and the parameters?

This is really confusing to me. And I apologize beforehand as I probably sound like an idiot - but hey, I only just started learning this a little over a month ago.

Is This A Good Question/Topic? 0
  • +

Replies To: C-Sharp - Writing Methods(I'm a noob)

#2 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,892
  • Joined: 12-December 12

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 04 October 2015 - 02:13 PM

Try not to mingle everything together. void is straightforward, it just states the the method won't return a value:
public void SampleMethod()
{
    // Body of the method.
    // optionally..
    return;    // no value returned
}


This is its only use (ignoring "void is also used in an unsafe context to declare a pointer to an unknown type" mentioned in the link for the moment).

We have a good tutorial here on DIC for C# Methods. I suggest you work through this and its examples. Click the Tutorials link at the top of this page for others.

Don't worry about ref just yet. Once you understand methods, how to declare and pass arguments, and specify the return type, then you can revisit 'ref'. (Methods :MSDN). Basically, ref is used on rare occasions when you might want to modify a passed value, rather than just relying on what is returned by the method.

This post has been edited by andrewsw: 04 October 2015 - 02:15 PM

Was This Post Helpful? 2
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 04 October 2015 - 03:47 PM

I suggest the block of questions starting at 33,
specifically number 36 "Q: I've been struggling with this for days/weeks and I can't figure it out and my professor is worthless and can't teach. Can someone here explain it to me?"


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.
Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute, check the condition of each of variable's run-time value, and watch the logic unfold right before your eyes.
Visualizing what your code does will let you see why it behaves the way it does.
It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

In addition to FAQ 2 in the list below, also check my signature block for a link on how to look at your variables' values at run-time.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?


WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.


FAQ (Frequently Asked Questions - Updated Sep 2015
Spoiler



Was This Post Helpful? 1
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 05 October 2015 - 08:06 AM

Edit: forgot that I had written a methods tutorial. Please read the following article:

http://www.dreaminco...4-c%23-methods/

Let's start by revisiting what a method is. It's a function that belongs to class. A function is simply a block of code that can be repeatedly executed. It may or may not "return" a value back to the caller.

Let's not overcomplicate things. Forget about ref and out for a minute. They're less frequently used, and you need to understand some other concepts (like reference types vs. value types) before you can grasp them. Trust me when I say they shouldn't affect your understanding of methods themselves. All they do is modify a method's parameters.

Which brings us to parameters. Methods can take any number of "parameters" or "arguments" (same thing), including zero. The reason you would want to pass things in is because the method might need information to do its job. For example:

public void AddAndPrint(int x, int y)
{
  int z = x + y;
  Console.WriteLine(z);
}


In this case, the "AddAndPrint" method can't do its job unless you give it two integers to add together and print to the console.

Note in our example, the "return type" is void. A better way to say that is "this method does not return a value".

If you like, you can split them up in your mind: methods that do return a value, and methods that don't. VB.NET has actually logically separated these, calling the former "functions" and the latter "subroutines".

That's a good distinction. You would use void when you want your method to take some kind of action, like printing or saving to disk or any number of things, but you don't need it to send a value back to the caller.

The other kind of method is the function that returns a value. The best analogy I can give for this type of function is...imagine you're back in a math class. Think of functions with return types as your calculator. You put values in, and it "returns" a value back to you. If the calculator calculated your answer but didn't give it back to you, it'd be a pretty useless thing.

So, functions that return a value are a handy way of saying "this chunk of logic is repeatably callable. Give me X values in, and I'll give you Y back."

Example:

public int Add(int x, int y)
{
  int z = x + y;
  return z;
}

//...later in the code
int answer = Add(5, 10);
int answer2 = Add(answer, 6);



See, we might want to use the same logic twice. That's why we make functions that can return a value.

Now, how to determine the type? Well, what kind of value do you want to work with? It's as simple as that. If you need an integer back, use int. Use the datatype you need.
Was This Post Helpful? 1
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 05 October 2015 - 04:27 PM


My standard beginner resources post - Updated June 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs, commonly text adventures or DnD or some other game. Games are truly the worst thing to start with.
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.


Why are you still building in WinForms?

Quote

"old 1990's WinForms"? i just use visual studio 2015 and started up with a windows form. didn't know there was an option for a newer winform?

WinForms is OLD. Virtually 'legacy' and to many of us nearly considered end-of-life. There are still placing building new projects in it, but I wouldn't work for anyplace that wanted to hold me back by two decades. There are probably tens of thousands of legacy WinForms applications in use in countless companies. "If it ain't broke, don't fix it." in other words: If there isn't a financial gain to be made in re-writing them right now why invest the time and money?

But with decades of WinForms comes developers with decades more experience than you'll have and you have no way to compete with them. Let the legacy developer maintain the legacy applications. Many of them are too stuck in their ways to move forward to WPF. Which is why if you look at job boards, what companies are hiring for and have been looking for, for the last several years its: WPF/MVVM.

So if I were you I would just jump right to WPF. Don't worry about WinForms unless you have a deliberate need for it down the line. Otherwise you spend 10,000 hours getting good at WinForms only to find you need to spend another 7,500 hours bringing your skillset forward several years into WPF - And at the same time break all the bad habits you learned in WinForms.

If some company hires you for WPF development, but would also like it if you could maintain some legacy applications THEN learn WinForms; and take your good habits from WPF with you.




I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 1
  • +
  • -

#6 Domin8  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-October 15

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 07 October 2015 - 02:38 PM

I've gotten through some of the tutorials and it helped with my understanding of methods as a whole. I'll definitely keep on working through the tutorial/guides, I found them very helpful and practical. I've also started using the debugger to get a better understanding of how the computer executes codes.

So thanks a lot for the help lads, much appreciated. :smile2:

One thing though, I read what you said about Windows Forms, tlhIn`toq, and I found it kind of alarming as we started learning C# in Windows Forms. However, this is only the beginner course. I think we're going to use the console mostly next semester, not sure though. My degree isn't programming however, it's within computer science and automation, so it's not pure programming-based.
Was This Post Helpful? 0
  • +
  • -

#7 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C-Sharp - Writing Methods(I'm a noob)

Posted 07 October 2015 - 06:02 PM

Don't worry too much about WinForms vs. WPF in school. You're going to have to learn whatever technologies your various classes make you learn. And truthfully, it's not "bad" to learn Windows Forms. It's good. It's just better to learn WPF if you were planning on using C# practically in the near future. For all we know, by the time you need to use it, something else might be the "new thing". WPF's not even necessarily the "new thing" anymore, Windows Store apps are. They're very similar to WPF, so there's that.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1