Tuesday, 27 August 2013

Accessing parameters from 'object' type in C#

Accessing parameters from 'object' type in C#

I am converting a VB.Net application to C# (as well as learning C#) and I
ran into a problem. One of the functions takes in an object and applies
modifications to certain parameters based on what is passed. This way, one
function can be used to update any control passed to it, which is working
fine in VB.
The full function has a lot more logic behind it, but here is a scaled
back version showing the basics:
public void TransformObject(object objObject, int LeftPadding, int
TopPadding, int WidthChange, int HeightChange)
{
objObject.Top = TopPadding;
objObject.Left = LeftPadding;
objObject.Width = WidthChange;
objObject.Height = HeightChange;
}
The problem is that 'Top', 'Left', 'Width', 'Height', etc. are not
defined, since it is using the object type.
Is there a way to keep the existing structure without having to create a
separate function or definition for each possible control type?

No comments:

Post a Comment