473,320 Members | 1,691 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,320 software developers and data experts.

Unable to access a variable from another class - please help!

I have a windows application called "WindowsApplication1". I have a
variable of tyoe DataSet called myDataSet as shown below:
namespace WindowsApplication1
{

public class Form1 : System.Windows.Forms.Form
{

System.Data.DataSet myDataSet;

}

I am trying to access this variable from a class called
DisplayQueryResults (inside the same namespace). Code is below:

namespace WindowsApplication1
{
/// <summary>
/// Summary description for DisplayQueryResults.
/// </summary>
public class DisplayQueryResults
{
//CONSTRUCTOR
public DisplayQueryResults()
{

WindowsApplication1.Form1. = new DataSet();

Problems is though after the "Form1." bit of the above I am not
presented with the option myDataSet! I don't know why. Can anyone help
me please? I know what your thinking: why don't I just declare
myDataSet somewhere inside DiplayQueryResults? I can't - it's used by
other classes so I have to put it in neutral territory so to speak. Do
you see my problem?
Any comments/suggestions/code samples - much appreciated.

Cheers,
Al
The frustrated one.
Nov 16 '05 #1
4 11894
On 8 Feb 2005 04:41:51 -0800, Al Murphy wrote:
I have a windows application called "WindowsApplication1". I have a
variable of tyoe DataSet called myDataSet as shown below:

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
System.Data.DataSet myDataSet;
}

I am trying to access this variable from a class called
DisplayQueryResults (inside the same namespace). Code is below:

namespace WindowsApplication1
{
public class DisplayQueryResults
{
//CONSTRUCTOR
public DisplayQueryResults()
{
WindowsApplication1.Form1. = new DataSet();

Problems is though after the "Form1." bit of the above I am not
presented with the option myDataSet! I don't know why. Can anyone help
me please? I know what your thinking: why don't I just declare
myDataSet somewhere inside DiplayQueryResults? I can't - it's used by
other classes so I have to put it in neutral territory so to speak. Do
you see my problem?
Any comments/suggestions/code samples - much appreciated.


The short answer is that class members are by default private and therefore
inaccessible outside of the class. You need to make the visibility either
public or internal, depending on whether DisplayQueryResults is in the same
assembly as Class1. There appear to larger design issues here, but you
show an obvious lack of understanding of basic C# and OO concepts and don't
say enough about the overall problem you are trying to solve, to allow
comment on that.
--
Tom Porterfield
Nov 16 '05 #2
You need to access this dataset through an instance of the form1 class.
i.e. You need to do:
WindowsApplication1.Form1 myform = WindowsApplication1.Form1();
After that you could use myform.DataSet1;

Another option is to make the dataset a static field that doesn't depend on
the instance of the form

i.e the defintion of the form1 would be
public class Form1 : System.Windows.Forms.Form
{

static System.Data.DataSet myDataSet;

}
Then use the dataset as you did. (However, not that inthis case the dataset
is associated with all the members of the class not a specific member)
Regards
Mohamed El Ashmawy
MEA Developer Support Center
ITWorx on behalf of Microsoft EMEA GTSC

Nov 16 '05 #3
What Tom means is that it's generally not considered good programming
practise to give one class access to the internal workings of another class.
Public/internal fields are the worst offenders in this respect. Can I
suggest you look up "encapsulation" as it refers to object-orientation. At
the very least in your scenario, I'd wrap the myDataSet field into a
property to allow external access.

"Tom Porterfield" <tp******@mvps.org> wrote in message
news:1h***************@tpportermvps.org...
On 8 Feb 2005 04:41:51 -0800, Al Murphy wrote:
I have a windows application called "WindowsApplication1". I have a
variable of tyoe DataSet called myDataSet as shown below:

namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
System.Data.DataSet myDataSet;
}

I am trying to access this variable from a class called
DisplayQueryResults (inside the same namespace). Code is below:

namespace WindowsApplication1
{
public class DisplayQueryResults
{
//CONSTRUCTOR
public DisplayQueryResults()
{
WindowsApplication1.Form1. = new DataSet();

Problems is though after the "Form1." bit of the above I am not
presented with the option myDataSet! I don't know why. Can anyone help
me please? I know what your thinking: why don't I just declare
myDataSet somewhere inside DiplayQueryResults? I can't - it's used by
other classes so I have to put it in neutral territory so to speak. Do
you see my problem?
Any comments/suggestions/code samples - much appreciated.


The short answer is that class members are by default private and
therefore
inaccessible outside of the class. You need to make the visibility either
public or internal, depending on whether DisplayQueryResults is in the
same
assembly as Class1. There appear to larger design issues here, but you
show an obvious lack of understanding of basic C# and OO concepts and
don't
say enough about the overall problem you are trying to solve, to allow
comment on that.
--
Tom Porterfield

Nov 16 '05 #4
WRH
I don't know if this helps or not but if you declare a
new instance of Form1, eg

public class DisplayQueryResults
{
//CONSTRUCTOR
public DisplayQueryResults()
{
WindowsApplication1.Form1 frm. = new Form1();
frm.myDataSet = new DataSet();

}

-----------------------------------------------------------------------------------------------------------------

"Al Murphy" <al*****@altavista.com> wrote in message
news:a2**************************@posting.google.c om...
I have a windows application called "WindowsApplication1". I have a
variable of tyoe DataSet called myDataSet as shown below:
namespace WindowsApplication1
{

public class Form1 : System.Windows.Forms.Form
{

System.Data.DataSet myDataSet;

}

I am trying to access this variable from a class called
DisplayQueryResults (inside the same namespace). Code is below:

namespace WindowsApplication1
{
/// <summary>
/// Summary description for DisplayQueryResults.
/// </summary>
public class DisplayQueryResults
{
//CONSTRUCTOR
public DisplayQueryResults()
{

WindowsApplication1.Form1. = new DataSet();

Problems is though after the "Form1." bit of the above I am not
presented with the option myDataSet! I don't know why. Can anyone help
me please? I know what your thinking: why don't I just declare
myDataSet somewhere inside DiplayQueryResults? I can't - it's used by
other classes so I have to put it in neutral territory so to speak. Do
you see my problem?
Any comments/suggestions/code samples - much appreciated.

Cheers,
Al
The frustrated one.

Nov 16 '05 #5

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

Similar topics

3
by: nikolai.onken | last post by:
Hello, I got following problem trying to inherit a variable from another class class Core { public $obj; function initialize($obj) {
1
by: Hal Vaughan | last post by:
I have a panel in a GUI that has three components: A JTextArea, a JCheckBox and a JButton. When the button is pressed, I want to check the value of the checkbox and get the text of the textarea...
2
by: Eugene | last post by:
Hi, I got a singleton class. Then in another class (CA), i would declare a variable with this singleton class and get a reference to it. I would have another class (IA1) that would inherit from CA...
2
by: paul meaney | last post by:
All, myself and another developer have been staring blankly at a screen for the past 48 hours and are wondering just what stunningly obvious thing we are missing. We are trying to load up 2...
5
by: Jeff Cobelli | last post by:
I am trying to include two classes as members of another class in a webservice. The definitions look basically like this: Public Class clsLender Public ID As String Public Name As String End...
3
by: drummond.ian | last post by:
Hello Everyone, This problem's been causing me a lot of trouble and I'm hoping somebody can help me out!! I have a dialog-based MFC application in visual studio 2003. I want to call a...
16
by: Mike | last post by:
Hi, I have a form with some controls, and a different class that needs to modify some control properties at run time. Hoy can I reference the from so I have access to its controls and...
3
by: kalpana84 | last post by:
hi, Am doing coding in c#,... actually i am creating four different classes in four different . And all classes has the same namespace. What to be done If i want invoke the method of one class...
1
by: Elisbeth | last post by:
I have a question about using lists with in another class for example i have a class class Position { public: Position(int a, int b){ x = a; y = b;
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.