Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ProgrammerGuide_001
{
    public class WinForm:Form
    {
        private Button button1;
        private TextBox textBox1;
        public WinForm()
        {
        }
        static void Main(string[] args)
        {
            WinForm myFrm = new WinForm();
            myFrm.button1 = new Button();
            myFrm.textBox1 = new TextBox();
            myFrm.Text = "my first window application";
            Application.Run(myFrm);
        }
    }
}


I have a question that when I declare 2 object variable like this:
C#
private Button button1;
private TextBox textBox1;

why I can't instantiate with the following statements:
C#
button1=new Button();
textBox1=new TextBox();

but this code can:
C#
myFrm.button1 = new Button();
myFrm.textBox1 = new TextBox();
Posted

1 solution

Because you're instantiating them inside the instance of the form class. This code is mostly retarded, it makes no sense, a basic console app is a better way to learn this concept, not as many distractions. You're in a static method, it has no state, so the objects don't exist inside main, they only exist inside an INSTANCE of the class.
 
Share this answer
 
Comments
Hoa Súng 14-Nov-12 17:13pm    
thx for helping me. I just understand a little bit so if you don't mind can explain more or give some example to clarify this matter.
Christian Graus 14-Nov-12 17:17pm    
I'm not sure what else to say. A static method can be called from the class name as in MyClass.CallMethod. Imagine you have a room full of cars. Each car can be a different colour, so colour is a property of an INSTANCE of car, created with Car c = new Car(); But, there's no Car.Color. Color is a property of *A* car, not car as a concept. Static methods and properties are used to store something that's always the same. You might have a static method called GetCars. Car.GetCars might return a list of cars, so it makes no sense to require a Car instance to do that.

Read a book or online article about object oriented programming ( OOP )
Hoa Súng 14-Nov-12 17:45pm    
Thx so much.
Christian Graus 14-Nov-12 17:50pm    
You should mark this as the answer, it helps people find existing answers for new questions
Hoa Súng 14-Nov-12 17:50pm    
but I can replace this code above like that:
public class WinForm:Form
{
//private Button button1;
//private TextBox textBox1;
public WinForm()
{
}
static void Main(string[] args)
{
WinForm myFrm = new WinForm();
//myFrm.button1 = new Button();
//myFrm.textBox1 = new TextBox();
Button button1=new Button();
TextBox textBox1=new TextBox();
myFrm.Text = "my first window application";
Application.Run(myFrm);
}
}

So what differences between them. (sorry if I ask you many stupid question but I want to know clearly)

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