How to call a string value from a method within another class?
-
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; } } }
-
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; } } }
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
-
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; } } }
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~
-
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
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??
-
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??
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