Typecasting

  • Thread starter Thread starter Christopher Weaver
  • Start date Start date
C

Christopher Weaver

I would like to refer to the members of the 'sender' sent to the event
handlers. Seems like I would need to typecast it as whatever after checking
to see if in fact it is a whatever.

How would I go about that?
 
If you know what the type os (and if it isn;t its a bug)

MyType t = (MyType)sender; // this will throw

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

I would like to refer to the members of the 'sender' sent to the event
handlers. Seems like I would need to typecast it as whatever after checking
to see if in fact it is a whatever.

How would I go about that?



[microsoft.public.dotnet.languages.csharp]
 
If you know what type it should be and if it isn't its a bug

MyType t = (MyType)sender; // throws an InvalidCastException on failure
t.UseMyType();

if you're not sure

MyType t = sender as MyType;
if( t != null )
{
t.UseMyType();
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

I would like to refer to the members of the 'sender' sent to the event
handlers. Seems like I would need to typecast it as whatever after checking
to see if in fact it is a whatever.

How would I go about that?
 
This is good, but I'm not getting exactly what I'm after.

Here's what I'm trying to do:

ComboBox cb = sender as ComboBox;
odbcDA_CategoryLookUp.Fill(cb.DataSource);

I'm told (through a build error) that I can't convert from 'object' to
'System.Data.DataTable'.

This works, of course:
odbcDA_CategoryLookUp.Fill(dsCategoryLookUp);

It seems (intuitively) that I should be able to refer to one of the sender's
properties once I've cast it as the appropriate component type. I would
also like to be able to refer to the DataAdapter in the same manner, but
despite the fact that a specific DataAdapter was used to generate the
DataSet, I can't see any connection between them.

This is the way I see it presently. I'm really new to this environment, so
please correct any misconceptions. The ComboBox has a DataSource property,
but the DataSet pointed to therein doesn't specify the source of its own
data. The DataAdapter is used to stuff the DataSet specified by the
DataSource, but the objects that utilize these DataSets don't have a
property that specifies which DataAdapter should be used or is by default
associated with it.

Thanks.
 
Christopher Weaver said:
This is good, but I'm not getting exactly what I'm after.

Here's what I'm trying to do:

ComboBox cb = sender as ComboBox;
odbcDA_CategoryLookUp.Fill(cb.DataSource);

I'm told (through a build error) that I can't convert from 'object' to
'System.Data.DataTable'.
It seems (intuitively) that I should be able to refer to one of the sender's
properties once I've cast it as the appropriate component type. I would
also like to be able to refer to the DataAdapter in the same manner, but
despite the fact that a specific DataAdapter was used to generate the
DataSet, I can't see any connection between them.

You are casting sender to ComboBox. That's fine. You can now access the
DataSource property. DataSource is of type object, not of type
DataTable, hence the compiler error.
 
OK, its not complaining about accessing the DataSource property of the combobox, it complaining that the thing you are passing to Fill is typed as an object not a DataTable. Change your code to the following:

ComboBox cb = (ComboBox)sender; // you're assuming this to be the case so accept the exception if its not
DataTable dataSource = (DataTable)cb.DataSource;
odbcDA_CategoryLookUp.Fill(dataSource);

or more concisely

odbcDA_CategoryLookUp.Fill( (DataTable)((ComboBox)sender).DataSource );

although I think that is less readable ;-).

As for the relationship between the data adapter and the dataset, this is by design. The DataSet is a datasource independent cache of data. Data can de entered programatically (manually), by loading an XML document in or by using a DataAdapter.

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

This is good, but I'm not getting exactly what I'm after.

Here's what I'm trying to do:

ComboBox cb = sender as ComboBox;
odbcDA_CategoryLookUp.Fill(cb.DataSource);

I'm told (through a build error) that I can't convert from 'object' to
'System.Data.DataTable'.

This works, of course:
odbcDA_CategoryLookUp.Fill(dsCategoryLookUp);

It seems (intuitively) that I should be able to refer to one of the sender's
properties once I've cast it as the appropriate component type. I would
also like to be able to refer to the DataAdapter in the same manner, but
despite the fact that a specific DataAdapter was used to generate the
DataSet, I can't see any connection between them.

This is the way I see it presently. I'm really new to this environment, so
please correct any misconceptions. The ComboBox has a DataSource property,
but the DataSet pointed to therein doesn't specify the source of its own
data. The DataAdapter is used to stuff the DataSet specified by the
DataSource, but the objects that utilize these DataSets don't have a
property that specifies which DataAdapter should be used or is by default
associated with it.

Thanks.
 
Thanks. I get it now.

I'm trying to create on generic method for all the ComboBoxes on the form.
What I'm getting from you is that 'sender' will give me the ComboBox and the
ComboBox will give me the DataSource/DataSet but nothing's going to give me
the DataAdapter. Is that right?
 
Yes, there is no route to the data adapter unless you provide one by some other means. Do you want one so you can resync the datatable and the database?

Regards

RIchard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Thanks. I get it now.

I'm trying to create on generic method for all the ComboBoxes on the form.
What I'm getting from you is that 'sender' will give me the ComboBox and the
ComboBox will give me the DataSource/DataSet but nothing's going to give me
the DataAdapter. Is that right?
 
Yes. I want the ComboBoxes to be repopulated each time the user clicks the
dropdown. There are seven of them. In other environments I've developed
generic event handlers whose behavior would depend on the object that
invoked them. I guess I could create my own descendent / subclass of the
ComboBox, but I've never done that in C#. How would you go about this or
would you bother.

Thanks for being there. I'm just getting started in this environment and
it's a pressure cooker at times.
 
If its a common idiom then I'd be tempted to encapsulate it in a control. The approach depends on your requirements:

if you want *all* of the behavior of a combobox exposed from your control then derive from it
if you want a subset of the behavior available then create a user control, place a combobox on it and expose teh bits of functionality you want.

The derived version would look sometthing like this I guess

class MyCombo : ComboBox
{
DataAdapter da;
public MyCombo(DataAdapter da)
{
this.da = da;
}
protected override void OnDropDown(EventArgs e)
{
// put repopulating stuff here
base.OnDropDown (e);
}
}

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Yes. I want the ComboBoxes to be repopulated each time the user clicks the
dropdown. There are seven of them. In other environments I've developed
generic event handlers whose behavior would depend on the object that
invoked them. I guess I could create my own descendent / subclass of the
ComboBox, but I've never done that in C#. How would you go about this or
would you bother.

Thanks for being there. I'm just getting started in this environment and
it's a pressure cooker at times.
 
Back
Top