Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi guyz,

why my class with auto implemented property gets null values? here's what i did:

1. i instantiate the class with the auto-implemented property and set the values like:

C#
class AIproperty
{
   public string CustomerName{ get; set; }

}

//Main class
AIproperty setvalues = new AIproperty();
setvalues.CustomerName = txtCustomer.text.ToString():


//other class
AIproperty setvalues = new AIproperty();
console.write(setvalues.CustomerName)


console.write(setvalues.CustomerName)
returns null value

what should i do?

-thanks in advance
Posted

First of all, you should initialize the property with string.Empty in the class constructor which will solve the null value problem.

C#
class AIproperty
{
   public string CustomerName{ get; set; }

   public AIproperty()
   {
      this.CustomerName = string.Empty;
   }
}



Mark it as answer if it is helpful
 
Share this answer
 
The class AIProperty is implemented correctly. If CustomerName returns null after assignment, the only reason is: assigned value was null. In your code the assigned value was txtCustomer.text.ToString(). Check it up: it must be null.

By the way, chances are, ToString is redundant as txtCustomer.text might be of the string type.

—SA
 
Share this answer
 
Comments
Venkatesh Mookkan 2-Aug-11 0:15am    
Hi SA,
ToString will fail if txtCustomer.Text is null.

I guess he/she is talking about the second code block and not the first. Or he assigned to the first block and trying to get the value from the second block.
Sergey Alexandrovich Kryukov 2-Aug-11 16:25pm    
Hm. Agree.
--SA
it still returns null value, txtCustomer.text has value, but when it comes to AIProperty it still returns null value, i also tried this,

C#
class AIproperty
{
   public string CustomerName{ get; set; }
 
   public AIproperty()
   {
      this.CustomerName = string.Empty;
   }
}


when the other class gets the value it still returns null value,, help pls.
 
Share this answer
 
The problem here is that you are creating the class instance twice, in two different classes, and the second time you create it, you assign nothing to AIproperty.CustomerName before the call to Console.Write.

If you want the value of the CustomerName Property set in 'Main class to persist in 'other class, you need to create the instance of AIproperty once, and establish a way for 'other class to access the instance of the AIProperty class created in 'Main class.
 
Share this answer
 
Comments
Member 8092191 2-Aug-11 1:10am    
can you give me an example??
BillWoodruff 2-Aug-11 3:48am    
Hi, I think the 'how-to' here is something you can come up with by yourself if you answer two questions, but first, you need to clarify if this is a Console application: in that case 'Main will be a static method executed in a Public Class, Program, and:

any instance of 'AIproperty created in the static Main method will not exist outside of that method.

With that as a basis, the questions we need to answer are:

1. what has to happen in the static method 'Main for the instance of 'AIproperty created there to persist, or be visible to classes outside 'Program ?

2. assuming you succeed in making the instance of 'AIproperty created in the static 'Main method of the 'Program Class visible to other classes: what will the other classes have to do to access that instance and its internal 'CustomerName property value ?

Another way to put this is: what does the code in the 'Main method need to do to "publish" a publicly accessible value for the CustomerName property of AIproperty that can be "read" by external classes ?

In my humble opinion, really exploring the issues raised in this problem will enable you, in the future, to deal with similar problems of references, and dependency injection, in many other scenarios.

good luck, Bill
Please have a look Auto-Implemented Properties[^]

From your code,

C#
AIproperty setvalues = new AIproperty();
console.write(setvalues.CustomerName);


setvalues.CustomerName returns null and it should be, See below code how does Auto implemented properties CLR handle,

Step 1: I implemented a small class

C#
public class AutoImplemetedProperties
{
    public string PropertyOne { get; set; }
}


Step 2: related reflected code for the above class is,

C#
public class AutoImplemetedProperties
{
    // Fields
    [CompilerGenerated]
    private string <PropertyOne>k__BackingField;

    // Properties
    public string PropertyOne
    {
        [CompilerGenerated]
        get
        {
            return this.<PropertyOne>k__BackingField;
        }
        [CompilerGenerated]
        set
        {
            this.<PropertyOne>k__BackingField = value;
        }
    }
}


So the default value for PropertyOne is null and when we try to access, it returns null which it should be, unless we assign any value to it and then try to access.

As result, after you instantiate AIproperty and with out assigning any values to CustomerName, it should return null as we saw the above example code.

Hope it helps:)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900