newbie question: read form properties from another class

R

rno

Hi,

I am sorry for this n00b question, but I'm stumped.

I created a form application (using C#.NET Express). For now, it is a
simple, singe form, that contains 1 button and 1 textbox.

The button calls a method in another class. My question: how do I read
the forms' textbox text from that class?

My problem seems to be getting a proper reference to the form in my
class. My background is VB6, where this was straightforward.

I tried the following:
-make the textbox public,
-create a public property in the form, that returns the textbox text,
-passing the form object ('this') to a method in the called (static)
class
-passing the form object ('this') to the class' constructor (non-static
class)

But I can't seem to get to the form's textbox text, as Intellisense and
the debugger keep telling me.

I feel like I am going about this the wrong way, and that I am
misunderstanding some key language concept.

Would really appreciate any pointers to get me going.

tia
arno
 
P

Peter Duniho

rno said:
Hi,

I am sorry for this n00b question, but I'm stumped.

I created a form application (using C#.NET Express). For now, it is a
simple, singe form, that contains 1 button and 1 textbox.

The button calls a method in another class. My question: how do I read
the forms' textbox text from that class?

You need to do two things:

– Ensure that the other class has a reference to the form class instance

– Ensure that the form class itself has a property that encapsulates
the text property you want access to
My problem seems to be getting a proper reference to the form in my
class. My background is VB6, where this was straightforward.

To some extent, it was straightforward only because in VB6, the language
provided default instances and unrestricted access to types. .NET is a
lot strictly, and C# more strict than VB.NET even.
I tried the following:
-make the textbox public,

That could work, if everything else is correct, but it's a REALLY BAD
IDEA. Expose "interface", not implementation.
-create a public property in the form, that returns the textbox text,

This is part of a complete solution.
-passing the form object ('this') to a method in the called (static)
class

This could be part of a complete solution.
-passing the form object ('this') to the class' constructor (non-static
class)

This also could be part of a complete solution.
But I can't seem to get to the form's textbox text, as Intellisense and
the debugger keep telling me.

Well, you seem to be on the right track. So obviously, you just haven't
gotten quite the right combination of elements together.

But without a concise-but-complete code example to _show_ us what
doesn't work, it's not possible to explain what's wrong.

What I _can_ do is offer a short, incomplete code example illustrating
the basic technique you need:

class Form1 : Form
{
public string TextBox1Text
{
get { return textBox1.Text; }
set { textBox1.Text = value; }
}

private void SomeMethod()
{
OtherClass oc = new OtherClass(this);

oc.SomeMethod();
}

// Normally in the Form1.Designer.cs file; here for
// illustration purposes only:
private TextBox textBox1;
}

class OtherClass
{
private Form1 _form1;

public OtherClass(Form1 form1)
{
_form1 = form1;
}

public SomeMethod()
{
string str;

// Retrieve the text:
str = _form1.TextBox1Text;

// Or set the text:
_form1.TextBox1Text = "Set from OtherClass.SomeMethod()";
}
}

Hopefully, when you compare the above to whatever code you actually
tried, you'll see one or more things that are different in a significant
way, and you can see what the correct approach is.

Now, one thing to keep in mind is that there may be other reasons to
approach this problem differently. It should be unusual to have to some
helper class that actually knows the type of the caller, as would be
required in the above example.

But, without knowing more about the broader problem you're trying to
solve, I can't really offer advice about how to avoid the above pattern
completely. So for now, it will have to do. :)

Pete
 
K

kndg

I created a form application (using C#.NET Express). For now, it is a
simple, singe form, that contains 1 button and 1 textbox.

The button calls a method in another class. My question: how do I read
the forms' textbox text from that class?

In addition to what Peter has wrote, if nothing in the AnotherClass that
suggest the need of a reference to your Form1 object, you can just have
that method to accept a string argument.

class AnotherClass
{
...

void SomeMethod(string text)
{
// do something
}
}

So, when you have the instance of AnotherClass (for example:
anotherClassInstance), you can just pass the textBox1.Text to the
SomeMethod() argument.

anotherClassInstance.SomeMethod(textBox1.Text);

Regards.
 
M

Michael C

rno said:
Would really appreciate any pointers to get me going.

I strongly agree with kndg's response. It's about as simple as it gets and
you don't need to refer to the textbox or form inside your class. You might
find it a pita but C# is actually doing what it is designed to do here. VB6
let you do it the wrong way where C# at least directs you towards to correct
method. Think of your class as a block of code that could be used by any
object, not just the one form. By placing references back to the textbox
into that class you make the class restricted to that form only and stops it
from being reused.

Michael
 
R

rno

Thank you all for your valuable input. I am sure I will get it working
now somehow!

arno
 
R

rno

Got it. I was so close :) I failed to properly get a reference to the
form, because I did not realise that it had to be of type 'Form1', not
Form. I suppose that's [bleep] obvious, just not to me :) I'm
learning...

tx again
arno
 

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