English 中文(简体)
利用其他班次C#的护卫
原标题:Using strings from other classes C#
  • 时间:2012-01-14 13:40:13
  •  标签:
  • c#
  • .net

我有一个类别要求,在请求/提出指示时,应发出指示。

class Checks
{
    public Checks(string hostname2)
    {
        // logic here when class loads

    }

    public void Testing()
    {
        MessageBox.Show(hostname2);
    }
}

怎样才能将“影子”带入“ class子”级建筑商,并允许在“cks子”级的任何地点打上这种str子?

例如,我从表1类别中叫作支票(影子2),现在,当支票类别开始时,我可以使用我的支票类别中的东道名称2。

最佳回答

2. 申报该类成员,并向该建筑商中的成员分配其价值:

class Checks
{
    private string hostname2;

    public Checks(string hostname2)
    {
       this.hostname2 = hostname2; // assign to member
    }

    public void Testing()
    {
        MessageBox.Show(hostname2);
    }
}

如果你还需要外部接触,使之成为财产:

class Checks
{
    public string Hostname2 { get; set; }

    public Checks(string hostname2)
    {
       this.Hostname2 = hostname2; // assign to property
    }

    public void Testing()
    {
        MessageBox.Show(Hostname2);
    }
}

财产首先通过公约发出资本信件。 现在你们可以这样说:

Checks c = new Checks("hello");
string h = c.Hostname2; // h = "hello"

感谢 请指出这一点:如果你希望财产只读,使定购物的私人:

public string Hostname2 { get; private set; }
问题回答

您需要将施工者的论点复制成一个类别变量:

class Checks {

    // this string, declared in the class body but outside
    // methods, is a class variable, and can be accessed by
    // any class method. 
    string _hostname2;

    public Checks(string hostname2) {
        _hostname2 = hostname2;
    }

    public void Testing() {
        MessageBox.Show(_hostname2);
    }

}

You can expose a public property to retun the hostname2 value which is the standard for exposing your private varibles


class Checks
{
    private string _hostname;
    public Checks(string hostname2)
    {
        _hostname = hostname2;
    }
    public string Hostname 
    {
       get { return _hostname; }
    }
}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签