Why should I use the typeof keyword to assign a data source to a BindingSource.DataSource?

4

For example:

BindingSource bs = new BindingSource(); 

bs.DataSource = typeof(object);      

For what reason should I use typeof(object) instead of just object or even "object" ?

edit: As others have asked, here is the code where I first saw the use of the typeof keyword, and in that case it was used with BindingSource . The code was found here , and yes, the code works.

BindingSource bs = new BindingSource();                              //**

private void Form1_Load(object sender, EventArgs e)
{
    bs.DataSource = typeof(Airplane);                                //**
    bs.Add(new Airplane("Boeing 747", 800));
    bs.Add(new Airplane("Airbus A380", 1023));
    bs.Add(new Airplane("Cessna 162", 67));

    grid.DataSource = bs;                                            //**
    grid.AutoGenerateColumns = true; // create columns automatically //**
    txtModel.DataBindings.Add("Text", bs, "Model");                  //**
}
    
asked by anonymous 01.12.2015 / 00:26

3 answers

2

In all cases I've used the BindingSource.DataSource " was with an object that contained data, because that is the purpose of this property, define a data source that will be used to make a binding in some part of the form .

Pass only one instance of System.Type to type Object into the BindingSource.DataSource property - which is what you are doing when using the typeof in bs.DataSource = typeof(object); - "is wrong".

The reason I said "is wrong" is that it was not clear in your question where you are planning to use this instance of BindingSource , it could be being used for example by a third party in>, something that was not among the Windows Forms default controls, and in that case the way it was implemented might vary.

    
01.12.2015 / 12:01
2
  

Why should I user the "typeof" keyword to set a DataSource in a Databinding?

You should not. I do not know where you got it, but it does not seem to make any sense. There is no way to give more details, because you did not specify where you took this example, the context that is inserted and also there are only two lines of code. Maybe this serves some specific purpose and you did not realize it, but there is the possibility that it is just a lack of knowledge of the person who set the example.

Searching a little, I saw that this is a "trick" to make DataSource empty, because if you bindingSource.DataSource = null you will get System.ArgumentException .

I'm not sure why this works to clean DataSource, but I'll continue to search and edit the response when I have something concrete.

  

For what reason should I use typeof (object) instead of only object or even "object"?

As I already said, I do not know why this "trick", but I'll explain the difference between these two.

typeOf(object) returns Type of what was passed as argument. While object is the type itself, I mean, by doing typeOf(T) you are returning an instance of System.Type , other than passing a type directly (such as T or object ).     

01.12.2015 / 12:13
0

You must assign an object to bs.DataSource . With typeof(object) you do the "conversion" of Class object to an object of type Type . Using only object would be interpreted by the compiler as a statement to an object of class object or a possible cast, but not the class reference you need.

It would be something like

   object obj = new object();
   if (obj.GetType() == typeof(object)) { } //isso roda

   if (obj.GetType() == object) { } //isso não
    
01.12.2015 / 02:36