Simon Verbeke
Simon Verbeke

Reputation: 3005

Acessing an original array from another class C#

I'm working on a game where I have a class for my character and a class to handle the GUI.

In the character class I have an array of structs which hold data about the character's skills. I'm trying to access this array with the GUI class, to display the info about the skills.

To use one class in another one, I would need to initialize a new instance of it, which resets the values I might have changed in the meantime. Is there any way to pass that array? I can't make it read-only because I still have to change the array. The array is also not being modified in the GUI class.

Also, in the future I'm going to save this data in XML or in a database. Is it a possibility to get the info out of these files every time I need them? Instead of having to work with passing the array on and things like that. Or even calculate everything server side?

Thanks in advance!

Simon.

Upvotes: 1

Views: 8562

Answers (6)

Faux
Faux

Reputation: 1

I had googled my problem and was take here, I used the answers here to do mine.

When a user clicked a button I wanted the code to be carried out in another class called Logic. My main class was called MainWindow.

I had an array of colors linked to two rectangles in MainWindow. In order to be able to access the values stored in the array i simply had to do this.

public partial class MainWindow : Window
{

 Rectangle[] choice;
 SolidColorBrush black;
Became
 public static Rectangle[] choice;
 public static SolidColorBrush black;

}

in my Logic class to access it I had to do this.

MainWindow.choice[0].Fill = MainWindow.black;

Hope that helps anyone at all.

Upvotes: 0

George Polevoy
George Polevoy

Reputation: 7671

Are you using structs in Array?

This way, you may be accessing the copies, they are not reference types in C#.

public struct PointStruct
    {
        public int X;
    }

    public class PointClass
    {
        public int X;
    }

    [TestMethod]
    public void TestStruct()
    {
        var structArray = new PointStruct[1];
        var classArray = new PointClass[1];

        int x;

        x = structArray[0].X;

        try
        {
            x = classArray[0].X;
        }
        catch(NullReferenceException e)
        {
            Console.WriteLine(e.Message);
        }

        classArray[0] = new PointClass();

        // It's now ok
        x = classArray[0].X;

        var point1 = structArray[0];
        var point2 = classArray[0];

        point1.X = 1;
        point2.X = 1;

        Assert.IsTrue(point2.X == 1);
        Assert.IsFalse (structArray[0].X == 1);

        structArray[0].X = 1;
        Assert.IsTrue(structArray[0].X == 1);
    }

Upvotes: 0

alexD
alexD

Reputation: 2364

Either your character should be static or you should store a reference to your character (or a List<Character> in your GUI class. Furthermore, it would probably be best not to provide direct access to the list of structs that maintains your character information if you just want to print. Your Character class should provide a method(s) for printing out the character data, i.e. character.printCharacterInfo() which will return a string, or maybe character.getCharacterInfo which will return the struct containing the data for that character which your GUI class can use to do whatever it wants.

Upvotes: 1

Seth Carnegie
Seth Carnegie

Reputation: 75130

If you want to have one array for the entire program, simply make the array public static. If you want to have that class be able to access the array inside characters that you pass it, make the array public or make a property for which the get part is public.

Upvotes: 2

Stealth Rabbi
Stealth Rabbi

Reputation: 10346

It is not good practice to pass data in to a GUI directly, if it can be avoided. I would suggest using the Model-View-Controller pattern or the Model-View-Presenter pattern. If you're using WPF, you should look at Model-View-ViewModel.

Basically, you want a class whose responsibility is to maintain the data (whether it be stored in memory, a database, etc.), the view needs some abstraction from the data, and also a means to format it for presentation/display purposes (Presenter).

Here is a brief overview : http://en.wikipedia.org/wiki/Model-view-presenter

Upvotes: 1

Kamyar
Kamyar

Reputation: 18797

If your class is not static (which seems not), you can define the array as a property of the character class and have access to array like: myCharacter.Skills.

If you want to have access to your Entities (character class in your case) in multiple projects, you can implement a multi-layer architecture and define your entities in an isolated class library and reference this class library in any project that should have access to you entities.

Upvotes: 1

Related Questions