Newbie scope questions

  • Thread starter Thread starter Geoffrey Collier
  • Start date Start date
G

Geoffrey Collier

I have created a form (form1) with some objects on it. I
then created a new class in the same file that has the
form1 code. It is in the same namespace. I now want to
address the objects on form1, but I can't get the new
class to "see" these objects. I get an
error "Form1.rtxt_output is inaccessible due to its
protection level". (Form1 is public). What gives?
The code is below.

public class testclass
{
//VARIABLES
int i;
int j;
char c;
string s;
float f;
double d;
double[] myarray=new double[10];

private testclass()
{

} //default constructor.
public void display()
{
for(i=0;i<10;++i)
{
//START HERE: I can't figure out
why I can't reference rtxt_output here.

//rtxt_output.Text="hello";
} //for i
}//display
}//testclass
 
Geoffrey Collier said:
I have created a form (form1) with some objects on it. I
then created a new class in the same file that has the
form1 code. It is in the same namespace. I now want to
address the objects on form1, but I can't get the new
class to "see" these objects. I get an
error "Form1.rtxt_output is inaccessible due to its
protection level". (Form1 is public). What gives?

Form1 may be public, but is rtxt_output public? I would suggest that
you don't actually make it public - you provide public (or internal)
properties to access it instead.
 
Well, that raises the question, how do I control the scope of an object
that was created visually? In the InitializeComponent() area I have the
various aloocations, e.g. this.rtxt_output.location=new
System.Drawing.Point(88,72) etc. But how do I set the scope of the
objects on form1? Thanks muchly?
Geoff Collier
 
There's a property in the property editor called Modifiers that you can use
to change the visibility of your variable. But it's easier, sometimes, to
just edit the declaration of the variable that the designer generated. You
can mess with the generated code as much as you want, contrary to the
warnings, and most of your changes will be reflected in the form designer
next time you open it.

Chris
 
Back
Top