Reputation: 2765
I have this method which returns a list:
public List<int> GetCapabilitiesInfo()
{
List<int> sizeList = new List<int>();
int cellSize = m_tree.db.getCellSize();
int mapSize = m_tree.db.m_mapInfo.mapsize;
sizeList.Add(cellSize);
sizeList.Add(mapSize);
return sizeList;
}
The list which is being returned above is one I wish to use in this method in another class: But it fails because it can't access a non-static field in a statix context.
public static void TileMapCapabilities(string title)
{
// This method call from another class fails because it can't access a non-static field in a static context
TilePicker.GetCapabilitiesInfo();
TileMapObject tmo = new TileMapObject()
{
Title = title,
Abstract = "Some clever text about this.",
SRS = "OSGEO:41001",
Profile = "local",
BoundingBox = new BoundingBox() { maxX = 10000000, maxY = 10000000, minX = -10000000, minY = -10000000 },
Origin = new Origin() { maxX = 123, maxY = 456, minX = -123, minY = -456 },
EBoundingBox = new EBoundingBox(),
MapSize = 256,
CellSize = 3
};
}
What I'm trying to do is add the two ints cellSize
and mapSize
to the list which I want to return so I can pick out the two values in it and insert it into my TileMapObject
. If I make GetCapabilitiesInfo()
static then I just get the same problem with m_tree
and so on. I'm sure I'm overlooking something fundamental here?
Upvotes: 0
Views: 82
Reputation: 37000
You need to create an instance of class TilePicker
because your method GetCapabilitiesInfo
is not marked static.
TilePicker t = new TilePicker();
t.GetCapabilitiesInfo();
However you may also mark the method static whereby you won´t need an instance:
static void GetCapabilitiesInfo() {...}
Upvotes: 0
Reputation: 152521
I'm assuming you can't make GetCapabilitiesInfo
static since it appears to use instance members like m_tree
. If that's the case, then you'll need to have an instance of TilePicker
in order to call GetCapabilitiesInfo
. So you'll need to either create one within TileMapCapabilities
:
public static void TileMapCapabilities(string title)
{
// This method call from another class fails because it can't access a non-static field in a static context
TilePicker tp = new TilePicker();
// initialize tp?
tp.GetCapabilitiesInfo();
or add a parameter so one can be passed in:
public static void TileMapCapabilities(string title, TilePicker tp)
{
// This method call from another class fails because it can't access a non-static field in a static context
tp.GetCapabilitiesInfo();
If neither of those are options then you'll have to look at the design in order to connect the two classes. Since your current code does not do anything with the list that is returned it's not clear how they should be connected.
Upvotes: 0
Reputation: 1518
Your list is part of class that is not static and so, you need an instance of the object it declares. You can do this:
public static void TileMapCapabilities(string title, TilePicker picker)
{
var list = picker.GetCapabilitiesInfo();
//do whatever you want to do with the list;
}
You can also pass the list itself:
public static void TileMapCapabilities(string title, List<int> list)
{
//do whatever you want to do with the list;
}
Just remember that the class where the routine you want to call is, is not static, so, you have to instantiate it to use the routine.
Upvotes: 2