Calling Procedure in C#

Ryobi

New member
Joined
Jan 12, 2022
Messages
1
Programming Experience
3-5
Hello every one. I have a quick question. I working in a small project to learn C#. My background is VB and working C# is sort on and off thing, but I want to learn more. Here is question. I have button a put
form that used to move an object forward and to certain point and then move it back to the beginning. I have the logic correct however I need to reused code so I need to create a procedure (It I believe it is call Class in C# since it does return any values). I know that in C# a class can not be call directly so instance must be made. Ideally would be to to have procedure in side that button but I can seem to figure it how. On thing is that not want any value returned back. I need something like what is below which does not work. Any ideas on this?


// ** Call MoveObject

MoveObject()

// **** Create Class

class MoveObject
{
// ** Code to move object is placed here.

}
 
If you'll pardon the pun, I'm moving this thread to C# General Discussion sub-forum since this has nothing that is VS.NET specific.
 
This forum is not really geared to be a general C# learning platform. It seeks to help people with specific issues. For general learning, I would highly recommend learning from a book, or a series of web posts specifically geared for teaching C#. If you are video learner, perhaps a series of videos for PluralSight or Khan Academy. (I have yet to find a good C# teaching series in YouTube -- the ones I've stumbled over tend to be terrible despite their claims of being the best way to learn.)

Anyway, as general overview:
In object oriented programming -- which was the core design concept for the C# language -- there are objects, and these objects have procedures/functions that tell the object to do something. (Most programming schools of thought make the arbitrary definition that procedures don't return anything, while functions are specifically meant to return something. But about procedures that have out parameters?) In the C# world, you define the characteristics of the objects by declaring a class. Within the class, methods represent procedures and functions. So to map to your example above, you would have something like:

C#:
class Program
{
    static void Main()
    {
        Car automobile = new Car();    // create a "Car" object
        car.Move();    // call the "Move" procedure
    }
}

// declaring an object type called "Car"
class Car
{
    // declaring a method that doesn't return anything
    public void Move()
    {
        // code that implements the car to move goes here
    }
}
 
Back
Top Bottom