Solved

c# how do I pass a generic object to a forms constructor

Posted on 2013-05-23
9
224 Views
Last Modified: 2013-06-06
For some reason, this wont' work

//wont work
        public FormXmlViewer(<T> objectToView)
//will work
        public FormXmlViewer(string objectAsStgring)
0
Comment
Question by:jackjohnson44
[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
  • 4
9 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39193353
Try
Public class  FormXmlViewer<T> : Form{
public FormXmlViewer(T objectToView){
}

}
0
 

Author Comment

by:jackjohnson44
ID: 39194231
Thanks.

I tried what is below and my InitializeComponent is having an error.  Is this a problem if I want two constructors, one with a string and the other with a generic?


    public partial class FormXmlViewer<T> : Form
    {
        public FormXmlViewer(string xmlSource)
        {
            InitializeComponent();
        }
        public FormXmlViewer(T objectToView)
        {
            InitializeComponent();
        }
0
 
LVL 4

Expert Comment

by:AnuTiji
ID: 39198922
Hi

Instead of passing string as parameter, we can specify it as object and check the type before doing any function with the parameter.

public FormXmlViewer(object obj)
{
     InitializeComponent();
}
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:jackjohnson44
ID: 39204418
Thanks, but I don't want to specify the objects in the function.  My current form has an xml string.  I also have a deserialize function that you pass an object and get back a string.

So I can currently do:
string xmlString = GetStringFromObject(myObject)  //this uses generics and works with many objects
Form myForm(xmlString)

In this scenario I never have to know the type of object.  I want to avoid having to do this in two steps and also have to edit the form for each new object.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39204446
change this:
  public partial class FormXmlViewer<T> : Form
    {
        public FormXmlViewer(string xmlSource)
        {
            InitializeComponent();
        }
        public FormXmlViewer(T objectToView)
        {
            InitializeComponent();
        }

Open in new window


to this:

 
 public partial class FormXmlViewer<T> : Form
    {
        public FormXmlViewer(string xmlSource)
        {
            //deserialize xmlSource to T first, then call the 2nd ctor with the object.

        }
        public FormXmlViewer(T objectToView)
        {
            InitializeComponent();
        }

Open in new window

0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39204447
u can use this 2 functions to deserialize<T> from xmlsource:
public static T XmlDeserializeFromString<T>(string objectData)
{
    return (T)XmlDeserializeFromString(objectData, typeof(T));
}

public static object XmlDeserializeFromString(string objectData, Type type)
{
    var serializer = new XmlSerializer(type);
    object result;

    using (TextReader reader = new StringReader(objectData))
    {
        result = serializer.Deserialize(reader);
    }

    return result;
}
 

Open in new window


so u ended up with:
public partial class FormXmlViewer<T> : Form
    {
        public FormXmlViewer(string xmlSource)
        {
            //deserialize xmlSource to T first, then call the 2nd ctor with the object.
FormXmlViewer(XmlDeserializeFromString<T>(xmlSource))
        }
        public FormXmlViewer(T objectToView)
        {
            InitializeComponent();
        }

Open in new window

0
 

Author Comment

by:jackjohnson44
ID: 39204526
Thanks, I tried that.  Please see my comment above posted on 2013-05-24 at 08:25:59
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39204565
look closely what i've posted, only one constructor calls InitializeComponent()
0
 

Author Comment

by:jackjohnson44
ID: 39204582
Thanks, I didn't notice that.  Can you please explain why it isn't needed?  I just thought it was always required.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…

740 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