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
    }
}
 
Hello Guys

@Skydiver
If you declare (and instantiate) automobile

Car automobile = new Car();

then we should use the same name in the call to Move()

automobile.Move();

I say this more for @Ryobi than for you, because you already know.

For any kind of animation you need variables:

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

// declaring an object type (a class) called "Car"
class Car
{
    // declaring an integer variable for the start X
    private int Start = 400; // or what you want
    // declaring an integer variable for reporting the movement
    private int X = Start;
    // declaring another variable for the Y of the "car"
    private int Y = 300; // or what you want
    // declaring another variable that is the reach point of the "car"
    private const int Point;
    // declaring another variable for the sense (right/left)
    private int Sense = 1; // 1 is for right and -1 for left

    // declaring a method that doesn't return anything
    public void Move()
    {
        int scroll = 3; // or what you want
        
        while (true)
        {
            X += scroll * Sense;
            DrawCar(X, Y);
            if (X >= Point)       // >=   [this supposing Point
            {                                         [
                Sense = -Sense;            [
            }                                         [
            else if (X <= Start)  // <= [ is on the Right of Start
            {
                break;
            }
        }
    }
    
    // declaring the method that draws the "car"
    private void DrawCar(int x, int y)
    {
       // code that implements the car to DRAW goes here
    }
}

If you come from VB you won't find C# very difficult.
You mentioned a button, so I suppose you're talking about Windows Forms. If so, you can easily draw on the form itself or you can add a PictureBox to the form.

Regards
Pablo
 
You are correct. No coffee this morning when I was writing that up.
 
Back
Top Bottom