Newbie question

  • Thread starter Thread starter Scott Starker
  • Start date Start date
S

Scott Starker

I have spent hours looking for the answer but I don't think I can phrase it
right to the search engines out there. So, I guess I'll have to try out
people and give it a shot!

If I have 2 .cs files and I want to share the same class (in a third .cs
file) with my 2 .cs files how do I do this? I.e., I have a project with 6 cs
files. In the Form1.cs file I have:
namespace TECData
{
public partial class Form1 : Form
{
ButtonArray MyButtonArray;
public Form1()
{
InitializeComponent();
MyButtonArray = new ButtonArray(this, "Arial");
for (int i = 1; i <= 0xff - 0x21; i++)
{
MyButtonArray.AddNewButton("Arial");
}
....

And then in TEC.cs I have:
namespace TECData
{
public partial class TEC : Form
{
public TEC()
{
InitializeComponent();
EncodingName();
}
private void EncodingName()
{
for (int x = 0; x <= 0xff - 0x21; x++)
{
if (MyButtonArray.CharArray(x))
{
....

MyButtonArray is built in Forms1.cs. That's fine. But now in TEC.cs the
error is "The name 'MyButtonArray' does not exist in the current context". I
have tried a bunch of ways but can anyone help me? (The CharArray(x) is a
bool method.)

Scott
 
MyButtonArray is a private member of the Form1 class. You will not be able
to access it from the TEC class for this reason.

In order to access it you will need to have an instance of the Form1 class
available in the TEC class and then either make it public or add in a
property to get/set it.

Hope this helps!
 
Also. what are you trying to accomplish.. From what it sounds you may not be
going about it correctly because your example is trying to share "member
variables" and not "classes" which is what you said you wanted to do?
 
That's right. I'm trying to share a member variable. How should I do this?
 
Well there are a few things that you will need to do.

1. You should add a property to the Form1 class that will allow you to
get/set the MyButtonArray variable. Something like this maybe?

public ButtonArray MyButton
{
get { return MyButtonArray; }
set { MybuttonArray = value; }
}

2. The TEC class will need to have a reference to an instance of the Form1
class. Maybe something like this?

private Form1 TheForm; //Member variable inside of TEC class that will be
set to an instance of the Form1 class.
public void SetForm1Instance(Form1 aForm)
{
TheForm = aForm;
}

3. Some other class needs to create an instance of both classes and then
pass in the Form1 instance to the TEC classes SetForm1Instance() method.
Probably the main application class or something similar? This is what will
provide the "link" between the classes.

4. You can then access the member variable by doing the following inside of
the TEC class.

public void SomeFunction()
{
if (TheForm.MyButton.CharArray(x))....
...
...
...
}

What you are wanting to do is pretty normal you just need to jump thru some
hoops to get it done. Just keep in mind that you cannot reference a
variable that is in another class without having an instance of that class
available. The one exception to this is static variables but that is a
different story!

Hope this helps.
 
Oh. I see. The CharArray (which is a member of ButtonArray class) of is not
directly available from two classes. But, TEC class is instatiated on Form1
class which is where the ButtonArray class is instantiated from and has the
member CharArray.

Well, I have one problem left: "Inconsistent accessibility: property type
'TECData.ButtonArray' is less accessible than property
'TECData.Form1.MyButton'"

In Form1.cs I have:
namespace TECData
{
public partial class Form1 : Form
{
// Declare a new ButtonArray object. // The control array was created from
http://msdn.microsoft.com/library/d...ngcontrolarraysinvisualbasicnetvisualcnet.asp
ButtonArray MyButtonArray; // It's called "Creating Control Arrays in Visual
Basic .NET and Visual C# .NET"
TEC myTEC;
public Form1()
{
InitializeComponent();
TEC.SetForm1Instance(Form1);
// The first "MyButtonArray"
MyButtonArray = new ButtonArray(this, "Arial");
....
public ButtonArray MyButton
<-- Pointed to by the debugger
{
get { return MyButtonArray; }
set { MyButtonArray = value; }
}

And in TEC.cs I have:
namespace TECData
{
public partial class TEC : Form
{
private Form1 TheForm; //Member variable inside of TEC class that will be
set to an instance of the Form1 class.
....
// build a string with the font characters in place
for (int x = 0; x <= 0xff - 0x21; x++)
{
if (TheForm.MyButton.CharArray(x))
{
....
public void SetForm1Instance(Form1 aForm)

{

TheForm = aForm;

}

....


Where is the problem?
 
hmmm..

Dont see anything wrong right away.

One though is how is TECData.ButtonArray class declared (public, private,
etc)? The error seems to be saying that that class is less accessible (maybe
marked private) then the MyButton property (which is marked public).
 
What you are wanting to do is pretty normal you just need to jump thru
The program worked when I had CharArray as a static variable but I wanted it
to add to the ButtonArray class because I didn't want there to be only one
ButtonArray class per program!

In your first message I thought you said that MyButtonArray is a private
member of the Form1 class and that's the way it's got to be. E.i. there is
no way to make the class public. Can there be?
 
Sorry.. What I mean is how is the ButtonArray class declared.

Ex.)

public class ButtonArray
{

}

or

private class ButtonArray
{

}

that may be the problem? Not sure.
 
Ah. I got it. I didn't give you all the code. I modified
"TEC.SetForm1Instance(Form1);" to "myTEC.SetForm1Instance(Form1);" and
changed "class ButtonArray : System.Collections.CollectionBase" to "public
class ButtonArray : System.Collections.CollectionBase" (which is the
"public" that you said) and I did some changes to my button_Click function
which calls the TEC.cs file.

Anyway, thanks for your help!

Scott
 
Scott said:
Oh. I see. The CharArray (which is a member of ButtonArray class) of is not
directly available from two classes. But, TEC class is instatiated on Form1
class which is where the ButtonArray class is instantiated from and has the
member CharArray.

Well, I have one problem left: "Inconsistent accessibility: property type
'TECData.ButtonArray' is less accessible than property
'TECData.Form1.MyButton'"

That's easy. You've declared the ButtonArray type to be internal to the
assembly (I assume - I didn't see the declaration in the code you
posted) and yet the property is public. You can't do that - it's like
publishing a web page in a new markup language but not telling people
what the details of the markup language are.

Either make ButtonArray public, or make the property internal.

Jon
 
Bummer. After following all of your advice I still get the member variable at
its default. The value for TheForm is the same thing as Form1 with member
variables set to their defaults. Which means that aForm is a new class (Form1
is the other one). Every time any button is click inside Form1,
MyButtomArray.CharArray gets set (or reset) (bool). Once this is done TEC
gets executed. Now, I want to access MyButtonArray.CharArray to get the bool
values in TEC. Is there anyway to do this?
 

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

Back
Top