How to access parameter of a method (declared in the method itself) from another method?
I want to access the string parameter declared in a method from another method. i.e., I made a string parameter called string survey in SaveLocal method. Now I want this to be accessed by the SendEmail method so I can get the string survey as the mail body (mail.Body).
//code void SaveLocal(string survey, int id) { //save to disk string fileName = "results" + id.ToString("D4"); print("Filename: " + fileName); Debug.Log(Application.persistentDataPath);
string filePath = Application.persistentDataPath + "/"+fileName + ".json";
//string path = Application.persistentDataPath;
byte[] JsonStringBytes = Encoding.UTF8.GetBytes(survey);
File.WriteAllBytes(filePath, JsonStringBytes);
InfoText.text += "\n\n" + MainMenuBehaviour.CurrentDictionary.GetKeyValue("saved_locally", false);
}
//code 2
public void SendEmail() { MailMessage mail = new MailMessage();
mail.From = new MailAddress("sender@gmail.com"); mail.To.Add("reciever1@gmail.com"); mail.To.Add("reciever2@gmail.com"); mail.To.Add("reciever3@gmail.com"); mail.Subject = "Test Mail"; mail.Body = "This is for testing SMTP mail from GMAIL"; //MAIL BODY
SmtpClient smtpServer = new SmtpClient("smtp.gmail.com"); smtpServer.Port = 587; smtpServer.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "senderpassword") as ICredentialsByHost; smtpServer.EnableSsl = true; ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; smtpServer.Send(mail); Debug.Log("success");
Answer by xxmariofer · May 14, 2019 at 11:01 AM
you cant, just declare it outside. declare the variable outside the method and just assign it inside, local vars memory get errased even if you could access method variables
Pretty sure he is not trying to pass the reference of a var but access a local method var, and accessing a mdthod local var is not posible
I think this is one of those "How do I become a dual Iranian-American citizen?" Because they want to buy Safron and someone told them only Iranian citizens can buy it. I think they're asking how a function can compute something and tell the rest of the program about it. I think.
I am storing all the answers to a questionnaire in the string survey. How do I get all the data in the string and print in the mail body so I can Email it to the recipients?
Create a var outside the method
private void string savedSurvey;
And inside the methof initialize it
savedSurvey = survey;
Now you xan access saved survey from anywhere inside the class