How can I link classes/methods from different files?

I’m having heaps of trouble with a game I’m making, I cant seem to figure out how to link classes which are in another file. I dont want to shove everything onto one page. How do i achieve this, thanks.

`
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Game1
{

public class Game1 : Game
{
    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        charactermovement();

            // TODO: Add your update logic here
            physics();

        base.Update(gameTime);
    }`


  ---------------------------------------------------------------

`using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;


namespace Game1
{
class Movement
{
    public void charactermovement() {
        if (Keyboard.GetState().IsKeyDown(Keys.Left))
        {
            Player1characterPosition.X = Player1characterPosition.X - 5;
        }
    }
  }
 }`
// this is by default protected 
class Movement   

// Make your class public
public class MyClass
{
   
   // Make a public method that can be called from a instance of the class.
   // created in another class.
   // you probably don't really want to ever pass game this way to update. 
   // this is just a extreme example of how you can pass things around.
   public void MyMethod(Game passMeThis)
   {
        // passMeThis.TheDotTellsAllInVisualStudio type it and look at what pops up.
   }
}

// in game 1
public class Game1 : Game
{
    // create a instance of your class in game1
    MyClass myClassInstance = new MyClass();

    protected override void Update(GameTime gameTime)
    {
            // call the method of your class and pass to it. 
            // whatever you want from game1.
            myClassInstance.MyMethod(this);

        base.Update(gameTime);
    }
    }`
1 Like

Thank you so much dude, you are the best, Ive been trying to get it right for hours THANK YOU SO MUCHHH

Your welcome.

However its pretty basic object oriented stuff, don’t be afraid to ask though…

There are friendly forums for learning the basics as well.
They probably can’t help much for monogame specific things but for basic c# questions.

Sites like this one are a good place to alternatively visit.
http://forums.codeguru.com/forumdisplay.php?11-C-Sharp-Programming&s=9b3bbc8113a39012935e067dbf4530aa

Don’t get to obsessed with doing that you leave no time for learning when your frustrated its a sign that its time to start asking around the block and looking for places with appropriate help. Also Googling your question in a way to make it as short and clear as you can. You will usually get a instant answer.

2 Likes

Also, if you NEED to keep everything in the same class, you can put part of the class in different files.
You just need to use the PARTIAL keyword in the class declaration in both files.

https://msdn.microsoft.com/en-us/library/wa80x488.aspx

For instance, if your game class draw method gets too big, you can put it in a different .cs file to help readability.

1 Like