jackjohnson44
asked on
c# how do I pass a generic object to a forms constructor
For some reason, this wont' work
//wont work
public FormXmlViewer(<T> objectToView)
//will work
public FormXmlViewer(string objectAsStgring)
//wont work
public FormXmlViewer(<T> objectToView)
//will work
public FormXmlViewer(string objectAsStgring)
ASKER
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();
}
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();
}
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();
}
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();
}
ASKER
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(myObje ct) //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.
So I can currently do:
string xmlString = GetStringFromObject(myObje
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.
change this:
to this:
public partial class FormXmlViewer<T> : Form
{
public FormXmlViewer(string xmlSource)
{
InitializeComponent();
}
public FormXmlViewer(T objectToView)
{
InitializeComponent();
}
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();
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, I tried that. Please see my comment above posted on 2013-05-24 at 08:25:59
look closely what i've posted, only one constructor calls InitializeComponent()
ASKER
Thanks, I didn't notice that. Can you please explain why it isn't needed? I just thought it was always required.
Public class FormXmlViewer<T> : Form{
public FormXmlViewer(T objectToView){
}
}