Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

cast of arrays with a given type

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
am72de@yahoo.de - 12 Mar 2008 17:21 GMT
Hi all,

I have to write an editor for Visual Studio. It's one editor for
different kind of Arrays: Button[], TextBox[] and so on.
The intern array is Control[], so I can use it for all of the
inherited controls.

But now I don't know how to convert a Control[] back to the given
type.

Please see the following example:

    Button[] btns = new Button[30];
    Control[] ctls = (Control[])btns;

The obvious way to cast the ctls back would be:

    Button[] btns2 = (Button[])ctls;

Does anybody know how to convert it back with only a given type?

Something like that:    Button[] btns3 = ctls as typeof(Button[]);

Thanks in advance
Andy
Martin Bonner - 12 Mar 2008 17:38 GMT
On Mar 12, 4:21 pm, am7...@yahoo.de wrote:
> Hi all,
>
[quoted text clipped - 5 lines]
> But now I don't know how to convert a Control[] back to the given
> type.

Why do you want to?  Can you not just convert the element you /
extract/ from the array?

> Please see the following example:
>
[quoted text clipped - 4 lines]
>
>         Button[] btns2 = (Button[])ctls;

... so instead of doing that, you would do:

         Button b = (Button)(ctls[i]);

> Does anybody know how to convert it back with only a given type?
>
> Something like that:    Button[] btns3 = ctls as typeof(Button[]);

I think what you mean, is that you have a function which has an
argument of type System.Type, and you want to use that to correctly
cast something.  Basically, I don't think you can, because there is no
way to declare the argument.

You have a number of choices:
1.  Just stick to the properties that are common to "Control".

2.  If the range of types is small, do a huge great switch, and handle
each one seperately (you might be able to make the body of the switch
be a function call, and generate the different functions via
generics).

3.  Do /everything/ via reflection (get properties, set properties,
call methods).  (That is going to be hard work).

4.  Generate the code at runtime, and compile it into a subsiduary
assembly.
am72de@yahoo.de - 12 Mar 2008 20:40 GMT
> On Mar 12, 4:21 pm, am7...@yahoo.de wrote:
>
[quoted text clipped - 46 lines]
> 4.  Generate the code at runtime, and compile it into a subsiduary
> assembly.

Unfortunately the editor should handle as many Controls, Buttons and
even Interfaces as possible. All works fine, except the automatic back
cast via a type.
Because the UITypeEditor.EditValue gives me only the
Propertydescriptor - and I don't want to cast every single type - I
thought there has to be a better and simpler way.
Ben Voigt [C++ MVP] - 12 Mar 2008 22:25 GMT
> Unfortunately the editor should handle as many Controls, Buttons and
> even Interfaces as possible. All works fine, except the automatic back
> cast via a type.
> Because the UITypeEditor.EditValue gives me only the
> Propertydescriptor - and I don't want to cast every single type - I
> thought there has to be a better and simpler way.

You need to use the PropertyDescriptor.SetValue and friends, there shouldn't
be any casting of the control type required (the component parameter is type
System::Object).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.