473,378 Members | 1,280 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,378 software developers and data experts.

Pass Class Property to method as parameter

dear friends ,

Is it possible to pass the class property to method as parameter ?
for e.g

Expand|Select|Wrap|Line Numbers
  1. class foo {
  2. int x;
  3.  
  4. public int number {
  5. set { set x = value;}
  6. get { return x;}
  7. }
  8.  
  9. }
  10.  
  11. public class _foo 
  12. {
  13. foo f = new foo();
  14. test(f.numer);
  15.  
  16. void test( ? ) {
  17. // get the property name here 
  18. }
  19.  
  20. }
  21.  
  22.  
Apr 10 '09 #1
3 7202
tlhintoq
3,525 Expert 2GB
Normally I would say "The best to answer that is to suggest you try it. Experimentation is the best way to learn."

But I see in your sample code that you're not sure how to pass it. So I think your question really is "How do I pass it?" more than "Is it possible?"

First: Fix how you spelled "number" on line 14. You can't expect it to work correctly if you try to pass f.numer instead of f.number.

Second: Line 16 should just expect to receive it as an int since that is how you created it in line 4
void test(int nIncomingInteger)
Apr 10 '09 #2
Dear tlhintoq

Thank you for spelling suggestion.
I think you did not get a picture.

please check this code :

Expand|Select|Wrap|Line Numbers
  1. foo f = new foo();
  2. f.number  = 13;
  3. test(f.number);
  4.  
  5. void test(int _number)
  6. {
  7.    Console.WriteLine (_number.ToString()); // result : 13
  8. }
  9.  
  10.  

BUT I am looking for code like this :


Expand|Select|Wrap|Line Numbers
  1. foo f = new foo();
  2. f.number  = 13;
  3. test(f.number);
  4.  
  5. void test( ???? )
  6. {
  7.    PropertyInfo info = foo.GetType().GetProperties();
  8.  
  9.     // for example 
  10.     string PropertyType  =  info[?????].PropertyType.ToString();
  11.    Console.WriteLine ("Name" + ????? + " : Type" +  PropertyType); 
  12.       // result : Name : number : Type :  Int32
  13.  
  14. }
  15.  
  16.  
Apr 10 '09 #3
tlhintoq
3,525 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. foo f = new foo();
  2. f.number  = 13;
  3. test(f.number);
  4.  
  5. void test( ???? )
  6. {
In your line 3 - you have indicated you are passing just the int of 'number' to the method "Test".

Expand|Select|Wrap|Line Numbers
  1. foo f = new foo();
  2. f.number  = 13;
  3. test(f.number);
  4.  
  5. void test(int IncomingInteger )
  6. {
Therefore in line 5 where you declare the method "test" you need to receive the int you are passing it.

If you want to pass the entire class object of the foo named 'f' then that is what you pass in line 3 and receive in line 5.

Expand|Select|Wrap|Line Numbers
  1. foo f = new foo();
  2. f.number  = 13;
  3. test(f);
  4.  
  5. void test( foo  IncomingFoo )
  6. {
Once you have an entire class instead of just an int from inside it, you can study it and pick apart its properties all you like. But just passing the int won't give your method any knowledge of its source, for you to do that with.
Apr 11 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

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: Laszlo Zsolt Nagy | last post by:
Hughes, Chad O wrote: > Is there any way to create a class method? I can create a class > variable like this: > Hmm, seeing this post, I have decided to implement a 'classproperty'...
1
by: Dion Heskett | last post by:
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(); }
7
by: Dave Y | last post by:
I am a newbie to C# and am having trouble trying to override a ListView property method. I have created a new class derived from the Forms.Listview and I cannot figure out the syntax to override...
6
by: | last post by:
I'm new to VS.NET, C#, and the enumerated datatype. I'm told that VS.NET 2005 Intellisense will pop up the members of an enum as a selection list if you are using the enum as a method parameter....
13
by: Maxim | last post by:
Hi! A have a string variable (which is a reference type). Now I define my Method like that: void MakeFullName(string sNamePrivate) { sNamePrivate+="Gates" }
2
by: Steve | last post by:
I'm in a weird situation where I'm using ComboBox's in a DataGrid. When the ComboBox selection changes, I'm currently storing the SelectedValue object into the DataSource of the DataGrid cell. ...
1
by: khalid sohail | last post by:
hi everybody i want to pass the window size parameter to <asp:hyperlink> attribute Navige URL Property. tell me how to pass the parameter to it.....actually i want to open the new window on the...
4
by: Etienne-Louis Nicolet | last post by:
I am working on an error handler class. Within a property set method of a given class property I'd like to pass the PropertyInfo to a function. Imagine something like: Public Class MyClass .......
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
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:
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.