Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys,

I'm playing around with C# and encountered a problem while trying to create a method in a class.

Actually I'm having 3 question.

This is my original code, have a look:
C++
namespace EApplication
{
    public partial class frmMainPage : Form
    {
        Button btnAddClientInfo = new Button();
        Button btnDataSheet = new Button();

        TabControl tabControl = new TabControl();

        public frmMainPage()
        {
            InitializeComponent();

            btnAddClientInfo.Parent = this;
            btnDataSheet.Parent = this;
            tabControl.Parent = this;
            
            btnAddClientInfo.Click += new EventHandler(this.btnAddClientInfo_Click);
            btnDataSheet.Click += new EventHandler(this.btnDataSheet_Click);

            this.Resize += new EventHandler(frmMainPage_Resize);
        }

        void btnAddClientInfo_Click(Object sender, EventArgs e)
        {
            //tabControl.Visible = true;
            TabPage tabpgAddClient = new TabPage("Add Client");
            AddClient userctrlAddClient = new AddClient();

            tabpgAddClient.Controls.Add(userctrlAddClient);
            tabControl.TabPages.Clear();
            tabControl.TabPages.Add(tabpgAddClient);
            tabControl.Visible = true;

            btnAddClientInfo.Enabled = false;
            btnDataSheet.Enabled = true;
        }

        void btnDataSheet_Click(Object sender, EventArgs e)
        {
            //tabControl.Visible = true;
            TabPage tabpgDataSheet = new TabPage("Data Sheet");
            DataSheet userctrlDataSheet = new DataSheet();

            tabpgDataSheet.Controls.Add(userctrlDataSheet);
            tabControl.TabPages.Clear();
            tabControl.TabPages.Add(tabpgDataSheet);
            tabControl.Visible = true;



            btnDataSheet.Enabled = false;
            btnAddClientInfo.Enabled = true;
        }

        void frmMainPage_Resize(Object sender, EventArgs e)
        {
        }
    }
}


Question 1:
Let say, I add a new method in class frmMainPage, I'm having errors.
8 Error Messages:
C++
MainPage.cs(52,9):error CS0116: A namespace does not directly contain members such as fields or methods

Same erros description to the other 7.

The code, looks like this:
C#
public frmMainPage()
{
           InitializeComponent();

            ....

           int test ()
           {
              return 1;
           }
}


Can anyone explain what is it wrong there, I have tried to declare the method as public, still the problem still occurs.

Question 2:
I remove the new method test() added in question 1 and created a new method as below:

C#
public frmMainPage()
{
       InitializeComponent();
      
       ....
}

...

void frmMainPage_Resize(Object sender, EventArgs e)
{
}


public int GetAddClientBottom()
{
    return btnAddClientInfo.Bottom;
}


And now I want this method be called from a different class, well it's a UserControl(another .cs file).

Here the place where method to be called:

C#
namespace EApplication
{
    public partial class AddClient : UserControl
    {
        public AddClient()
        {
            InitializeComponent();

            //resize the page size
            this.Width = this.Right - 45;
            this.Height = (this.Bottom - 80) - ( frmMainPage. + 15);
        }
    }
}


I want to call the
C++
GetAddClientBottom()
method, in place where I BOLD it. Unfortunaltely, i'm unable to place the call.

Any ideas, I', kinda out of idea what gets wrong, because I tried declare the method as public, but no use. Whats still missing?

Question 3:

In the original code above, you can see this in some places:
C#
btnAddClientInfo.Parent = this;
btnDataSheet.Parent = this;
tabControl.Parent = this;
tabControl.Parent = this;


Why do we need this?Can;t we just add the objects to the form?

Thanks alot. :)

Skunkhead.
Posted

#1: "A namespace does not directly contain members such as fields or methods" -- this is what it is. You did not put method in the form class, you messed up your curly brackets and put it outside the class but inside the name space.

#2: You can call the method of the class only if it is static, otherwise you can only call a method on instance to the class. You need to create an instance:

C#
class MyClass {
    internal MyAccessibleMethod() { /* ...*/ }
    public MyAccessibleMethodw() { /* ...*/ }
    int field;
}
MyClass myClassInstance = new MyClass();
myClassInstance.MyAccessibleMethod();


Of course, for accessibility the methods should be internal or public (if you need access from some other assembly).
Now, how instance methods work: pretty much like static methods, only there is an additional parameter called this. This parameter represents the instance, can be used to access instance members. It is not shown in parameter list but technically implemented as an extra hidden parameter. It is visible inside methods (including constructors):

C#
internal MyAccessibleMethod() { this.field = 3; /*...*/ }


In most cases "this." can be omitted. Sometimes, qualification of a member with "this." is absolutely unavoidable. Example: there is a parameter with the same name as field.

#3: I just explained this; in your sample it can represent any instance of Control (including Form).
This code
C#
btnAddClientInfo.Parent = this;

is equivalent to
C#
this.Controls.Add(btnAddClientInfo);


This is a way to add children to a controls during run-time; can be done when a form is already shown -- the code will show a new child immediately. The child control should be set up before inserting (location, text, etc).

One general advice: stop doing Forms programming for a while: your level on confusion is too high. Instead, long the C# itself and some basic programming using much more simple coding, maybe as a console application.
 
Share this answer
 
v4
Comments
Sandeep Mewara 26-Jan-11 3:32am    
Good answer. My 5!
Sergey Alexandrovich Kryukov 26-Jan-11 14:10pm    
Thank you very much, Sandeep. Answer to #3 updated, fixed my mistake.
--SA
Sergey Alexandrovich Kryukov 26-Jan-11 11:35am    
Added important fix to the answer to Q#3.
Sorry for the inconvenience.
--SA
skunkhead 30-Jan-11 21:43pm    
thanks
Sergey Alexandrovich Kryukov 30-Jan-11 21:54pm    
You're welcome. Thanks for accepting this answer. Hope it will help you.
--SA
Q1: The code you added doesn't appear inside of a class. You appear to have to the code outside of a class section somewhere.

Q2: It's not that there's something missing. it's that you have a severe design flaw. Your usercontrol should not call ANYTHING by the form's name. A usercontrol should not care which form it's on. If it did, it could only be used on that one form, nowhere else.

You're also dependant on a certain piece of code existing on that form. This means that every form that you drop this control on now needs to implement a method or property so your control will work. This is a heavily flawed design. I can't tell you how to fix it because, simply, I have no idea what your usercontrol is doing, what your code is doing, or why.

Q3: Yes. I have no idea why you need that code since there isn't enough of your code to see what's going on.
 
Share this answer
 
Comments
skunkhead 30-Jan-11 21:43pm    
thanks Dave

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900