Solved

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

Posted on 2013-05-23
9
228 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
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!

 

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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 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