Home All Groups Group Topic Archive Search About

How to get reference to instance of a generic object type

Author
28 Dec 2006 7:18 PM
Luke Martz
I have some code where I pass in control references as a generic Object,
instead of the actual type of control.

i.e.  void SetWidth (object ControlToSet)
{ object.width = 50}
....

I understand how I can get the properties of a generic object, and even see
the Name of the instance that the object is.

I can even get the type of the object.

But how do I actually reference the instance that the object is holding, so
I can reference the properties of the instance?

Thanks.

Author
28 Dec 2006 10:46 PM
Alien2_51
I'm not sure if this is what you are looking for or not, if I understand you
correctly you have some generic functions that manipulate an object and you
want the actual instane to be updated rather than a copy, I found this in VS
Help..

Example 2: Passing Value Types by Reference

The following example is the same as Example 1, except for passing the
parameter using the ref keyword. The value of the parameter is changed after
calling the method.

// PassingParams2.cs

using System;

class PassingValByRef

{

    static void SquareIt(ref int x)

    // The parameter x is passed by reference.

    // Changes to x will affect the original value of myInt.

    {

        x *= x;

        Console.WriteLine("The value inside the method: {0}", x);

    }

    public static void Main()

    {

        int myInt = 5;

        Console.WriteLine("The value before calling the method: {0}",

           myInt);

        SquareIt(ref myInt);   // Passing myInt by reference.

        Console.WriteLine("The value after calling the method: {0}",

           myInt);

    }

}


Show quote
"Luke Martz" wrote:

> I have some code where I pass in control references as a generic Object,
> instead of the actual type of control.
>
> i.e.  void SetWidth (object ControlToSet)
> { object.width = 50}
> ...
>
> I understand how I can get the properties of a generic object, and even see
> the Name of the instance that the object is.
>
> I can even get the type of the object.
>
> But how do I actually reference the instance that the object is holding, so
> I can reference the properties of the instance?
>
> Thanks.
Author
28 Dec 2006 11:00 PM
Luke Martz
I am able to pass by ref if the function I am passing to has the same type
for the input, like your example.

In my situation I am sometimes passing in a label, or textbox control.  Both
have a Width property for that type of control.  I don't want to define each
type as a parameter, so I cast the label or textbox control as Object, and
pass that in.  However I am unable to access the property Width of the Object
I passed in.  What I want to do is verify that the Object I passed in
actually has a Width Property and then I can set it.

Thanks for the help, and anymore insights you may have.

Show quote
"Alien2_51" wrote:

> I'm not sure if this is what you are looking for or not, if I understand you
> correctly you have some generic functions that manipulate an object and you
> want the actual instane to be updated rather than a copy, I found this in VS
> Help..
>
> Example 2: Passing Value Types by Reference
>
> The following example is the same as Example 1, except for passing the
> parameter using the ref keyword. The value of the parameter is changed after
> calling the method.
>
> // PassingParams2.cs
>
> using System;
>
> class PassingValByRef
>
> {
>
>     static void SquareIt(ref int x)
>
>     // The parameter x is passed by reference.
>
>     // Changes to x will affect the original value of myInt.
>
>     {
>
>         x *= x;
>
>         Console.WriteLine("The value inside the method: {0}", x);
>
>     }
>
>     public static void Main()
>
>     {
>
>         int myInt = 5;
>
>         Console.WriteLine("The value before calling the method: {0}",
>
>            myInt);
>
>         SquareIt(ref myInt);   // Passing myInt by reference.
>
>         Console.WriteLine("The value after calling the method: {0}",
>
>            myInt);
>
>     }
>
> }
>
>
> "Luke Martz" wrote:
>
> > I have some code where I pass in control references as a generic Object,
> > instead of the actual type of control.
> >
> > i.e.  void SetWidth (object ControlToSet)
> > { object.width = 50}
> > ...
> >
> > I understand how I can get the properties of a generic object, and even see
> > the Name of the instance that the object is.
> >
> > I can even get the type of the object.
> >
> > But how do I actually reference the instance that the object is holding, so
> > I can reference the properties of the instance?
> >
> > Thanks.
Author
28 Dec 2006 11:26 PM
Alien2_51
I'd probably write an overload for each type, seems redundant I know but
keeps things moving along.. You might take a look at the PropertyDescriptor
class, here's an example of a sort function I've used, it's VB but should
give you an idea of how to use it.

    Public WriteOnly Property Sort() As String
        Set(ByVal Value As String)
            Dim str() As String = Split(Value, " ")
            Dim mSortPropertyName As String = "", mSortDirection As String =
