Visual studio Csharp Pulbic string

Discussion in 'Programmer's Corner' started by Dritech, Mar 2, 2014.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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?
     
    spinnaker likes this.
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    Do you think it might make it a little easier if you posted the relevant code? Just a thought.
     
  3. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    potentially, the namespace will be different between forms?
    Also, have you tried without static?
     
  4. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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:

    Code ( (Unknown Language)):
    1. public partial class Form_Home : Form
    2.     {
    3.  
    4.         public static string test;  // initialize string
    .........

    Code ( (Unknown Language)):
    1.  
    2. string Send_code = "a";
    3. serial(test, Send_code);
    Code used in form 2:

    Code ( (Unknown Language)):
    1. private void Use_COMport_Click(object sender, EventArgs e)
    2.         {
    3.             test = COMport_to_use.Text.ToString();  
    4.             this.Close();   //Close this form
    5.         }
    6.  
    By the way, I am using this code to interface (or try to) Visual Studio with the PIC microcontroller.
     
  5. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi, I removed the static and the same error is showing.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    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.
     
  7. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    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
     
    Dritech likes this.
  8. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    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.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    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.
     
  10. chrisw1990

    Active Member

    Oct 22, 2011
    543
    41
    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).
     
Loading...