[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help with Generics

Posted on 2009-04-11
7
Medium Priority
?
248 Views
Last Modified: 2012-05-06
Dear Experts,

I have a generic class as follows:

public class Presenter<T>
{
   // implementation goes here
}

I use it as follows in other classes

Presenter<IGenericView> presenter = new Presenter<IGenericView>();

I want to now write a function in a third class that can take the presenter object. Can you pleaes help me with the signature of that method should look like assuming it returns a bool and takes a Presenter object?

public bool DoSomething(Presenter myPresenter)   <--- this does not work
0
Comment
Question by:vbhargav80
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 16

Expert Comment

by:CuteBug
ID: 24121327
0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121370
Notice in this line:
Presenter<IGenericView> presenter = new Presenter<IGenericView>();

that you specify <IGenericView> or you could have specified <whatever>.  It's no different when passing parameters into a function, you can't leave that part out.
0
 

Author Comment

by:vbhargav80
ID: 24121395
Hi brandonvmoore,

Does that mean I have to write my function as

public bool DoSomething(Presenter<IGenericView> myPresenter)   ??

I do not want this. What I want is that in my presenter class I instantiate my third class which contains the method I am trying to write. This method then needs to be passed the presenter object.

SO something like this

public Presenter<T>
{
   MyClass myObject = new MyClass();
   myObject.DoSomething(this);

}

public MyClass
{
    public bool DoSomething(Presenter myPresenter) <-- I want to fix this
}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121469
When you create an instance of a generic class you have to specify what the type will be then.  If you want the flexibility you're looking for then you could just always use a type of 'object' and not even use a generic class (since the type would always be object).  And if you needed to you could cast it into other types where necessary.  My suspicion though, is that you are learning something new and trying to apply it to a problem where it isn't really needed.

Explain what you're trying to accomplish and I or someone here can probably help you find a better approach.
0
 

Author Comment

by:vbhargav80
ID: 24121504
HI brandonvmoore,

I trying to use the "Chain of Responsibility" pattern in some code that is already there.

Out team have written a sort of model view presenter pattern where the presenter takes a generic type (as explained above).

So if I have a class called MyView then to instantiate the presenter they say something like

Presenter<IMyView> presenter = new Presenter<IMyVIew>(this);

In my Presenter class now, I am writing the function to be called by the submit handler of a button (in the view). For different views, I am creating different handlers and hence want an interface

interface ISubmitHandler
{
   public bool Submit(Presenter object);
}

This is where I'm getting stuck. Hope that helps.

0
 
LVL 5

Expert Comment

by:brandonvmoore
ID: 24121626
OK, the interface is in no way related to the use of generics.  Maybe you know that, but based off your comment I wasn't sure.

Interfaces and handlers are two different things.  You would only create an interface if you were making a class that is going to be inherited by other classes and you wanted to give them the ability to write their own code for some function(s).  You probably do not need to use any interfaces.

A handler is just a function, not an interface.  What makes it a handler is that you attach it to an event.  Read up on handlers in the microsoft documentation that comes with Visual Studio as it should provide some examples.

As far as generics, I still don't know what YOUR class is trying to do.  But unless YOUR class accepts and works with various data types, then you don't need to use generics.  Just because your class is being used in a generic class doesn't mean that you  have to do anything special with generics.

Hope that helps some.  You might consider watching some of the free videos on microsoft.com on OOP in c# or do some reading on the web as there is a multitude of information on it out there.
0
 
LVL 5

Accepted Solution

by:
brandonvmoore earned 500 total points
ID: 24121638
Just FYI: What I wrote about interfaces is true, but somewhat misleading as to their true purpose.  I'll leave it to you to research what they're for though.  I'll bet wikipedia's got a good article on it.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question