Newbie scope questions

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
 
J

Jon Skeet

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.
 
G

Geoffrey Collier

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
 
C

Chris Capel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top