C# Ambigüedad con variables privadas [cerrado]
Frecuentes
Visto 477 veces
2
Let me give you a quick example of my problem. I can't seem to understand the error:
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;
}
}
}
I a C# novice to be honest with you. But I don't understand why this would not be possible. Is it because of I pass the referance from o to this.Variable?
The Goal of this should be that I'm able to reuse the NaServer Object.
2 Respuestas
2
private NaServer NetappServer {get; set;}
y
public NaServer NetappServer()
have the same name, change one.
Respondido el 22 de Septiembre de 12 a las 23:09
omg should have seen that :( thanks - Norynda
@Norynda also: remove the void
del constructor de NetappConnection
- marc gravell
thx done that, stumbled upon that already... - Norynda
0
Properties have getters and/or setters get; set;
. Variables do not. The keyword class
is lower case (according to CodesInChaos). Fields (or class member variables, if you prefer) are often written with a leading underscore and have _camelCase, i.e. first letter is lower case and successive parts start with an upper case letter.
class MyClass
{
private SomeObject _variable;
public void SomeMethode()
{
_variable = new SomeObject();
}
}
The C# editor of Visual Studio and the C# compiler give you very good hints of what may be wrong. Take them into account!
Respondido el 22 de Septiembre de 12 a las 23:09
The words you say are valid (although holy wars have started over the underscore), yet I'm not sure any of this really applies to the question...? - marc gravell
The OP changed his code example meanwhile. - Olivier Jacot Descombes
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c# or haz tu propia pregunta.
The only reason this should give an error is because you wrote
Class
with a captial letter. - CodesInChaosSomething else is wrong here. As was said, assuming you mistyped the class declaration, there is nothing wrong with this code. Can you post the actual error in it's complete form? And the real class? - Erik Funkenbusch
First of all, this is not a variable but a property. - Olivier Jacot-Descombes