简体   繁体   English

无法访问外部类型的非静态成员

[英]Cannot access a non-static member of outer type

The error is in TextBox1.Text and Label2.Text are the "non-static members of outer type". 错误发生在TextBox1.Text和Label2.Text是“外部类型的非静态成员”中。

Everything else is in good working order except for TextBox1.Text and Label2.Text. 除了TextBox1.Text和Label2.Text,其他所有东西都工作正常。

The program should take a pre existing ftp file(server side) and copy it to a file specified in TextBox1.Text (client side). 该程序应获取一个预先存在的ftp文件(服务器端),并将其复制到TextBox1.Text(客户端)中指定的文件中。

Error code: 错误代码:

Cannot access a non-static member of outer type 'FTPDownload._Default' via nested type 'FTPDownload._Default._Default' C:\\Users\\user\\path\\Default.aspx.cs 无法通过嵌套类型“ FTPDownload._Default._Default”访问外部类型为“ FTPDownload._Default”的非静态成员C:\\ Users \\ user \\ path \\ Default.aspx.cs

Default.aspx Default.aspx

<center><table><tr><td align="center"><br /><br />
    <asp:TextBox ID="TextBox1" runat="server">C:/</asp:TextBox></td></tr><tr><td align="center"><br />
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /></td></tr><tr><td align="center"><br /><br />
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</td></tr></table></center>

Default.aspx.cs Default.aspx.cs

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public partial class _Default : System.Web.UI.Page
{
        private string host = "ftp://mysite.com";
        private string user = "ftp72390003-0";
        private string pass = "pass";
        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;
    protected void Button1_Click(object sender, EventArgs e)
    {
        download("GraphingPlanets.exe", TextBox1.Text + "GraphingPlanets.exe");
    }
    public void download(string remoteFile, string localFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(host + "/" + remoteFile));
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }
            }
            catch (Exception ex) { Label2.Text = "Write: " + ex.ToString; }
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;
        }
        catch (Exception ex) { Label2.Text = "Total: " + ex.ToString; }
    }

C# isn't like Java. C#不像Java。 An inner class doesn't carry an implicit reference to the outer instance used to create it. 内部类不带有对用于创建它的外部实例的隐式引用。 You will have to pass the reference explicitly to the inner class instance so that it can use it. 您必须将引用显式传递给内部类实例,以便它可以使用它。

Alternatively, if you did not mean to create the inner "_Default" class, but instead to just add code to the outer "_Default" class, you should fix the code so that it doesn't have the nested class. 另外,如果您不是要创建内部“ _Default”类,而是只是向外部“ _Default”类添加代码,则应修复该代码,使其不具有嵌套类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#无法访问外部类型的非静态成员 - C# cannot access a non-static member of outer type 无法通过嵌套类型X访问外部类型X的非静态成员 - Cannot access a non-static member of outer type X via nested type X 无法通过嵌套类型访问外部类型的非静态成员 - Cannot access a non-static member of outer type via nested type 无法通过嵌套类型“FormMain.ImageDelegateClass”访问外部类型“FormMain”的非静态成员 - Cannot access a non-static member of outer type 'FormMain' via nested type 'FormMain.ImageDelegateClass' 无法使用第三方提供的类通过嵌套类型访问外部类型XXX的非静态成员 - Cannot access a non-static member of outer type XXX via nested type with a third-party provided class 使标签静态错误:无法通过嵌套类型“windowsForm8.Form1.DBConnect”访问外部类型“windowsForm8.Form1”的非静态成员 - make a label static error : cannot access a non-static member of outer type 'windowsForm8.Form1' via nested Type 'windowsForm8.Form1.DBConnect' 访问非静态成员所需 - Required to access non-static member C#无法在静态上下文中访问非静态成员字段,除非实际处于静态上下文中 - C# Cannot access non-static member field in static context, without actually being in static context 无法访问非静态字段 - Cannot access non-static field 访问非静态成员需要对象引用 - An object reference is required to access non-static member
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM