锄头从其他类访问类的物种数据 [英] Hoe to access Data Of Inbstance Of Class From Other Class

查看:84
本文介绍了锄头从其他类访问类的物种数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii朋友



我有以下C级课程#



Hii Friends

I have Following Classes in 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;
        }
    }}





现在我想要的是



在这里的地方....



我想制作Phonebook_Status类的对象并检查状态中实例status的值是什么class



根据这个值想要设置错误类实例的值



例如





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

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





我怎么能实现呢???请帮助我....



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

推荐答案

您正试图以直接相反的方式访问成员;难怪它不能工作。您的公共成员是 _Status (属性),而不是 status (私有字段,<$ c $的支持字段) c> _Status 。所以通过 _Status 访问它,而不是 status ,只给会员好名字,没有违反(好)微软命名约定(不推荐'_')。



此外,你可以使用自动实现的属性。有点像:

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:
public string Status { get; private set; }






or

public string Status { get; set; } // both getter and setter are public, if you need it

还有什么?好吧,使用 string 类型的状态是一个非常非常糟糕的主意。完全不受支持,特别是当您将值与立即常量(如成功)进行比较时。想象一下你拼错了。编译器无法帮助您检测问题。想象一下,你改变了一些价值观。一样。非常非常不受支持的代码。考虑使用枚举类型。



更一般地说,使用字符串代表数据而不是数据本身的趋势对于许多初学者来说是非常具有破坏性的谬误。试想一下。



-SA

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


由SA简要解释改变你的示例代码喜欢这个



As breifly explain by SA change your example code like this

if(object._status == "success")
    {
      _code=1;
    }


这篇关于锄头从其他类访问类的物种数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