Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Methods in C#


  • Please log in to reply
9 replies to this topic

#1 Josef1994

Josef1994

  • Members
  • 111 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:01:29 AM

Posted 08 October 2011 - 07:42 AM

Can somebody please explain to me how to use methods, classes and the return value in C#? Any help will be much appreciated.

Edited by Josef1994, 09 October 2011 - 05:46 AM.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 AM

Posted 09 October 2011 - 10:07 AM

Using methods or functions is exactly the same in any language. The question you asked covers pretty much the first few weeks of any introductory programming course. DO you have some specific questions that can;t be answered by doing some research?

#3 CompSecurityExpert

CompSecurityExpert

  • Members
  • 7 posts
  • OFFLINE
  •  
  • Local time:01:29 AM

Posted 21 October 2011 - 06:03 PM

Can somebody please explain to me how to use methods, classes and the return value in C#? Any help will be much appreciated.


The Below Is some code that I just wrote so you can see both an integer function and a void static function. Void Functions just execute the code below or that method whatever you want to call it. You can compile this yourself
if you are using a console application though you will have to right click on it in the solution explorer and hit add reference and then select system.windows.forms since I used some calls to Windows message boxes here. Hope this helps: There are probably easier ways we could have done this but I'm using this to try and help you understand methods in classes in C# and what not. Another thing you might learn are Generic Methods which are type safe and what not:: I don't know if you have learned or care about that in any case I hope this helps let me know if it doesn't.!!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace FunctionDemoForBleepingComputer
{
    class Program
    {
        //main function all lines of code in between brackets will be executed
        public static void _MsgQuit()
        {
            switch (MessageBox.Show("Would You Like to Add Numbers Again? This Application Will Restart If You Click Yes, Or Else We Will Exit If You Click NO?", "More yes No", MessageBoxButtons.YesNo, MessageBoxIcon.Warning))
            {
                case DialogResult.Yes:
                    //we exit the application
                    Application.Restart();
                    break;
                case DialogResult.No:
                    Application.Exit();
                    break;
            }

            //end of this void function last brace i.e. end of code
               }


        //integer ADD function returning two values
        public int FuncAdd(int a, int <img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />
        {
            //just an integer function to reutn to value arguments passed to it
            return (a + <img src='http://www.bleepingcomputer.com/forums/public/style_emoticons/<#EMO_DIR#>/cool.gif' class='bbc_emoticon' alt='B)' />;

        }
        ~Program()
        {
            //cleanup you don't need this if you don't want too but bahh!!  That is the deconstructor with the stupid ~
        }

        //Main
        static void Main(string[] args)
        {
            //we will try this if the user passes arguments like letters abc to the add func we must catch that
            try
            {
                //get our two arguments for our function note: console stuff is in a string format so we have to convert it To In32
                Console.WriteLine("We Are Going To Add Two Numbers Please Enter Number One?");
                int NumOne = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("Enter The Next Number");
                int NumTwo = Convert.ToInt32(Console.ReadLine());

                //create a new instance of the method in that class because we didn't make it static unlike our msgquit void func -- example purposes
                Program AddNums = new Program();

                //display the numbers added
                Console.WriteLine("The Numbers Added Equal, {0}", AddNums.FuncAdd(NumOne, NumTwo));
            }
            catch
            {
                MessageBox.Show("You Entered Something Wrong Please Enter Only Numbers Like 12, 56, etc. When Asked The Application Will Now Restart: Or Exit Try Again");
                
            }

            //lastly call our last void STATIC function
            _MsgQuit();


            
        }
    }
}





Edited by CompSecurityExpert, 21 October 2011 - 06:10 PM.


#4 chromebuster

chromebuster

  • Members
  • 899 posts
  • OFFLINE
  •  
  • Gender:Female
  • Location:the crazy city of Boston, In the North East reaches of New England
  • Local time:02:29 AM

Posted 17 December 2011 - 10:36 PM

That code helped me to understand a bit better. Thanks! I'm working on this very thing too. And like the original poster, a little bit confused by it. What I still don't understand though is when you have the static void Main(String [] args) code, why the arguments on the command line aren't placed with in that array like args = arg 1, arg2, and so on.

The AccessCop Network is just me and my crew. 

Some call me The Queen of Cambridge


#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 AM

Posted 18 December 2011 - 10:19 AM

Have you ever dumped out the contents of the args array to see what is in there? Maybe it would answer your question.

#6 chromebuster

chromebuster

  • Members
  • 899 posts
  • OFFLINE
  •  
  • Gender:Female
  • Location:the crazy city of Boston, In the North East reaches of New England
  • Local time:02:29 AM

Posted 18 December 2011 - 01:46 PM

You mean via Ildasm? I was trying to get that to work and it like failed. I couldn't save the file for some reason.

The AccessCop Network is just me and my crew. 

Some call me The Queen of Cambridge


#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 AM

Posted 18 December 2011 - 02:53 PM

What does 'it didn't work' mean? What was the error message when it failed? Did you research the error message?

#8 chromebuster

chromebuster

  • Members
  • 899 posts
  • OFFLINE
  •  
  • Gender:Female
  • Location:the crazy city of Boston, In the North East reaches of New England
  • Local time:02:29 AM

Posted 18 December 2011 - 02:57 PM

It wasn't like that. You have to understand that I do not use the mouse to operate the computer. I am all keyboard usage because I'm as blind as they come. But I doubt that had anything to do with it. What I did was I opened a project of mine (one of my demos) and then I had it dump out the whole contents of the project via the file menu. When it came time to save, I had the file named, placed, and then I hit save. I went to the location where I had put the file, and it just wasn't there.

The AccessCop Network is just me and my crew. 

Some call me The Queen of Cambridge


#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:12:29 AM

Posted 19 December 2011 - 02:28 PM

How you interface with your PC is irrelevant. I also don't know what you mean by 'had it dump out the whole contents of the project via the file menu'. I have no idea what IDE you are using. I also do not know where you were trying to save the file. Depending on your OS, some directories will be blocked from being written to. What was the location of your save file?

#10 chromebuster

chromebuster

  • Members
  • 899 posts
  • OFFLINE
  •  
  • Gender:Female
  • Location:the crazy city of Boston, In the North East reaches of New England
  • Local time:02:29 AM

Posted 19 December 2011 - 02:48 PM

I am a VS 2010 professional user, and my goal was to try and save the .il dump of the project under one of the folders in my documents. What I meant by dump from the file menu of the IL disassembler is that I tried to use the dump command from the file menu. When you go to the file menu, there is either dump, or dump treeview. If you only have Ildasm dump the treeview, then it will only save the current view, but if you click dump, then it will save the current view of your project and not give you as much information, but if you click dump, then it will allow you to save your project as a .il file and the result will be the actual IL that the C# code compiles to, and that's what I was trying to get but I couldn't save the file for some reason for the program just didn't do anything.

The AccessCop Network is just me and my crew. 

Some call me The Queen of Cambridge





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users