Click here to Skip to main content
Sign Up to vote bad
good
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:
        private Button button1;
        private TextBox textBox1;
why I can't instantiate with the following statements:
        button1=new Button();
        textBox1=new TextBox();
but this code can:
         myFrm.button1 = new Button();
         myFrm.textBox1 = new TextBox();
Posted 14 Nov '12 - 10:43


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.
  Permalink  
Comments
Hoa Súng - 14 Nov '12 - 17:13
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:17
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:45
Thx so much.
Christian Graus - 14 Nov '12 - 17:50
You should mark this as the answer, it helps people find existing answers for new questions
Hoa Súng - 14 Nov '12 - 17:50
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)
Christian Graus - 14 Nov '12 - 17:52
This creates a new button and Textbox, but never uses it for anything. The form has different buttons to the ones you created, which are local and are not inside myFrm, or used there or any where else. You scoped a LOCAL variable, which is never used. But you could not scope a MEMBER variable inside a static method. You can create local variables, everywhere. The only thing is, you can't access MEMBER variables in a static method.
Hoa Súng - 14 Nov '12 - 18:07
Thx so much <3
Christian Graus - 14 Nov '12 - 18:48
No problem - good luck !!

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 253
1 Rohan Leuva 220
2 Sergey Alexandrovich Kryukov 208
3 Abhinav S 168
4 Mahesh Bailwal 165
0 Sergey Alexandrovich Kryukov 8,494
1 OriginalGriff 6,799
2 CPallini 3,603
3 Rohan Leuva 2,923
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid