473,383 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Pass Class as parameter in a method.

How can I pass a Class as a parameter to in a method ?

i.e.

Private myMethod( string pram1, Classobject as pram2)
{

Classobject.DataSource = reader;
Classobject.DataBind();
}

I wish to pass either a System.Web.UI.WebControls.DataGrid or
System.Web.UI.WebControls.Repeater class object.

Is this possible ?

Many Thanks

Dion
Nov 15 '05 #1
1 23417
Hi Dion,

You do not pass a class, you pass an instance of a class , the method would
look like :

void myMethod( string param1, DataGrid param2)
{

}

Now, even as a DataGrid and a Repeater both have a DataSource property its
parent class ( System.Web.UI.Control ) does not provide it, therefore if you
use the same function and pass an instance of a DataGrid and/or Repeater,
you should declare the second parameter as Control and then later in the
method test the type of the parameter ( you can use the "is" operator ) :
if ( param2 is System.Web.UI.DataGrid )
{
DataGrid grid = (DataGrid) param2;
grid.DataSource = ....
}
else

if ( param2 is System.Web.UI.Repeater )
{
Repeater grid = (Repeater ) param2;
grid.DataSource = ....
}

As you can see you will have to write the same code twice anyway. Now that I
think on it you could use reflection and write the code once, something like
this:
if ( param2 is System.Web.UI.Repeater || param2 is
System.Web.UI.DataGrid )
{
//using reflection assign to the property the value
}
Or you can write two methods with the second parameter beign the exact type.
Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Dion Heskett" <dh******@llewellyn.co.uk> wrote in message
news:el**************@tk2msftngp13.phx.gbl...
How can I pass a Class as a parameter to in a method ?

i.e.

Private myMethod( string pram1, Classobject as pram2)
{

Classobject.DataSource = reader;
Classobject.DataBind();
}

I wish to pass either a System.Web.UI.WebControls.DataGrid or
System.Web.UI.WebControls.Repeater class object.

Is this possible ?

Many Thanks

Dion

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Victor Ng | last post by:
I'm doing some evil things in Python and I would find it useful to determine which class a method is bound to when I'm given a method pointer. For example: class Foo(object): def...
12
by: MacFly | last post by:
Hi everyone, HRESULT WINAPI DirectPlayMessageHandler( PVOID pvUserContext, DWORD dwMessageId, PVOID pMsgBuffer) I want that method to be class member method so it could have access to class...
5
by: Dan =o\) | last post by:
given this brief scenario, is this possible somehow? class A { public SomeMethod() { } }
3
by: Brett | last post by:
I have several classes that create arrays of data and have certain properties. Call them A thru D classes, which means there are four. I can call certain methods in each class and get back an...
9
by: Martoon | last post by:
I want to instantiate an STL map with my own compare function, and I want to pass a parameter to the compare function that will be stored and used for all comparisons in that map instance. As an...
9
by: Grant Schenck | last post by:
Hello, I have a base class with several derived classes: ScriptBase + ScriptCallInbound + ScriptCallOutbound I then have another class: Line
3
by: John Dalberg | last post by:
I have seen examples for List<T>.FindAll(findthis)where findthis is a predicate. How do I pass a parameter to this predicate so that I have different values to search for? I don't want to use...
6
by: Max | last post by:
i have a event bind function like this(though it is not so robust): bind$=function(o,evt,fn,cb){ var aE='attachEvent'; var aEL='addEventListener'; if(!o&&o){ return o(evt,fn,!!cb); } return...
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.