Click here to Skip to main content
14,239,882 members
Rate this:
Please Sign up or sign in to 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:

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
Rate this:
Please Sign up or sign in to vote.

Solution 1

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

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

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



Mark it as answer if it is helpful
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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
   
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.
   
Hm. Agree.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 3

it still returns null value, txtCustomer.text has value, but when it comes to AIProperty it still returns null value, i also tried this,

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

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.
   
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
Rate this:
Please Sign up or sign in to vote.

Solution 5

Please have a look Auto-Implemented Properties[^]

From your code,

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

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


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

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:)
   
v3

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100