Solved

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

Posted on 2013-05-23
9
211 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Diagnostics with Net and Net.Sockets 2 26
Hey Audio! 3 27
FTP file download using c# 3 21
List<PaisEntity> - show some 1 24
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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

12 Experts available now in Live!

Get 1:1 Help Now