Alex Gordon
Alex Gordon

Reputation: 60691

c# cannot access textboxes from same class

i have two separate questions; however, since they are very similar i will ask them in one posting.

what is the reason i cannot reference the textboxes from here? i created another file in my project and put

namespace EnterData.DataEntry
{
    public partial class WebForm1 : System.Web.UI.Page
    { 

to make it go into the same namespace and partial class as the webform. but i cannot access the textbox!

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)
            {

            }

i get this error:

Error 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

on all textboxes

what is the reason that i cannot access the textboxes from here?

Upvotes: 2

Views: 486

Answers (3)

Jim Dagg
Jim Dagg

Reputation: 2032

Did you mean to nest classes here? If you intended to declare a method that returns a Main_Lom_Form(), try this:

public LOMDLL.Main_Lom_Form PopulateMainForm()

If you intended to call that method against a member of WebForm1 called TheForm, then instantiate that outside of the call to PopulateMainForm:

LOMDLL.Main_Lom_Form TheForm = new LOMDLL.Main_Lom_Form();
public void PopulateMainForm()
{
    // snip
}

Upvotes: 2

Matthew Abbott
Matthew Abbott

Reputation: 61589

I think you have a few issues with your code. The first one that pops out is this:

public class LOMDLL.Main_Lom_Form PopulateMainForm()

That is not a valid line of C# code. I'm assuming you actually meant to write:

public LOMDLL.Main_Lom_Form PopulateMainForm()

Secondly, if you have defined Main_Lom_Form as static, you can't instantiate it, it's a static class. You can address this:

public class Main_Lom_Form

I think its a combination of the above two issues which is causing the compiler to have a stroke.

Upvotes: 2

Davide Piras
Davide Piras

Reputation: 44595

here there is an error:

public class LOMDLL.Main_Lom_Form PopulateMainForm()

are you declaring this class inside the webform class? anyway the class declaration is wrong.

for the last point of your question, when you have this:

public static class Main_Lom_Form

imagining you moved out from the other webform class, what is the namespace you have around it (before)? Just play around moving the class inside same namespace than class WebForm1 and you will not need to put the namespace before the class name, you will be able to create it like this:

var obj = new Main_Lom_Form();

in case it would make sense, but I doubt ;-)

Upvotes: 2

Related Questions