473,473 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

passing any object as parameter to a function which takes Object class as parameter and fetching the members at runtime

Ram
Hi Friends

I want to develope a custom control in .net which can be used with any
project. I am writing a function in that class which I want to take any
object as parameter. For that I have used Object class as parameter.
Now it can take any object as its parameter. But the problem is that I
want to access the values of the private or public member variables of
the passed object, for which I may have to typecast the Object class
variable into appropriate class but I can't do so coz I dont't have the
dll of the classes whose object will be passed to the function.

how can I do that.

Let me clarify the situation.

In the control I have a function like Script(Object obj)

public void Script(Object obj)
{

}

Now suppose there is a class called Mail which may be defined in any
assembly I don't know.

class mail
{

public MailId;
public Name;
}

now suppose the object of this class is passed to the Function script.

Now how can I access the values of MailId and Name in my function
script as I dont't have anyway to typecast the Object class into mail
class coz I don't have the dll for the mail class.

Thanks in advance.
Ram

Nov 21 '05 #1
5 7424
Ram,
I am not sure if you want code in C# or JavaScript. However this is a VBNet
newsgroup than in my idea not the proper place. Can you explain us a little
bit more why you have chosen this newsgroup to ask your question.

Cor
"Ram" <ra***************@yahoo.co.in> schreef in bericht
news:11*********************@g44g2000cwa.googlegro ups.com...
Hi Friends

I want to develope a custom control in .net which can be used with any
project. I am writing a function in that class which I want to take any
object as parameter. For that I have used Object class as parameter.
Now it can take any object as its parameter. But the problem is that I
want to access the values of the private or public member variables of
the passed object, for which I may have to typecast the Object class
variable into appropriate class but I can't do so coz I dont't have the
dll of the classes whose object will be passed to the function.

how can I do that.

Let me clarify the situation.

In the control I have a function like Script(Object obj)

public void Script(Object obj)
{

}

Now suppose there is a class called Mail which may be defined in any
assembly I don't know.

class mail
{

public MailId;
public Name;
}

now suppose the object of this class is passed to the Function script.

Now how can I access the values of MailId and Name in my function
script as I dont't have anyway to typecast the Object class into mail
class coz I don't have the dll for the mail class.

Thanks in advance.
Ram

Nov 21 '05 #2

Hi I want to develope this in C#. And even if u can give me some idea
how to do it in VB.net, it is welcomed coz I just want the basic idea.

Thanks
*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #3
Rajneesh,

\\\
Public sub mySub(byvalue a as Object)
messagebox.show(a.ToString)
End Sub

mySub("I hope this helps")
///

I hope this helps,

Cor
Nov 21 '05 #4
"Ram" <ra***************@yahoo.co.in> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
I am writing a function in that class which I want to take any object
as parameter. But the problem is that I want to access the values of
the private or public member variables of the passed object


In order to use any properties or methods on any object, your
application must know about the Type that defines that object.

Since you application doesn't (and can't) know this, you can't do
what you're trying to do.

You /might/ be able to define an Interface that defines the properties
and methods that you want to work with and have your "other"
classes implement this Interface.
Then, something like this will work :

Public Interface ImyInterface
Property ID As Integer
Property Name As String
End Interface

Sub Script( ByVal thing As ImyInterface )
MsgBox thing.ID
End Sub

HTH,
Phill W.
Nov 21 '05 #5
Ram,
In addition to the other comments.

| Now how can I access the values of MailId and Name in my function
| script as I dont't have anyway to typecast the Object class into mail
| class coz I don't have the dll for the mail class.

Does the Script function know specifically that the object passed with have
a MailId & a Name, if it does it sounds like the Script function knows
something about the type of the Object parameter. In which case I would pass
either a base class or an Interface to the Script function.

If the Script function doesn't know specifically that the object passed has
a MailId & a Name, instead it is told that the object might have a MailId, a
Name, or a Thingamajig. Then I would use " late binding", as in one of:
Reflection, Component Model, CallByName, or Option Strict Off. Within VB.NET
CallByName & Option Strict Off are by far the "easiest"!

FWIW: DataBinding within Windows Forms is based on the Component Model.

The following might be a good spot to start on Reflection:

http://msdn.microsoft.com/library/de...natruntime.asp

The following might be a good spot to start on the Component Model:

http://msdn.microsoft.com/library/de...onentmodel.asp

I would recommend a base class or interface over late binding.

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net
"Ram" <ra***************@yahoo.co.in> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
| Hi Friends
|
| I want to develope a custom control in .net which can be used with any
| project. I am writing a function in that class which I want to take any
| object as parameter. For that I have used Object class as parameter.
| Now it can take any object as its parameter. But the problem is that I
| want to access the values of the private or public member variables of
| the passed object, for which I may have to typecast the Object class
| variable into appropriate class but I can't do so coz I dont't have the
| dll of the classes whose object will be passed to the function.
|
| how can I do that.
|
| Let me clarify the situation.
|
| In the control I have a function like Script(Object obj)
|
| public void Script(Object obj)
| {
|
| }
|
| Now suppose there is a class called Mail which may be defined in any
| assembly I don't know.
|
| class mail
| {
|
| public MailId;
| public Name;
| }
|
| now suppose the object of this class is passed to the Function script.
|
| Now how can I access the values of MailId and Name in my function
| script as I dont't have anyway to typecast the Object class into mail
| class coz I don't have the dll for the mail class.
|
| Thanks in advance.
| Ram
|
Nov 21 '05 #6

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

Similar topics

5
by: Newsgroup - Ann | last post by:
Gurus, I have the following implementation of a member function: class A { // ... virtual double func(double v); void caller(int i, int j, double (* callee)(double)); void foo() {caller(1,...
5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
10
by: Patrick Stinson | last post by:
What sort of operations can one do with classes? I know class A{}; typeid(A); works, but what else can you do? Is there a way to pass a class as a parameter to a function? class pointers? how...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
3
by: dice | last post by:
Hi, In order to use an external api call that requires a function pointer I am currently creating static wrappers to call my objects functions. I want to re-jig this so I only need 1 static...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
10
by: sumsin | last post by:
The C++ Object Model book says that 'Nonstatic data members are allocated directly within each class object. Static data members are stored outside the individual class object. Static and...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.