Answered by:
accessing auto implemented properties

Question
-
Just playing around with auto implemented properties and I ran into a small issue trying to get the accessor from a different method from where it was set. I can get it to work from within the same method, but not outside.
How can this be done?
Still new, but getting better. :)- Edited by David M Morton Friday, September 4, 2009 3:31 PM Formatting.
- Edited by RyanDunn Friday, September 4, 2009 3:39 PM
Friday, September 4, 2009 3:27 PM
Answers
-
You are instantiating a new instance of Customer and not setting the property.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by RyanDunn Friday, September 4, 2009 4:01 PM
Friday, September 4, 2009 3:51 PM -
Your trouble isn't with automatic properties... it's with variable scope, and the fact that setting an instance value of one instance of a type doesn't ensure that value is available in all situations. Let's look at your code:
public class Customer
{
public string Name { get; set; }
}
public class AutoImplementedCustomerManager
{
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(); // <--- this method call doesn't take a customer.
}
private void DisplayName()
{
Customer cust = new Customer(); // <--- a completely separate customer from the one in Main()
Console.WriteLine("Name: {0}", cust.Name); // <--- should show no name. It's a new customer.
Console.ReadKey();
}
}
To get around this, you have to pass the value into the method. Like this:
public class Customer
{
public string Name { get; set; }
}
public class AutoImplementedCustomerManager
{
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(cust); // <--- passing the customer into the method.
}
private void DisplayName(Customer cust) // <--- note the incoming method parameter
{
Console.WriteLine("Name: {0}", cust.Name);
Console.ReadKey();
}
}
Note how I'm passing the existing Customer instance to the method? That's what you have to do in order to access the instance properly.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked as answer by RyanDunn Friday, September 4, 2009 4:00 PM
Friday, September 4, 2009 3:54 PM -
Try this
private void DisplayName(Customer cust)
{
if (!String.IsNullOrEmpty(cust.Name))
Console.WriteLine("Name: {0}", cust.Name);
}
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(cust);
Console.ReadLine();
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by RyanDunn Friday, September 4, 2009 4:16 PM
Friday, September 4, 2009 3:55 PM
All replies
-
Not sure what the block is at the top of your post. I'll remove it. Could you expound on what you're trying to do? Perhaps post some code?
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowserFriday, September 4, 2009 3:31 PM -
Sorry about that block.. here something quick I typed up as an example.
public class Customer { public string Name { get; set; } } public class AutoImplementedCustomerManager { static void Main() { Customer cust = new Customer(); cust.Name = "Bob Smith"; DisplayName(); } private void DisplayName() { Customer cust = new Customer(); Console.WriteLine("Name: {0}", cust.Name); Console.ReadKey(); } }
Still new, but getting better. :)Friday, September 4, 2009 3:47 PM -
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Employee emp = new Employee { FirstName = "John", LastName="Grove" }; Console.WriteLine("{0} {1}", emp.FirstName, emp.LastName); Console.ReadLine(); } } class Employee { public String FirstName { get; set; } public String LastName { get; set; } } }
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comFriday, September 4, 2009 3:49 PM -
You are instantiating a new instance of Customer and not setting the property.
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by RyanDunn Friday, September 4, 2009 4:01 PM
Friday, September 4, 2009 3:51 PM -
Your trouble isn't with automatic properties... it's with variable scope, and the fact that setting an instance value of one instance of a type doesn't ensure that value is available in all situations. Let's look at your code:
public class Customer
{
public string Name { get; set; }
}
public class AutoImplementedCustomerManager
{
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(); // <--- this method call doesn't take a customer.
}
private void DisplayName()
{
Customer cust = new Customer(); // <--- a completely separate customer from the one in Main()
Console.WriteLine("Name: {0}", cust.Name); // <--- should show no name. It's a new customer.
Console.ReadKey();
}
}
To get around this, you have to pass the value into the method. Like this:
public class Customer
{
public string Name { get; set; }
}
public class AutoImplementedCustomerManager
{
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(cust); // <--- passing the customer into the method.
}
private void DisplayName(Customer cust) // <--- note the incoming method parameter
{
Console.WriteLine("Name: {0}", cust.Name);
Console.ReadKey();
}
}
Note how I'm passing the existing Customer instance to the method? That's what you have to do in order to access the instance properly.
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked as answer by RyanDunn Friday, September 4, 2009 4:00 PM
Friday, September 4, 2009 3:54 PM -
Try this
private void DisplayName(Customer cust)
{
if (!String.IsNullOrEmpty(cust.Name))
Console.WriteLine("Name: {0}", cust.Name);
}
static void Main()
{
Customer cust = new Customer();
cust.Name = "Bob Smith";
DisplayName(cust);
Console.ReadLine();
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by RyanDunn Friday, September 4, 2009 4:16 PM
Friday, September 4, 2009 3:55 PM