4 Replies - 353 Views - Last Post: 05 February 2013 - 01:44 PM Rate Topic: -----

#1 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

How to pass a class as an argument of a function?

Posted 05 February 2013 - 11:21 AM

Hi there, currently I'm creating a new sub-window once, in the body of the main window constructor:
PopUp hey = new PopUp();


However, for my needs, this needs to be done in another function along with some other things; more importantly, this function has to be able to create several different sub-windows(e.g. PopUp, HelpWindow, EnterSomeInfo, etc.). I tried this:
        public CreateWindow( Form which)
        {
            which frm = new which();
//....


but I get

Quote

The type or namespace 'which' could not be found. Are you missing a using directive or an assembly reference?

I followed these two links:
http://en.csharp-onl...embly_reference
http://blogs.msdn.co.../29/123028.aspx
but they didn't help me much as I need 'which' to be able to refer to several different forms.
Changing
public CreateWindow( Form which)

to
public CreateWindow( class which)

or
public CreateWindow( object which)

also throws similar errors. How can I pass a class(or rather, form) as an argument of a function?

Is This A Good Question/Topic? 0
  • +

Replies To: How to pass a class as an argument of a function?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5582
  • View blog
  • Posts: 11,941
  • Joined: 02-June 10

Re: How to pass a class as an argument of a function?

Posted 05 February 2013 - 11:38 AM

Quote

        public CreateWindow( Form which)
        {
            which frm = new which();
//....




You need to STOP. And then read (or re-read) your textbook.

You are WAY lost. You're not going to be able to build anything until you understand the foundation concepts.

"which" is the instance of a form that is being passed into your CreateWindow method.

Unless you have defined a class that is *also* called which, you are trying to assign your existing instance to something that doesn't exist.

None of this makes any sense for several reasons.
1 - if you are passing in an instance of a form then why would you immediately replace it with a new form of some type
2 - you are trying to assign it to a type that doesn't exist.

Look in my signature block. There are links to tutorials for classes and objects. They might help. But honestly, you need to back up at least 2 chapters in your book and re-work those lessons until you fully comprehend the material. Don't move forward just because you somehow got something working. If you don't actually understand it, then don't move forward.


My standard beginner resources post - Updated JAN 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,639
  • Joined: 24-September 10

Re: How to pass a class as an argument of a function?

Posted 05 February 2013 - 11:54 AM

you want to reference a Class, then create an instance based off the Class you have referenced. Dynamically constructing objects with out exact knowledge of the type it will be.

This is called reflection, and can be done with the Activator class:

public Form CreateForm(Type classType)
{
     if(!typeof(Form).IsAssignableFrom(classType)) throw new ArgumentException("Supplied type must inherit from Form");

     return System.Activator.CreateInstance(classType) as Form;
}



Note CreateInstance does allow for an object array to pass in parameters if you need to with a constructor that requires parameters.





Now that I've answered your question. Why do you need this? This isn't really considered an appropriate method for creating objects, but can be useful if your design requires it. But before implementing it you need to ask yourself if your design is the right design int he first place.
Was This Post Helpful? 2
  • +
  • -

#4 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

Re: How to pass a class as an argument of a function?

Posted 05 February 2013 - 12:06 PM

Well, I'm not that novice. FWIW I've already written a program (~4k lines long) that works. Now that I read your explaination I do understand the siliness of my try.
I needn't 'which' to be an instance of a form. Rather, I've been trying to create a shorthand of this thing:
        public createWindow( int which)
        {
            switch (which)
            {
                case 1: PopUp mywindow = new PopUp(); break;
                case 2: HelpWindow mywindow = new HelpWindow(); break;
                case 3: EntersomeInfo mywindow = new EntersomeInfo(); break;
                case 4: Options mywindow = new Options(); break;
//...
            }
//...


Do you know how could this be done? Thanks in advance.

update: I was answered while replying.

Quote

Now that I've answered your question. Why do you need this? This isn't really considered an appropriate method for creating objects, but can be useful if your design requires it. But before implementing it you need to ask yourself if your design is the right design int he first place.

Well, the main window of the program is supposed to work with one out of several different classes at a time, each of which inherits from an abstract class. And each class requires a set of subwindows that would appear in the main window(the main window is something like MDI).
The program then needs to be able to switch between instances the classes it is working with, destroying one and creating another in its place.
Now, the calling of a subwindow requires some settings that go along with it, which are the same. So instead of
PopUp mywindow = new PopUp();
mywindow.settings = whatever;
mywindow.settings2 = whatever;
mywindow.settings3 = whatever;
HelpWindow mywindow2 = new HelpWindow();
mywindow2.settings = whatever;
mywindow2.settings2 = whatever;
mywindow2.settings3 = whatever;
EntersomeInfo mywindow3 = new EntersomeInfo();
mywindow3.settings = whatever;
mywindow3.settings2 = whatever;
mywindow3.settings3 = whatever;
Options mywindow4 = new Options();
mywindow4.settings = whatever;
mywindow4.settings2 = whatever;
mywindow4.settings3 = whatever;
// five or so more windows to come


I was trying to do this:

mywindow = createWindow(PopUp);
mywindow2 = createWindow(HelpWindow);
mywindow3 = createWindow(EnterSomeInfo);
mywindow4 = createWindow(Options);
// five or so more windows to come


Was This Post Helpful? 0
  • +
  • -

#5 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2538
  • View blog
  • Posts: 4,639
  • Joined: 24-September 10

Re: How to pass a class as an argument of a function?

Posted 05 February 2013 - 01:44 PM

Activator should do the job.

I'm not saying your design is bad. I actually have a similarish thing going on in our product at work. I call it my 'FormManager' which can receive keys for the kind of form to be opened. I then have XML that defines the entries for a menu (there's tons, a big piece of software), and each entry references the key it deals with (I say key because the key contains both the form to be opened, but also other info to go with it... depending on the form). Then I have multiple menu systems that can consume the XML, this way the user can select from multiple menu styles.

The keys also play other roles, such as defining tasks that consist of going through multiple forms in succession in a specific order.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1