Author Topic: C# - New "instance" of object type?  (Read 3546 times)

0 Members and 1 Guest are viewing this topic.

pkohut

  • Guest
C# - New "instance" of object type?
« on: February 17, 2009, 02:00:50 PM »
Passing a parameter of "Type" to a function, how can a new instance of
that type be created?  Example, form has many buttons, when one is pressed
it swaps out the current view control with one associated with the button
that was pressed.

There is 20 or so buttons in all and the layout isn't locked down (could change as
this is a first project with Silverlight / WPF and I'm just playing around at this point).

Since the layout could change I'm trying to keep all the logic in just a couple functions
instead of spread throughout all the button event handlers.

Thanks,
Paul

Current method (works)
Code: [Select]
void SwitchView(UserControl newView)
{
    MainViewCtrl.Children.Clear();
    newView.Margin = new Thickness(4);
    MainViewCtrl.Children.Add(newView);
}

bool IsCurrentViewSameAs(Type ctrl)
{
    UserControl uc = (UserControl)MainViewCtrl.Children[0];
    if (uc.GetType() == ctrl)
        return true;
    return false;
}

void Btn1_Click(object sender, RoutedEventArgs e)
{
    if (IsCurrentViewSameAs(typeof(ctrlView1)) == true)
        return;
    SwitchView(new ctrlView1());
}

void Btn2_Click(object sender, RoutedEventArgs e)
{
    if (IsCurrentViewSameAs(typeof(ctrlView2)) == true)
        return;
    SwitchView(new ctrlView2());
}

Would like to somehow do something like the this?
Code: [Select]
void SwitchView(Type newViewType)
{
    UserControl uc = (UserControl)MainViewCtrl.Children[0];
    if (uc.GetType() == ctrl)
        return;

    MainViewCtrl.Children.Clear();
    newView.Margin = new Thickness(4);
    MainViewCtrl.Children.Add( ??????????? ); // <<-- Hm, how to create a new instance
                                                            // of type newViewType ???
}

void Btn1_Click(object sender, RoutedEventArgs e)
{
    SwitchView(typeof(ctrlView1));
}

void Btn2_Click(object sender, RoutedEventArgs e)
{
    SwitchView(typeof(ctrlView2);
}

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8805
  • AKA Daniel
Re: C# - New "instance" of object type?
« Reply #1 on: February 17, 2009, 02:10:19 PM »
maybe Activator::CreateInstance

bikelink

  • Guest
Re: C# - New "instance" of object type?
« Reply #2 on: February 17, 2009, 02:14:39 PM »
yes... createinstance
also see "default" operator

Spike Wilbury

  • Guest
Re: C# - New "instance" of object type?
« Reply #3 on: February 17, 2009, 02:23:23 PM »
like you I also playing with c# and many of the new derivatives out there - but I am still consider myself neophyte


I have been in this place, see if helps:

http://blois.us/blog/2008_04_08_houseomirrors_archive.html

pkohut

  • Guest
Re: C# - New "instance" of object type?
« Reply #4 on: February 17, 2009, 02:27:21 PM »
Thanks guys,
CreateInstance worked, very cool!

BTW, I looked at the operator list earlier and passed right over
"default" because it didn't ring any bells.

Paul

pkohut

  • Guest
Re: C# - New "instance" of object type?
« Reply #5 on: February 17, 2009, 02:36:00 PM »
Good link.  I just installed the Silverlight SDK Saturday night and figured I would try
and port one of my current projects to it.  So far all I've done is the UI layout and
a small bit of code.  Very impressed with what SL can do and the easy at which it
can be done.

Paul

like you I also playing with c# and many of the new derivatives out there - but I am still consider myself neophyte


I have been in this place, see if helps:

http://blois.us/blog/2008_04_08_houseomirrors_archive.html

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8805
  • AKA Daniel
Re: C# - New "instance" of object type?
« Reply #6 on: February 17, 2009, 02:49:18 PM »
Paul, Did you quit the native code?

pkohut

  • Guest
Re: C# - New "instance" of object type?
« Reply #7 on: February 17, 2009, 03:42:11 PM »
Not yet.  I've been looking at Qt and wxWidgets as possible frameworks
to program against, and with 4.5 of Qt going LGPL it is looking more and
more viable.

Someone here (I think) recently mentioned WPF was cool and they wouldn't
go back to WinForms, which got me looking at that as well.

Anyways, heres the UI I've put together so far with Silverlight more or less
duplicating the UI of http://www.pkautomation.com/wgs84_geodesic_calc.php
which is done in MFC.

Paul

ps. I tested that web page in IE 6 and it works on all my machines and VM's
except for one.  Don't know why, so if you have probblems with the page or
pages try hitting the refresh button F5.

Paul, Did you quit the native code?


It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8805
  • AKA Daniel
Re: C# - New "instance" of object type?
« Reply #8 on: February 17, 2009, 03:58:32 PM »
Wow! Nice work  8-)

pkohut

  • Guest
Re: C# - New "instance" of object type?
« Reply #9 on: February 17, 2009, 04:42:47 PM »
Thanks.

Hooking up this UI in .Net is SO EASY and quick to do.  Must be doing
something wrong  ^-^

Paul

Wow! Nice work  8-)