Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Augs on February 26, 2013, 04:47:01 pm

Title: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Augs on February 26, 2013, 04:47:01 pm
Sorry to bother you again. I know I post here quite a lot for help. I have tried googling it.

Any way I need help changing the Accessibility Domain(?). Normally I declared all the variables at the top but this time I must declare an array in a method.

Thanks in advance and sorry to bother you again.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Scipi on February 26, 2013, 05:06:36 pm
What are you trying to do, exactly? It looks like Accessibility Domain is more related to Access Modifiers in classes.

For instance, you had a private internal class

Code: [Select]
public class MyClass{

    public static int foo = 0;

    static MyClass(){
        MyInnerClass.bar = 0;
    }

    private class MyInnerClass{
        public static int bar;
    }
}

MyClass.MyInnerClass.foo would be inaccessible since MyInnerClass is private.

So this doesn't seem too related to your problem.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Augs on February 26, 2013, 05:41:36 pm
Code: [Select]
public void DetectNPC()
{
    [...](Other code in method)
    Vector2[] PhilCharNPCHitVectors = new Vector2[ArrayLength];
    float[] NPCHitXCoord = new float[ArrayLength];
    float[] NPCHitYCoord = new float[ArrayLength];                   
    [...]
}

private void UpdateInput()
 {
            OldState = KeyState;
            KeyState = Keyboard.GetState();
            if (KeyState.IsKeyDown(Keys.S) && themap.HitMap[Convert.ToInt32(YCoord) + 1].Substring(Convert.ToInt32(XCoord) * 2, 2) == "..")
            {
                YCoord = YCoord + 1;
            }
            if (KeyState.IsKeyDown(Keys.W) && themap.HitMap[Convert.ToInt32(YCoord) - 1].Substring(Convert.ToInt32(XCoord) * 2, 2) == "..")
            {
                YCoord = YCoord - 1;
            }
            if (KeyState.IsKeyDown(Keys.D) && themap.HitMap[Convert.ToInt32(YCoord)].Substring((Convert.ToInt32(XCoord) * 2) + 1, 2) == "..")
            {
                XCoord = XCoord + 1;
            }
            if (KeyState.IsKeyDown(Keys.A) && themap.HitMap[Convert.ToInt32(YCoord)].Substring((Convert.ToInt32(XCoord) * 2) - 1, 2) == "..")
            {
                XCoord = XCoord - 1;
            }
            NextMap();
}

So I need to access the arrays from DetectNPC in UpdateInput. The arrays must be declared in the DetectNPC. Passing is not an option as UpdateInput is not called in DetectNPC.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Scipi on February 26, 2013, 05:51:44 pm
You should try defining them outside DetectNPC() and make sure both methods are within the same class. That way, they are accessible by both methods.

The problem here is that the arrays are out of scope. That is, they are only defined within DetectNPC(). When you leave DetectNPC() via return, those arrays are cleared off the stack. You should either make them global variables, or members of a class that contains both methods.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Augs on February 27, 2013, 12:11:44 pm
I was planning on that but, I have to delete the array every time DetectNPC runs as it is supposed to change size every time.

Edit:
The problem here is that the arrays are out of scope. That is, they are only defined within DetectNPC(). When you leave DetectNPC() via return, those arrays are cleared off the stack. You should either make them global variables, or members of a class that contains both methods.

That's what I am trying to change, I want to have the array in scope while still declaring it in the method. Any way of doing this?

Oops double post edited into one post.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Scipi on February 27, 2013, 02:23:15 pm
I don't think there would be any way, you would have to delete or overwrite the arrays each time, unless there's a container you would use like a Vector that you can change the size of dynamically.
Title: Re: [Help]C# Accessibility Domain(I think that's what it's called.)
Post by: Augs on March 07, 2013, 05:00:13 pm
Problem solved. I used lists.