C#与私有变量的歧义

时间:2012-09-22 22:38:54

标签: c#

让我举一个问题的简单例子。我似乎无法理解错误:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetApp.Manage;

namespace Toaster.Library
{
    class NetappConnection
    {
        private string Hostname     {get; set;}
        private int ApiMajor        {get; set;}
        private int ApiMinor        {get; set;}
        private string Username     {get; set;}
        private string Password     {get; set;}

        private NaServer NetappServer {get; set;}

        public void NetappConnection(string Hostname, int ApiMajor, int ApiMinor,     string Username, string Password)
        {
            this.Hostname = Hostname;
            this.ApiMajor = ApiMajor;
            this.ApiMinor = ApiMinor;
            this.Username = Username;
            this.Password = Password;

            this.ConnectToNetapp();
        }

        private void ConnectToNetapp()
        {
            NaServer s = new NaServer(this.Hostname, this.ApiMajor, this.ApiMinor);
            s.ServerType = NaServer.SERVER_TYPE.FILER;
            s.TransportType = NaServer.TRANSPORT_TYPE.HTTP;
            s.Port = 80;
            s.Style = NaServer.AUTH_STYLE.LOGIN_PASSWORD;
            s.SetAdminUser(this.Username, this.Password);

            this.NetappServer = s; <<-- Error: Ambiguity between 'Toaster.Library.NetappConnection.NetappServer' and 'Toaster.Library.NetappConnection.NetappServer()'
        }

        public NaServer NetappServer()
        {
            return this.NetappServer;
        }
    }
}

我是C#新手,老实说。但我不明白为什么这是不可能的。是不是因为我把参与从o传递到了这个。变量?

这个目标应该是我能够重用NaServer对象。

2 个答案:

答案 0 :(得分:2)

private NaServer NetappServer {get; set;}

public NaServer NetappServer()

具有相同的名称,更改一个。

答案 1 :(得分:0)

属性包含getter和/或setter get; set;。变量没有。关键字class是小写的(根据CodesInChaos)。字段(或类成员变量,如果您愿意)通常使用前导下划线写入并具有_camelCase,即首字母为小写,后续部分以大写字母开头。

class MyClass 
{
   private SomeObject _variable;

   public void SomeMethode()
   {
      _variable = new SomeObject();
   }
}

Visual Studio的C#编辑器和C#编译器为您提供了很好的提示。考虑到它们!