Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

C# Classes [Intermediate Level Tutorial]

C# Classes


Getting Started:
The first thing we want to do when creating a new class is, create the class! To do this go to the top of your IDE and click on

Project-->Add Class

Name is whatever you want, in this case, I will be using "hf.cs"

Once you have created your class, go over to your solution explorer on the right side of your IDE and double click on the class.

[IMG]http://i52.[censored].com/6gewlz.jpg[/IMG]

Once inside of your class you will see two curly brackets. A lot like your form, everything in your class should go in between these brackets.

Insert the following:
Code
public string hack()
        {
            return "Hackforums!";
        }


If you do not know what a string is, please see my variables tutorial.

"Public" allows things outside of the class to see the string. If we used the word "Private" only things inside of the class would be able to see it. But since we want to use it from our form, it must be public.

"return "Hackforums!";" is going to return the string Hackforums.


Alright, so go back to your form and create a new button. Double click the button and insert the following:

Code
hf test;

            test = new hf();

            MessageBox.Show(test.hack());

hf is the name of our class and test is going to be our variable name.
By doing all of this we are setting up our variable and creating a new object.

MessageBox, if we did it correctly, should show the string text that we setup earlier.


[IMG]http://i53.[censored].com/28gwhzq.jpg[/IMG]

YAY!

Conclusion:

This was an extremely basic intro to Classes, in the near future I will create another tutorial that will go much more in depth.

Code:

Form:
Code
private void button1_Click(object sender, EventArgs e)
        {
            hf test;

            test = new hf();

            MessageBox.Show(test.hack());
        }

Class:
Code
class hf
    {


        public string hack()
        {
            return "Hackforums!";
        }


    }
http://www.pulsecodes.info
Computer and Information Science major.
Information Technology Apprentice.
Freelance Programmer.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by dummycartSep 9, 2021 03:38 AM 1 146 Sep 9, 2021 03:38 AM
Last Post: Lindros
[Python] by CiataLearnerDec 22, 2018 02:27 PM 1 212 Dec 22, 2018 02:27 PM
Last Post: notjapanese
[C] by Adam2017Dec 3, 2018 03:06 PM 2 167 Dec 3, 2018 03:06 PM
Last Post: Adam2017