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

  • Thread starter Thread starter Al Murphy
  • Start date Start date
A

Al Murphy

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.
 
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.
 
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
 
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.
 
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();

}
 
Back
Top