Link to home
Create AccountLog in
Avatar of Wizard_Microsystems
Wizard_Microsystems

asked on

Error: <Object>.Method() is a 'method' but is used like a 'type'

I grabbed some sample DataGridView code off of the MSDN site and placed it into Visual Studio C# 2010.  Upon trying to build, I get the error "DataGridViewSample.MyForm.Form1() is a 'method' but is used like a 'type'".  

The error initially appeared on the line instantiating the object, but since I've changed it the error now appears because of the line of code in Main(), Application.Run(new Form1());.  (The compiler doesn't like the Form1() method).

When I copied the code from MSDN, MyForm (the object) was originally named Form1, which is the same as the method Form1(), and since I was getting a similar error there I renamed the object to MyForm.  I have effectively pushed the error to a different line of code!

I have beat my head against the wall off-and-on for over 4 hours looking at every resource possible, including EE.  I'm stuck.  Help!!!  ;)  I'm sure this will be easy for an experienced programmer.  Thanks in advance!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DataGridViewSample
{
    
    public class MyForm : Form                <-  MyForm was originally named Form1 and the error threw here
    {
        private DataGridView dataGridView1 = new DataGridView();

        private System.Collections.ArrayList customers =
            new System.Collections.ArrayList();

        [STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new Form1());      <- since renaming the object, the same error throws on Form1()
        }

        public void Form1()
        {
            this.dataGridView1.Dock = DockStyle.Fill;
            this.Controls.Add(this.dataGridView1);
            this.Load += new EventHandler(Form1_Load);
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {

	  }
. . . 

    }
}

Open in new window

SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Please try following modified code:
using System;
using System.Windows.Forms;

namespace DataGridViewSample
{
    public partial class MyForm : Form
    {
        private DataGridView dataGridView1;

        private System.Collections.ArrayList customers =
            new System.Collections.ArrayList();

    
        public MyForm()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.dataGridView1.Location = new System.Drawing.Point(0, 0);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.Size = new System.Drawing.Size(292, 273);
            this.dataGridView1.TabIndex = 0;
            // 
            // MyForm
            // 
            this.ClientSize = new System.Drawing.Size(292, 273);
            this.Controls.Add(this.dataGridView1);
            this.Name = "MyForm";
            this.Load += new System.EventHandler(this.MyForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.ResumeLayout(false);

        }

        [STAThreadAttribute()]
        public static void Main()
        {
            Application.Run(new MyForm());      
        }

        private void MyForm_Load(object sender, EventArgs e)
        {

        }
    }
}

Open in new window

Avatar of Wizard_Microsystems
Wizard_Microsystems

ASKER

Close please.
My apologies for letting this question become abandoned.  I have reviewed the question and have done what I believe is a fair split of points based on how much each expert's comments assisted.