-2

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

user9405863
  • 1,506
  • 1
  • 11
  • 16
c.pacheco
  • 7
  • 1
  • The error is self-explanatory. In `Storeinfo(contact:contact,address: address,number: number)`, you're referencing variables that do not exist in the context of `StartProgram` method - they are neither local variables, nor parameters of `StartProgram`, nor members of the encapsulating class (I guess so). – Eduard Malakhov Mar 18 '18 at 09:14

2 Answers2

0

contact,address &number are variables that are created within the scope of the NewContact method. They would never be accessible outside the scope of the method.

In your second example, we are creating the values in NewContact and passing values to StoreInfo by reference.

Ritwik Sen
  • 296
  • 1
  • 13
0

What you're seeing has to do with variable scope.

Those three variables inside NewContact() only exist within that method, and are only accessible from within it. If you want your first code snippet to work, you'll have to return the values.

There's a few ways to attack this. You could just return a Tuple<string,string,string> or a List<string> or whatever you want. Or you could create a class like this:

public class ContactRecord
{
    public string Contact { get; set; }
    public string Address { get; set; }
    public string Number { get; set; }
}

ContactRecord NewContact ()
{
    string contact = SnapsEngine.ReadString("Enter the contact name");
    string address = SnapsEngine.ReadMultiLineString("Enter " + contact + " address");
    string number = SnapsEngine.ReadString("Enter " + contact + " number");

    return new ContactRecord { Contact = contact, Address = address, Number = number };
}

Then in the original method, use the returned values:

if (firstaction == "New Contact")
{
    var contact = NewContact();
    Storeinfo(contact: contact.Contact, address: contact.Address, number: contact.Number);
}
Grant Winney
  • 65,241
  • 13
  • 115
  • 165