Click here to Skip to main content
15,895,538 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii Friends

I have Following Classes in C#

C#
public class Phonebook_Response
{
    public Phonebook_Response() { }

    //do something here....
}

public class Phonebook_Error
{
	public Phonebook_Error() {}
          private int error_code;
          public int _code
         {
           get
           {
            return error_code;
            }
           set
           {
            error_code=value;
           }

        }
}

public class Phonebook_Status
{
    public Phonebook_Status() {}

    private string status;

    public string _Status
    {
        set
        {
            status = value;
        }
        get
        {
            return status;
        }
    }}


Now what I want is

at place of do here....

I want to make object of Phonebook_Status class and check what is value of instance "status" in status class

according to this value want to set value of instances of error class

for example

C#
if(status == "success")
{
  _code=1;
}


how can I achive it ??? please help me ....
Posted

You are trying to access members in the directly opposite way; no wonder it cannot work. Your public member is _Status (property), not status (a private field, a backing field for _Status. So access it by _Status, not status, only give the members nice names, without violation of (good) Microsoft naming conventions ('_' is not recommended).

Also, you can use auto-implemented properties instead. Something like:
C#
public string Status { get; private set; }

or
C#
public string Status { get; set; } // both getter and setter are public, if you need it
What else? Well, using string type for status is a really, really bad idea. Totally unsupportable, especially when you compare the value with a immediate constant like "success". Imagine you misspell it. A compiler won't help you to detect the problem. Imagine you changes some of the values. Same thing. Very, very unsupportable code. Consider using enumeration types.

More generally, the trend to use strings representing data instead of data itself is a very destructive fallacy of many beginners these days. Just think about it.

—SA
 
Share this answer
 
As breifly explain by SA change your example code like this

C#
if(object._status == "success")
    {
      _code=1;
    }
 
Share this answer
 

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