Visual studio Csharp Pulbic string

Thread Starter

Dritech

Joined Sep 21, 2011
907
Hi all,

I am using two forms and will be using a sting named "test" in both forms. To so so, I declared the string as "public static string test;" in form1.

For some reason I am getting the following error:

Error 1 The name 'test' does not exist in the current context
Why is this happening please?
 

spinnaker

Joined Oct 29, 2009
7,830
Hi all,

I am using two forms and will be using a sting named "test" in both forms. To so so, I declared the string as "public static string test;" in form1.

For some reason I am getting the following error:



Why is this happening please?
Do you think it might make it a little easier if you posted the relevant code? Just a thought.
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
Hi. I did not post the code since it is quite long, but below is the part of the code which have to do this this string:

Code in form 1:

Rich (BB code):
public partial class Form_Home : Form
    {

        public static string test;  // initialize string
.........

Rich (BB code):
string Send_code = "a";
serial(test, Send_code);
Code used in form 2:

Rich (BB code):
private void Use_COMport_Click(object sender, EventArgs e)
        {
            test = COMport_to_use.Text.ToString();  
            this.Close();   //Close this form
        }
By the way, I am using this code to interface (or try to) Visual Studio with the PIC microcontroller.
 

spinnaker

Joined Oct 29, 2009
7,830
So where is the error occurring?

What class is private void Use_COMport_Click(object sender, EventArgs e) contained?


And this code:

string Send_code = "a";
serial(test, Send_code)


You really need to provide enough information so people can help you.
 

chrisw1990

Joined Oct 22, 2011
551
have you tried referencing the variable through parent.test ?
as your dialog box, i assume, comes up and takes priority (i.e.: you cannot interact with the originating form.)
you are using two separate name spaces though.
i.e.
form1 uses test, has declaration of test etc.
form 2 can see test if it references form1.

I dont have any code at the moment with multiple dialogs (i hate them :p ) but try referencing it through form1.test or parent.test from the dialog
 

Thread Starter

Dritech

Joined Sep 21, 2011
907
Thanks a lot chrisw1990 :)

Including form1. solved the problem.

Another quick question. If I use 10 buttons on Visual Studio to turn on/off 10 separate LEDs on the PIC, which is the best way to do it?

I was doing it as below:

If button 1 is pressed, send character "a" to the PIC.
If button 2 is pressed, send character "b" to the PIC.
If button 3 is pressed, send character "c" to the PIC.
.......

On the PIC I used if conditions as follows:

If character = a, turn LED 1 on
If character = b, turn LED 2 on
........

Is there another way to do this more efficiently?

Thanks in advance.
 

ErnieM

Joined Apr 24, 2011
8,415
Is there another way to do this more efficiently?
Defining "efficiently" would help making a sensible reply.

10 symbols for 10 commands maps well, but it makes me wonder if you can turn the LEDs off.

The smallest this would fit into would be a 2 byte quantity, where individual bits ma to individual LEDs.
 

chrisw1990

Joined Oct 22, 2011
551
i agree with Ernie, simply for throughput efficiency as 16bits is a lot cheaper than 20bytes.. but it depends how often youre going to be executing this code. your pic can happily sit there with a 20 case switch statement in an interrupt routine when it receives the data if thats all its doing (light load anyway if not all its doing).
 
Top