Scope of objects across seperate source files

  • Thread starter Thread starter surfrat_
  • Start date Start date
S

surfrat_

Hi,

My project has about 4 source files (xxx.cs) and I am having a problem
with scope between the files. If I put the code all within one class
everything works OK. Can you please point me to some good resources
which explain scope on the file/class level.

What I want to do is as follows:
File A containing class AA
File B containing class BB
Access members of BB from A.

I am getting myself all twisted in knots :)

Thanks

SurfRat.
 
SurfRat,

You need to include the public access modifier on all members of BB to
be able to access them from A.

// File B
internal class BB
{
public void DoSomething() { }
}

// File A
internal class A
{
public void UseBB()
{
BB x = new BB();
x.DoSomething();
}
}

Brian
 
You need to reference BB in A.

You can then either create an instance of BB in A and call its methods, or
you can create an instance of BB somewhere else and pass it into A as a
parameter to a method in AA, or as a return value of a method in AA. Either
way, once you have an instance of BB, you can call methods on it.

Peter
 
Hi,

Thanks for the help so far. I will give you a specific example I am
battling with...

File DataManipulation.cs

namespace WeatherStation
{
public class DataManipulation
{
public DataManipulation()
{

}

public void InitializeWeatherStationList()
{
List<WeatherlinkDll.SensorImage> WeatherStationList = new
List<WeatherlinkDll.SensorImage>();
}
}
}


File WeatherStation.cs

namespace WeatherStation
{
public partial class formWeatherStation : Form
{

public void InitializeWeatherStation()
{
DataManipulation test = new DataManipulation();
test.InitializeWeatherStationList();
}

private void buttonTest_Click(object sender, EventArgs e)
{
test.WeatherStationList.Add(WeatherStationImage);
}

This gives error "The name 'test' does not exist in the current
context"

If I leave out the last line it compiles OK and I noticed that as soon
as I step out of InitializeWeatherStation() test is no longer
accessable which ties up with the error message. If I include all the
code in buttonTest_Click it works and the data is added to the list but
the list of course gets created every time. How do I sort this out?

I am new to C# coming from a embedded C background.

Thanks

SurfRat.
 
Of course it is. Test is in the scope of the InitializeWeatherStation()
method. As soon as that method exits, all its variables are popped off the
stack with the method's stack frame and the reference no longer exists.

The solution is to make test an instance variable:

namespace WeatherStation
{

private DataManipulation test = new DataManipulation; // or you could do
this in the constructor

public partial class formWeatherStation : Form
{

public void InitializeWeatherStation()
{
test.InitializeWeatherStationList();
}

private void buttonTest_Click(object sender, EventArgs e)
{
test.WeatherStationList.Add(WeatherStationImage);
}

The variable test is now in the scope of the class, so you can access it in
any method of the class.

HTH


Peter
 
Hi,

The keyword private generates an error when used as follows:
private DataManipulation test = new DataManipulation;

Am I missing something?

Thanks

SurfRat.
 
Move the declaration inside the class and add some brackets:

public partial class formWeatherStation : Form
{
private DataManipulation test = new DataManipulation();

....

Marc
 
Namespaces can't have variables - only classes.

You need something like a WeatherStation class. Sorry, I missed that when I
replied before.

This isn't meant to be a nasty question... but do you understand the
concept of classes and the like: because if you don't you're going to be in
trouble with .NET.

If this is part of the problem, you're welcome to write to me privately and
I'll try to help. No charge :)

(pbradley [at] uwic [dot] ac [dot] uk)



Peter
 
Back
Top