Why should I use the typeof keyword to assign a data source to a Bindingsource.Datasource?

Asked

Viewed 155 times

4

For example:

BindingSource bs = new BindingSource(); 

bs.DataSource = typeof(object);      

Why should I use typeof(object) instead of just object or even "object"?

Edit: As other people have asked, there’s the code where I first saw the keyword typeof, and in this case was used with a BindingSource. The code has been 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");                  //**
}
  • 3

    I don’t think you need to, look at the example here: https://msdn.microsoft.com/pt-br/library/system.windows.forms.bindingsource.datasource(v=vs.110). aspx

3 answers

2

In all cases I used the property 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 somewhere on the form.

Pass only one instance of System.Type for the guy Object into the property BindingSource.DataSource - which is what you are doing while using the operator 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 control third party, something that was not among the standard Windows Forms controls, and in this case the mameira as it was implemented could vary.

2

Why should I use the keyword "typeof" to set a Datasource in a Databinding?

You shouldn’t. I don’t know where you get it from, but it doesn’t 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 haven’t noticed, but there’s a possibility it’s just lack of knowledge of who made the example.

Researching a little I saw that this is a "trick" to leave the DataSource empty, because if you do bindingSource.DataSource = null will receive a System.ArgumentException.

I don’t know exactly why this works to clean up Datasource, but I will continue to search and edit the answer when I have something concrete.


Why should I use typeof(Object) instead of only Object or even "Object"?

Like I said, I don’t know why this "trick" is happening, but I’ll explain the difference between these two.

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

0

You must assign an object to bs.DataSource. With the typeof(object) you make the "conversion" of the Class object for an object of the type Type. Use only the object would be interpreted by the compiler as a statement to an object of the 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.