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

R

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
 
C

Cor Ligthert [MVP]

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
 
R

Rajneesh Mishra

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
 
C

Cor Ligthert [MVP]

Rajneesh,

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

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

I hope this helps,

Cor
 
P

Phill. W

Ram said:
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.
 
J

Jay B. Harlow [MVP - Outlook]

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/d.../cpcondiscoveringtypeinformationatruntime.asp

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

http://msdn.microsoft.com/library/d...n-us/cpref/html/frlrfsystemcomponentmodel.asp

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

--
Hope this helps
Jay
T.S. Bradley - http://www.tsbradley.net


| 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
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top