-2

Hello I have a problem passing the constructor with argument of my class. There it is:

    // Non-static variables exist once for each instance of the class
    Vector2 position;

    public Vector2 Position { get { return position; } }
    public myClass(Vector2 position)
    {
        this.position = position;
    }

I'm trying to instance it like this (Game1 class):

myClass myClassInstance;

protected override void Initialize()
{
    // TODO: Add your initialization logic here.
    myClassInstance = new myClass(Position);
    base.Initialize();
}

But I get this error at Initialize():

Error   1   The name 'Position' does not exist in the current context
PowerUser
  • 812
  • 1
  • 11
  • 32
  • 1
    I'm not sure what you're trying to do here. In your Initialize() there doesn't seem to be a variable called Position which is why it's giving you that error. – Haedrian Nov 15 '14 at 20:22
  • @Haedrian is in the Game1 class. I used this answer for my code: http://stackoverflow.com/a/11383467/3944196 – PowerUser Nov 15 '14 at 20:35
  • Please I really need some help about this... – PowerUser Nov 15 '14 at 20:53
  • If you want help, then post a good, complete, concise code example. See http://stackoverflow.com/help/mcve. As it stands, no one can tell what the relationship between your `Initialize()` method and whatever type that contains the `Position` property is. Obviously they are not in the same type, but it's not clear at all what value you should be passing to the constructor instead of the erroneous "Position". – Peter Duniho Nov 15 '14 at 22:38
  • I posted a link in the previous comment... Anyway I got it solved. I entirely removed the instance which in fact was only used for a duplicated `Draw()` method. Now the entire game functions properly. – PowerUser Nov 15 '14 at 22:55

1 Answers1

2

Your Initialize method does not have any kind of "Position" variable declared in scope. For example, you might do something like this:

myClass myClassInstance;

protected override void Initialize()
{
    // variables defined in a function are typically lower-case in C#
    Vector2 position = new Vector2(0,0);

    // I changed "Position" to "position" to match the variable
    myClassInstance = new myClass(position);
    base.Initialize();
}

However the bigger problem here may be that you need to spend a little time getting up-to-speed on your C# before continuing, as this is probably the first of many problems you'll run into if the syntax doesn't make sense to you. Here's a great video series to start with:

https://www.youtube.com/playlist?list=PLMUNVW3VsMWUB9mPN10vU6jvc_jbHvs1g

Victor Chelaru
  • 4,491
  • 3
  • 35
  • 49