Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If I pass parameters to a "Method A" and within this method call another "Method B" is not passed parameters. As I can get the values ​​of the parameters of the method of "A" in the "B " method.


---------------------------------------------------------------------------------

C#
public class A{

	public void Method_A(string name){
	
		var b = new B();
		
		b.Method_B();
	}
}

public class B{

	public void Method_B(){	
	/*
		see parameter values of Method_A	
	*/	
	}
}

---------------------------------------------------------------------------------
Posted
Updated 20-Feb-15 2:59am
v5
Comments
phil.o 20-Feb-15 8:44am    
Totally unclear. Please improve your question with relevant code fragment.
phil.o 20-Feb-15 8:54am    
Still you did not provide any code fragment illustrating your question.
Thanks7872 20-Feb-15 8:56am    
If i get it correctly, you can use reference variables. Have a look at it in Google.com
[no name] 20-Feb-15 9:06am    
What is the reason you don't like/can pass a Parameter also to Method_B in class B?
elnichu 23-Feb-15 9:38am    
The code is already done and I wanted a generic form of log errors

That's a terrible idea. I could write Method_C and call Method_B from it. How can you expect Method_B to be able to use whatever parameters Method_C has? Or would you want to somehow detect whether or not Method_B was called from Method_A and throw an execption if it wasn't?



Method_A would have to pass it's parameters to Method_B.

Have you considered having class A pass the equivalent of Method_B to class B as a delegate? Or maybe class B needs an event?
 
Share this answer
 
I'm trying to express your question in valid c# code:
C#
void MethodA(int param) {
   MethodB();
}

void MethodB() {
   // You cannot access MethodA's parameter here
}


Solution:
- Pass the parameter to MethodB (but that means modifying MethodB's signature):
C#
void MethodA(int param) {
   MethodB(param);
}

void MethodB(int param) {
   // Here you can access param's value
}

OR
- Use a global variable outside of your methods:
C#
int _param;

void MethodA(int param) {
   _param = param;
   MethodB();
}

void MethodB() {
   // Here you can access _param which has been initialized with MethodA's parameter
}
 
Share this answer
 
Comments
elnichu 20-Feb-15 9:09am    
This is could solve a through AOP ?
PIEBALDconsult 20-Feb-15 9:25am    
No.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900