Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Csharp.net
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:43am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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 at 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 at 14-Nov-12 17:45pm
   
Thx so much.
Christian Graus at 14-Nov-12 17:50pm
   
You should mark this as the answer, it helps people find existing answers for new questions
Hoa Súng at 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)
Christian Graus at 14-Nov-12 17:52pm
   
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 at 14-Nov-12 18:07pm
   
Thx so much <3
Christian Graus at 14-Nov-12 18:48pm
   
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
0 OriginalGriff 205
1 Sergey Alexandrovich Kryukov 185
2 jlopez788 109
3 Rob Philpott 100
4 Prakriti Goyal 97
0 OriginalGriff 6,837
1 Sergey Alexandrovich Kryukov 5,589
2 Maciej Los 3,479
3 Peter Leow 3,323
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100