""
            'parse the input string

            If str.Length = 1 Then 'direction not specified, use default
                mSortPropertyName = str(0).Trim
            ElseIf str.Length = 2 Then

                mSortPropertyName = str(0).Trim & ""
                mSortDirection = str(1).Trim & ""

            Else
                Throw New ApplicationException("Too many parameters
specified for the sort.")

            End If
            Dim childType As Type = GetType(SalesPersonInfo)
            Dim mSortProperty As PropertyDescriptor = _

TypeDescriptor.GetProperties(childType).Item(mSortPropertyName)

            Select Case mSortDirection.ToUpper.Trim
                Case Is = "DESC"
                    DirectCast(Me, IBindingList).ApplySort(mSortProperty,
ListSortDirection.Descending)
                Case Is = "ASC"
                    DirectCast(Me, IBindingList).ApplySort(mSortProperty,
ListSortDirection.Ascending)
                Case Else

                    DirectCast(Me, IBindingList).ApplySort(mSortProperty,
ListSortDirection.Ascending)

            End Select

        End Set

    End Property



Show quote
"Luke Martz" wrote:

> I am able to pass by ref if the function I am passing to has the same type
> for the input, like your example.
>
> In my situation I am sometimes passing in a label, or textbox control.  Both
> have a Width property for that type of control.  I don't want to define each
> type as a parameter, so I cast the label or textbox control as Object, and
> pass that in.  However I am unable to access the property Width of the Object
> I passed in.  What I want to do is verify that the Object I passed in
> actually has a Width Property and then I can set it.
>
> Thanks for the help, and anymore insights you may have.
>
> "Alien2_51" wrote:
>
> > I'm not sure if this is what you are looking for or not, if I understand you
> > correctly you have some generic functions that manipulate an object and you
> > want the actual instane to be updated rather than a copy, I found this in VS
> > Help..
> >
> > Example 2: Passing Value Types by Reference
> >
> > The following example is the same as Example 1, except for passing the
> > parameter using the ref keyword. The value of the parameter is changed after
> > calling the method.
> >
> > // PassingParams2.cs
> >
> > using System;
> >
> > class PassingValByRef
> >
> > {
> >
> >     static void SquareIt(ref int x)
> >
> >     // The parameter x is passed by reference.
> >
> >     // Changes to x will affect the original value of myInt.
> >
> >     {
> >
> >         x *= x;
> >
> >         Console.WriteLine("The value inside the method: {0}", x);
> >
> >     }
> >
> >     public static void Main()
> >
> >     {
> >
> >         int myInt = 5;
> >
> >         Console.WriteLine("The value before calling the method: {0}",
> >
> >            myInt);
> >
> >         SquareIt(ref myInt);   // Passing myInt by reference.
> >
> >         Console.WriteLine("The value after calling the method: {0}",
> >
> >            myInt);
> >
> >     }
> >
> > }
> >
> >
> > "Luke Martz" wrote:
> >
> > > I have some code where I pass in control references as a generic Object,
> > > instead of the actual type of control.
> > >
> > > i.e.  void SetWidth (object ControlToSet)
> > > { object.width = 50}
> > > ...
> > >
> > > I understand how I can get the properties of a generic object, and even see
> > > the Name of the instance that the object is.
> > >
> > > I can even get the type of the object.
> > >
> > > But how do I actually reference the instance that the object is holding, so
> > > I can reference the properties of the instance?
> > >
> > > Thanks.
Author
29 Dec 2006 4:06 AM
Dave Sexton
Hi Luke,

In WinForms all controls derive from a common base class:
System.Windows.Forms.Control, which derives from
System.ComponentModel.Component, which derives from
System.MarshalByRefObject, which derives from System.Object.

The inheritance chain for the Control class can be found here:

Control Class [WinForms]
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.aspx

The Control class provides the Width property for all of its derived
classes, so if it's just the Width that you want to use then accept a
Control argument, which in this case is the least common denominator that
provides the functionality you want:

private void SetWidth(Control control)
{
    control.Width = 50;
}

If you're writing an ASP.NET application then you can use the
System.Web.UI.WebControls.WebControl class instead, which also provides a
Width property for its derived classes:

WebControl Class
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.aspx

BTW, you shouldn't use the term "generic" in the manner in which you have
since it's an overloaded term that has a specific meaning in the 2.0
framework. e.g., "C# Generics".  Instead of referring to an object as a
"generic object" just refer to it as an "object", which everyone knows is
"generic" as in your sense of the term, since all types ultimately derive
from System.Object :)

Show quote
"Luke Martz" <LukeMa***@discussions.microsoft.com> wrote in message
news:5920F92C-BCFF-4C77-9A6A-A4074972FE56@microsoft.com...
>I have some code where I pass in control references as a generic Object,
> instead of the actual type of control.
>
> i.e.  void SetWidth (object ControlToSet)
> { object.width = 50}
> ...
>
> I understand how I can get the properties of a generic object, and even
> see
> the Name of the instance that the object is.
>
> I can even get the type of the object.
>
> But how do I actually reference the instance that the object is holding,
> so
> I can reference the properties of the instance?
>
> Thanks.

AddThis Social Bookmark Button