Problem with "Cannot access nonstatic member of outer type via nested type"!  
Author Message
Javawag





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

I am relatively new to C# and i'm trying to make a simple game. For each type of game "object" i am using classes and storing the instances of these in Lists.

public partial class TestForm : Form
{
public TestForm() {}...

List<objExplosion> objExplosions = new List<objExplosion>();

class engineObject
{
public bool destroyed = false;
}

class objWall : engineObject
{
public Point Location;
public void destroy()
{
destroyed = true;
objExplosion myExp = new objExplosion();
myExp.Location = Location;
objExplosions.Add(myExp);
}
}

class objExplosion : engineObject {}...
}

This is the code I am using. I have not shown the objExplosion class or the TestForm() part as they are irrelevant. I have underlined the part of the line causing the problem.

How do i make the List, "objExplosions", visible to the class objWall

Thanks for your time reading this, and hope you can help!! If you need more info, just ask.

- Javawag


Visual C#13  
 
 
ahmedilyas





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

you need to pass a reference of that object into the class you wish to access it.

passing variables between classes:

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=729974&SiteID=1

perhaps you can have some base method that will add the object into the collection of the base class



 
 
Javawag





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

I have no idea what that means! I can't add a base method though, as I need an instance of the class (objExplosion) to be made when an instance of another class (objWall) is destroyed (ie, in its destroy() method).

All I need to do is to make the lists available to any piece of code in that form. Do you have any ideas as to how to acheive this (Putting the word "public" before it gives another error, "Inconsistent Accessibility: field type List<objExplosion> is less accessible than objExplosions" (note: objExplosions is the name of the list of objExplosion instances.))

Thanks,
- Javawag

 
 
ahmedilyas





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

have you read the link i supplied you would need to pass a reference/instance of that object from one class, to the other so it can have its own reference and access that object.

 
 
pradeep_toluganti





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

Hi

make the varible destroyed as protected.

Regards,

Pradeep


 
 
mberseth





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

Javawag -

If you aren't going to pass a reference to the objWall object, you can make the objExplosion list static/internal and reference the collection on the Form1 class directly. Keep in mind that all instances of the form will share the same collection, if this is a problem for your app, you should consider the original suggestion ...

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

static List<objExplosion> objExplosions = new List<objExplosion>();

class engineObject

{

public bool destroyed = false;

}

class objWall : engineObject

{

public Point Location;

public void destroy()

{

destroyed = true;

objExplosion myExp = new objExplosion();

Form1.objExplosions.Add(myExp);

}

}

class objExplosion {}

}



 
 
decyclone





PostPosted: Visual C# Language, Problem with "Cannot access nonstatic member of outer type via nested type"! Top

Hi Javawag,

I made some changes to your code and commented it well, just check it out.

public partial class Form1 : Form
{
// I don't know why you left this list alone in the Form class,
// when following object oriented concepts.
List<objExplosion> objExplosions;

class engineObject
{
public bool destroyed = false; // Destroyed flag is common for all engine objects

public virtual void destroy() // Then why not a common Destroy() method which provides a default behavior and can be extended
{
this.destroyed = true;
}
}

class objWall : engineObject
{
public Point Location; // If this variable is common for all engine objects, I suggest you take it to engineObject class
public List<objExplosion> objExplosions; // The class can not access members of other classes
// So I added a variable to store explosion list

public objWall()
{
Location = Point.Empty;
objExplosions = null;
}

// When creating a Wall object, an existing explosion list is passed
public objWall(List<objExplosion> InObjExplosions)
{
// This stores the already created list's refenrece
// It does not create a new one
this.objExplosions = InObjExplosions;
}

public override void destroy()
{
base.destroy(); // First default Destroy action

// Extend the Destroy() functionality
objExplosion myExp = new objExplosion();
myExp.Location = Location;
objExplosions.Add(myExp);
}
}

class objExplosion : engineObject
{
public Point Location;

public objExplosion()
{
Location = Point.Empty;
}
}

public Form1()
{
InitializeComponent();

objExplosions = new List<objExplosion>();
}

private void Form1_Load(object sender, EventArgs e)
{
// Here is the sample code for using the objWall class
objWall Wall = new objWall(this.objExplosions);

Wall.destroy();
}
}