473,402 Members | 2,046 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Scope of objects across seperate source files

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.

Jan 17 '07 #1
7 1401
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

su******@hotmail.com wrote:
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.
Jan 17 '07 #2
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

<su******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
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.

Jan 17 '07 #3
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.SensorImageWeatherStationList = 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.
Peter Bradley wrote:
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

<su******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
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.
Jan 18 '07 #4
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
<su******@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
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.SensorImageWeatherStationList = 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.
Peter Bradley wrote:
>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

<su******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googleg roups.com...
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.

Jan 18 '07 #5
Hi,

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

Am I missing something?

Thanks

SurfRat.

Peter Bradley wrote:
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
<su******@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
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.SensorImageWeatherStationList = 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.
Peter Bradley wrote:
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

<su******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
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.
Jan 22 '07 #6
Move the declaration inside the class and add some brackets:

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

....

Marc
Jan 22 '07 #7
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
<su******@hotmail.comwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hi,

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

Am I missing something?

Thanks

SurfRat.

Peter Bradley wrote:
>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
<su******@hotmail.comwrote in message
news:11**********************@s34g2000cwa.googleg roups.com...
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.SensorImageWeatherStationList = 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.
Peter Bradley wrote:
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

<su******@hotmail.comwrote in message
news:11**********************@l53g2000cwa.googleg roups.com...
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.


Jan 22 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Bryan Martin | last post by:
I have a object that is created in a seperate domain which needs to be passed back to the parent class. Because this object is created in a seperate domain if I try to pass the object back to the...
9
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. ...
3
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2...
5
by: pembed2003 | last post by:
Hi all, I am reading the book "C How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
4
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
7
by: moondaddy | last post by:
I want to create a public enum that can be used throughout a project. I created an enum like this in a module: Public Enum ParentType Project = 0 Stage = 1 VIP = 2 Func = 3 Equipment = 4...
5
by: rick | last post by:
I'm working on a disassembler for a school project. Things work but I get compiler warnings as follows: cc -W -Wall -pedantic -ansi main.c defs.h:44: warning: 'blah' defined but not used blah...
1
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.