zetar
zetar

Reputation: 1233

Accessing instance variables across multiple c# files

What is the preferred method for accessing variables that need to be instantiated across multiple C# files?

For example a dictionary cannot be declared in one file and used in another unless it is passed from method to method.

I have the same problem with an array of bytes.

I am primarily a C++ coder and these problems are handled by include files or extern declarations.

'Small' variables in C# can be global. But this option - to the best of my knowledge - is not possible with variables that need to be instantiated like dictionaries or byte arrays.

Sorry for the newbie question but I can't find any solutions in my books or on the Internet. I've already wasted a good part of today trying to figure out how to use a dictionary in two C# files in the same project without any answers.

To try and be more detailed about the problem:

This is the declaration in MainWindow.xaml.cs:

 public partial class MainWindow : Window
{
    public static byte[,] TerrainMap;

But when I try to access it in another file PlantDisplay.xaml.cs (which shares the same namespace) it throws the error: error CS0103: The name 'TerrainMap' does not exist in the current context

Upvotes: 1

Views: 4382

Answers (5)

ispiro
ispiro

Reputation: 27643

Make a class with the variable you need as static (and public).

You can later change it as you wish from anywhere. Just declare it as static, and "instantiate" it anywhere.

Example

class MyClass
{
    public static Dictionary<int, string> dictionary;
}

And anywhere:

MyClass.dictionary = new Dictionary<int, string>();
//...

Upvotes: 1

alex
alex

Reputation: 12654

You can make your dictionary static, but that look like a poor design option. From object-oriented perspecitve, better alternative is to put the functions you need form the dictionary to a separate interface with meaningful name, like ISettingsProvider with GetValue/SetValue methods, and pass it to the constructors of other classes that need it.

You can also use dependency injection to wire these classes together.

Upvotes: 3

Fede
Fede

Reputation: 44038

Use Partial classes:

file myclass1.cs:

public partial class MyClass
{
    public SomeObject SomeProperty {get;set;}
}

file myclass2.cs:

public partial class MyClass
{
    private void SomeMethod()
    {
        var myprop = this.SomeProperty;
    }

}

When compiled, these 2 separate pieces of code will be compiled in a single class.

Upvotes: 1

Keith Nicholas
Keith Nicholas

Reputation: 44288

a public static on a class can be accessed from anywhere. C# will allow you to access any declared type even across files. Its different from C++ in that as long as a type exists within your project C# will be able to resolve the type name, you don't have to #include any headers or anything.

Having said that, accessing public static fields is often a sign of a bad design. Sometimes they are needed / make life easier, but often there's a better way to do things.

Upvotes: 0

Jacob
Jacob

Reputation: 78850

Perhaps you're looking for static members:

public class SomeClass
{
    public static IDictionary<string, string> SomeAccessibleDictionary = new Dictionary<string, string> 
    {
        { "dictionary", "data" },
        { "key", "value" },        
    }
}

This sample could be accessed with:

SomeClass.SomeAccessibleDictionary

Upvotes: 1

Related Questions