Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to call a string value from a method within another class?

How to call a string value from a method within another class?

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aghosh Babu
    wrote on last edited by
    #1

    i have 2 classes, i want to call a string from one method within a class to another class.

    class SMSManager
    {
    public string abc;
    public void testMethod(string MPN, string RPN, string text)
    {
    SMSModem SMSMdm = new SMSModem();

            MessageBox.Show("RPN" + RPN);
    

    abc = RPN; //this isnt wrking

    // HERE I HAVE A STRING CALLED RPN WHICH I WANT TO CALL INTO ANOTHER CLASS. THE RPN VALUE SHOULD
    //BE THE SAME AS WHEN THE CODE EXECUTES THE ABOVE MESSAGEBOX CODE.

            int x = myArray.Length / 5;
           
            for (int i = 0; i < x; i++)
            {
                string a = myArray\[i, 0\];
                if (a == MPN)
                {
                    MessageBox.Show("MODEM NUMBER :"+MPN);
                    SMSModem SMSM = new SMSModem();
                    SMSM.SendSms();
                }
               
            }
             
           
        }
    

    class SMSModem
    {
    public bool SendSms()
    {
    try
    {
    SMSManager MGR = new SMSManager();
    string num = MGR.abc; //This is not retreiving the value!

    //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
    MessageBox.Show("RECEIRE" +num);

                serialPort1.WriteLine("AT+CMGF=1;+CSCA=\\"+6598540020\\";+CMGS=\\"+65" + num + "\\"\\r\\n");
                System.Threading.Thread.Sleep(40);
                serialPort1.Write("Test message from coded program");
    
                char\[\] arr = new char\[1\];
    
                arr\[0\] = (char)26; //ascii value of Ctrl-Z
    
                serialPort1.Write(arr, 0, 1);
    
                System.Threading.Thread.Sleep(3000);
                string data = serialPort1.ReadExisting();
    
                MessageBox.Show(data);
    
                if (data.IndexOf("OK") != -1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
        }
       
    }
    
    K D 2 Replies Last reply
    0
    • A Aghosh Babu

      i have 2 classes, i want to call a string from one method within a class to another class.

      class SMSManager
      {
      public string abc;
      public void testMethod(string MPN, string RPN, string text)
      {
      SMSModem SMSMdm = new SMSModem();

              MessageBox.Show("RPN" + RPN);
      

      abc = RPN; //this isnt wrking

      // HERE I HAVE A STRING CALLED RPN WHICH I WANT TO CALL INTO ANOTHER CLASS. THE RPN VALUE SHOULD
      //BE THE SAME AS WHEN THE CODE EXECUTES THE ABOVE MESSAGEBOX CODE.

              int x = myArray.Length / 5;
             
              for (int i = 0; i < x; i++)
              {
                  string a = myArray\[i, 0\];
                  if (a == MPN)
                  {
                      MessageBox.Show("MODEM NUMBER :"+MPN);
                      SMSModem SMSM = new SMSModem();
                      SMSM.SendSms();
                  }
                 
              }
               
             
          }
      

      class SMSModem
      {
      public bool SendSms()
      {
      try
      {
      SMSManager MGR = new SMSManager();
      string num = MGR.abc; //This is not retreiving the value!

      //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
      MessageBox.Show("RECEIRE" +num);

                  serialPort1.WriteLine("AT+CMGF=1;+CSCA=\\"+6598540020\\";+CMGS=\\"+65" + num + "\\"\\r\\n");
                  System.Threading.Thread.Sleep(40);
                  serialPort1.Write("Test message from coded program");
      
                  char\[\] arr = new char\[1\];
      
                  arr\[0\] = (char)26; //ascii value of Ctrl-Z
      
                  serialPort1.Write(arr, 0, 1);
      
                  System.Threading.Thread.Sleep(3000);
                  string data = serialPort1.ReadExisting();
      
                  MessageBox.Show(data);
      
                  if (data.IndexOf("OK") != -1)
                  {
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }
              catch
              {
                  return false;
              }
          }
         
      }
      
      K Offline
      K Offline
      Karmendra Suthar
      wrote on last edited by
      #2

      MSManager MGR = new SMSManager();
      string num = MGR.abc; //This is not retreiving the value!
      //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
      MessageBox.Show("RECEIRE" +num);

      string num = MGR.abc; before you can do this you need to call MGR.testMethod which actually does abc = RPN; Alternatively You can set the abc in a constructor. Regards, Karmendra

      A 1 Reply Last reply
      0
      • A Aghosh Babu

        i have 2 classes, i want to call a string from one method within a class to another class.

        class SMSManager
        {
        public string abc;
        public void testMethod(string MPN, string RPN, string text)
        {
        SMSModem SMSMdm = new SMSModem();

                MessageBox.Show("RPN" + RPN);
        

        abc = RPN; //this isnt wrking

        // HERE I HAVE A STRING CALLED RPN WHICH I WANT TO CALL INTO ANOTHER CLASS. THE RPN VALUE SHOULD
        //BE THE SAME AS WHEN THE CODE EXECUTES THE ABOVE MESSAGEBOX CODE.

                int x = myArray.Length / 5;
               
                for (int i = 0; i < x; i++)
                {
                    string a = myArray\[i, 0\];
                    if (a == MPN)
                    {
                        MessageBox.Show("MODEM NUMBER :"+MPN);
                        SMSModem SMSM = new SMSModem();
                        SMSM.SendSms();
                    }
                   
                }
                 
               
            }
        

        class SMSModem
        {
        public bool SendSms()
        {
        try
        {
        SMSManager MGR = new SMSManager();
        string num = MGR.abc; //This is not retreiving the value!

        //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
        MessageBox.Show("RECEIRE" +num);

                    serialPort1.WriteLine("AT+CMGF=1;+CSCA=\\"+6598540020\\";+CMGS=\\"+65" + num + "\\"\\r\\n");
                    System.Threading.Thread.Sleep(40);
                    serialPort1.Write("Test message from coded program");
        
                    char\[\] arr = new char\[1\];
        
                    arr\[0\] = (char)26; //ascii value of Ctrl-Z
        
                    serialPort1.Write(arr, 0, 1);
        
                    System.Threading.Thread.Sleep(3000);
                    string data = serialPort1.ReadExisting();
        
                    MessageBox.Show(data);
        
                    if (data.IndexOf("OK") != -1)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
           
        }
        
        D Offline
        D Offline
        Dragonfly_Lee
        wrote on last edited by
        #3

        Aghosh Babu wrote:

        abc = RPN; //this isnt wrking

        What do you mean it did not work? It is just a simple assign-op. Maybe you can debug it.

        Aghosh Babu wrote:

        string num = MGR.abc; //This is not retreiving the value!//I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"

        According to the cdoe, the value should be null.

        :) I Love KongFu~

        1 Reply Last reply
        0
        • K Karmendra Suthar

          MSManager MGR = new SMSManager();
          string num = MGR.abc; //This is not retreiving the value!
          //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
          MessageBox.Show("RECEIRE" +num);

          string num = MGR.abc; before you can do this you need to call MGR.testMethod which actually does abc = RPN; Alternatively You can set the abc in a constructor. Regards, Karmendra

          A Offline
          A Offline
          Aghosh Babu
          wrote on last edited by
          #4

          i call the MGR.testMethod in the button event in another form as:

          public partial class Form1 : Form
          {
          public Form1()
          {
          InitializeComponent();
          }
          public void sampleArray()
          {

              }
          
             
          
              
          
              private void button1\_Click(object sender, EventArgs e)
              {
                  SMSManager sm = new SMSManager();
                  
                  sm.testMethod("82083427", "1122887779", "testmessagetxt");
              }
          }
          

          how can i retreive the value then??

          K 1 Reply Last reply
          0
          • A Aghosh Babu

            i call the MGR.testMethod in the button event in another form as:

            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            }
            public void sampleArray()
            {

                }
            
               
            
                
            
                private void button1\_Click(object sender, EventArgs e)
                {
                    SMSManager sm = new SMSManager();
                    
                    sm.testMethod("82083427", "1122887779", "testmessagetxt");
                }
            }
            

            how can i retreive the value then??

            K Offline
            K Offline
            Karmendra Suthar
            wrote on last edited by
            #5

            Think you need to re-visit OOPS concepts. In button click event you have created another object sm and you have set sm.abc by running testMethod. and you have never run SMSMdm.testMethod to set SMSMdm.abc, it wil give error also because you have never initialized SMSMdm.abc.

            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            SMSManager sm = new SMSManager();
            }
            public void sampleArray()
            {

                }
            
                private void button1\_Click(object sender, EventArgs e)
                {            
                    sm.testMethod("82083427", "1122887779", "testmessagetxt");
                }
            }
            

            class SMSManager
            {
            public string abc="";
            public void testMethod(string MPN, string RPN, string text)
            {
            SMSModem SMSMdm = new SMSModem();

                    MessageBox.Show("RPN" + RPN);
                    abc = RPN;  //this isnt wrking
            
                    int x = myArray.Length / 5;
                   
                    for (int i = 0; i < x; i++)
                    {
                        string a = myArray\[i, 0\];
                        if (a == MPN)
                        {
                            MessageBox.Show("MODEM NUMBER :"+MPN);
                            SMSModem SMSM = new SMSModem();
                            SMSM.SendSms();
                        }
                    }
                }
            

            class SMSModem
            {
            public bool SendSms()
            {
            try
            {
            //this is not required now
            //SMSManager MGR = new SMSManager();
            //string num = MGR.abc; //This is not retreiving the value!

                        //I WANT THAT VALUE TO BE CALLED HERE AND ASSIGN IT TO "num"
            
                        string num = **sm**.abc;
                        MessageBox.Show("RECEIRE" +num);
                 serialPort1.WriteLine("AT+CMGF=1;+CSCA=\\"+6598540020\\";+CMGS=\\"+65" + num + "\\"\\r\\n");
                        System.Threading.Thread.Sleep(40);
                        serialPort1.Write("Test message from coded program");
            
                        char\[\] arr = new char\[1\];
            
                        arr\[0\] = (char)26; //ascii value of Ctrl-Z
            
                        serialPort1.Write(arr, 0, 1);
            
                        System.Threading.Thread.Sleep(3000);
                        string data = serialPort1.ReadExisting();
            
                        MessageBox.Show(data);
            
                        if (data.IndexOf("OK") != -1)
                        {
                            return true;
                        }
                        else
            
            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups