By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,665 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,665 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.