recently I started learning C# by reading books and while using methods to build an app contact i got this issue.
I can't understand why the compiler don't recognise the variables of the parameters of the method "Storeinfo" (contact/address/number) if these variables are supposed to be set and used after the method "NewContact" is called. (This method is supposed to set the variables contact,address and number). The mistake which visual studio points is this:
"The name contact doesn't exist in the actual context"
"The name address doesn't exist in the actual context"
"The name number doesn't exist in the actual context"
public void StartProgram()
{
string firstaction;
bool exit = true;
while (exit)
{
firstaction = SnapsEngine.SelectFrom2Buttons("New Contact", "Find Contact");
if (firstaction == "New Contact")
{
NewContact();
Storeinfo(contact:contact,address: address,number: number);
}
}
}
void Storeinfo(string contact,string address,string number)
{
contact = contact.ToLower();
SnapsEngine.SaveStringToLocalStorage(itemName: contact + ":address", itemValue: address);
SnapsEngine.SaveStringToLocalStorage(itemName: number + ":number", itemValue: number);
}
void NewContact ()
{
string contact = SnapsEngine.ReadString("Enter the contact name");
string address = SnapsEngine.ReadMultiLineString("Enter " + contact + " address");
string number = SnapsEngine.ReadString("Enter " + contact + " number");
}
}
but when i rearrange the code like this: (moving the method "Storeinfo" inside the "NewContact" method it works just fine)
public void StartProgram()
{
string firstaction;
bool exit = true;
while (exit)
{
firstaction = SnapsEngine.SelectFrom2Buttons("New Contact", "Find Contact");
if (firstaction == "New Contact")
{
NewContact();
}
}
}
void Storeinfo(string contact,string address,string number)
{
contact = contact.ToLower();
SnapsEngine.SaveStringToLocalStorage(itemName: contact + ":address", itemValue: address);
SnapsEngine.SaveStringToLocalStorage(itemName: number + ":number", itemValue: number);
}
void NewContact ()
{
string contact = SnapsEngine.ReadString("Enter the contact name");
string address = SnapsEngine.ReadMultiLineString("Enter " + contact + " address");
string number = SnapsEngine.ReadString("Enter " + contact + " number");
Storeinfo(contact:contact,address: address,number: number);
}
}
I would be infinitely thankful to whoever is able to explain this
BTW im using a library called "Snapslibrary" that's why it is used repeatedly