denotes field where class expected and other problem

J

jm

I have snippets from Form1 and a class named netclass.cs (just an
arbitrary name.)

Form1 has a textBox1 on it. I want to reference it in the class. I
put:

static void Main()
{

Application.Run(new Form1());
netclass nc = new netclass(this);
netclass.StartClient();
}

In the netclass class:

public class netclass
{
static Form1 _Form;

public netclass(Form1 form)
{
_Form = form;
//
// TODO: Add constructor logic here
//
}

I get the error:

Keyword this is not valid in a static property, static method, or
static field initializer
(and it is talking about the keyword "this")

If I change the "static Form1 _Form;" to just "Form1 _Form;" I get
past that error, but then I get another error when I try an use the
_Form textBox1:

string temp = _Form.textBox1.Text; //in netclass.cs just want to get
the text

I can't figure out what to put to make them both go away.

Thank you for any help.
 
A

Angel J. Hernández

Hi there...

1-. There's no way you can reference the this pointer within a static method
(ur doing it from within Main)

2-. The constructor expects to receive an object (ur passing it) then u
require to store that object into a member (it can't be static)
and ur doing it... Change the member definition from...

static Form1 _Form;

to this...

private Form1 _Form;

Regards,
 

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