Solved

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

Posted on 2013-05-23
9
206 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
  • 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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now