Return value of method

G

Guest

Hello. I have method (called GetKey) in a class that returns a string.
Also, I have in another class, a method which makes a call to the GetKey
method. What I wish to do is to do an evaluation of the return value of the
GetKey method, within this 2nd class. My question is, should I create a
field/property and assign it the value of the return value in GetKey class?
What you suggest? Here is some fake code so far

Class GetKey
{
public string GetKeyMethod(string myParam)
{
....
.....
return myKey
}

Class Myclass
{
GeyKey gk = new GetKey()
gk.GetKey(someParam)

//MUST NOW DO EVAL OF myKey,,, IF (blah blah blah)
 
N

Nicholas Paldino [.NET/C# MVP]

Toco,

What do you mean an evaluation of what is returned? Do you mean you
have code in there which you want to run? If so, you need to find some
other way to do this, as C# doesn't really lend itself to interpretation on
the fly (yes, there are methods to do this, but in reality, it's not worth
it).
 
J

Jon Skeet [C# MVP]

Toco said:
Hello. I have method (called GetKey) in a class that returns a string.
Also, I have in another class, a method which makes a call to the GetKey
method. What I wish to do is to do an evaluation of the return value of the
GetKey method, within this 2nd class. My question is, should I create a
field/property and assign it the value of the return value in GetKey class?
What you suggest? Here is some fake code so far

Class GetKey
{
public string GetKeyMethod(string myParam)
{
...
....
return myKey
}

Class Myclass
{
GeyKey gk = new GetKey()
gk.GetKey(someParam)

//MUST NOW DO EVAL OF myKey,,, IF (blah blah blah)

So do:

string key = gk.GetKey(someParam);

if (key....)
{
...
}

Basically, the code you showed discards the return value of the method,
whereas you want to "save" it in a variable.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Toco said:
Hello. I have method (called GetKey) in a class that returns a string.
Also, I have in another class, a method which makes a call to the GetKey
method. What I wish to do is to do an evaluation of the return value of
the
GetKey method, within this 2nd class. My question is, should I create a
field/property and assign it the value of the return value in GetKey
class?
What you suggest? Here is some fake code so far

Your post is not very clear, so maybe more details will help you get a
better answer.

If you are asking if you should create a property in GetKey the answer is
not, the way you have it now is the correct one.

Calling the method (which you would have to change to return void ) and
forcng the calling code use a property to get the return value is not a good
idea. It's a bad design even.

Also if GetKeyMethod does not use any instance variable declared in GetKey I
suggest do it static.

Also I would suggest you to change the name GetKey is not very intuitive.
 

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