473,372 Members | 936 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,372 software developers and data experts.

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 7418
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.