[英]c# cannot access textboxes from same class
我有两个单独的问题; 但是,由于它们非常相似,因此我会在一个帖子中问他们。
我无法从此处引用文本框的原因是什么? 我在项目中创建了另一个文件并放入
namespace EnterData.DataEntry
{
public partial class WebForm1 : System.Web.UI.Page
{
使其进入与Webform相同的名称空间和子类。 但我无法访问文本框!
public partial class WebForm1 : System.Web.UI.Page
{
public class LOMDLL.Main_Lom_Form PopulateMainForm()
{
//populate class
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
try
{
TheForm.lom_number = lom_numberTextBox.Text.ToInt();
TheForm.identified_by = identified_byTextBox.Text;
TheForm.occurrence_date = occurrence_dateTextBox.Text.ToDateTime();
//TheForm.pre_contact = pre_contactTextBox.Text; //need to create this texdtbox
//TheForm.pre_practice_code = pre_practice_codeTextBox.Text; //create this
TheForm.report_by = report_byTextBox.Text;
TheForm.report_date = report_dateTextBox.Text.ToDateTime();
TheForm.section_c_comments = section_c_commentsTextBox.Text;
TheForm.section_c_issue_error_identified_by = section_c_issue_error_identified_byTextBox.Text;
TheForm.section_d_investigation = section_d_investigationTextBox.Text;
TheForm.section_e_corrective_action = section_e_corrective_actionTextBox.Text;
TheForm.section_f_comments = section_f_commentsTextBox.Text;
}
catch (Exception e)
{
}
我收到此错误:
错误r 20 Cannot access a non-static member of outer type 'EnterData.DataEntry.WebForm1' via nested type 'EnterData.DataEntry.WebForm1.LOMDLL' C:\\Documents and Settings\\agordon\\My Documents\\Visual Studio 2008\\Projects\\lomdb\\EnterData\\DataEntry\\DAL.cs 68 38 EnterData
在所有文本框上
我无法从此处访问文本框的原因是什么?
这里有一个错误:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
您是在webform类中声明该类吗? 无论如何,类声明是错误的。
对于问题的最后一点,当您有以下内容时:
public static class Main_Lom_Form
想象一下您从另一个Webform类中移出了,之前(周围)的名称空间是什么? 只需将类移到与WebForm1
类相同的名称空间中,您无需将名称空间放在类名称之前,就可以这样创建它:
var obj = new Main_Lom_Form();
以防万一,但我怀疑;-)
我认为您的代码有一些问题。 弹出的第一个是:
public class LOMDLL.Main_Lom_Form PopulateMainForm()
这不是有效的C#代码行。 我假设您实际上打算写:
public LOMDLL.Main_Lom_Form PopulateMainForm()
其次,如果您已将Main_Lom_Form
定义为静态,则无法实例化它,这是一个静态类。 您可以解决此问题:
public class Main_Lom_Form
我认为这是上述两个问题的结合,导致编译器出现问题。
您是要在这里嵌套课程吗? 如果要声明一个返回Main_Lom_Form()
,请尝试以下操作:
public LOMDLL.Main_Lom_Form PopulateMainForm()
如果您打算针对称为TheForm
的WebForm1
成员调用该方法,请在对PopulateMainForm
的调用之外实例化该方法:
LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
public void PopulateMainForm()
{
// snip
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.