Overload base on return value?

  • Thread starter Thread starter FrzzMan[at]vnOCzone.com
  • Start date Start date
F

FrzzMan[at]vnOCzone.com

Hello,

Thank you all about the assoc array help, now I have another problem...

I have a function that take the varname as the parameter and return the
variable value. But the type of return value is unpredictable, is there
anyway to do this?
 
...
I have a function that take the varname as the parameter
and return the variable value. But the type of return
value is unpredictable, is there anyway to do this?

You'll probably get a lot of different answers, where the bottomline is
another question:

What are you going to use the values for,
when you got them?

Anyway, as *all* types in C# inherits from System.Object, you can use a
generic construction like this:

public object ReturnValue(string varname)
{
// Assuming you have used a Hashtable ht,
// with the varname as key.

return ht[varname];
}

// Bjorn A
 
Thanks, I'll try this.

What I'm trying to do is coding a configuration class. This function
will get the variable name, find the value of that variable and its type.

Then convert the value to suitable type (as the value are always string,
it's in the XML file).

Bjorn said:
...

I have a function that take the varname as the parameter
and return the variable value. But the type of return
value is unpredictable, is there anyway to do this?


You'll probably get a lot of different answers, where the bottomline is
another question:

What are you going to use the values for,
when you got them?

Anyway, as *all* types in C# inherits from System.Object, you can use a
generic construction like this:

public object ReturnValue(string varname)
{
// Assuming you have used a Hashtable ht,
// with the varname as key.

return ht[varname];
}

// Bjorn A
 
FrzzMan,

Keep in mind that if you return a boxed value as an object the client code
will still have to cast it to the appropriate type before it can be used.
If the client code knows what types to expect anyway, this is only a
syntactic annoyance, but there is also unboxing overhead to consider.

I would lead towards creating a class with all the configuration properties
exposed, and use those properties. Something along these lines on the
client side:

// The constructor reads the config file and loads the values
AppConfiguration config = new AppConfiguration(strConfigFileName);
// Now use the values, for example:

if (config.LoggingEnabled = true) {
logger.EmailEnabled = true;
logger.EmailAddress = config.LogEmailAddress;
}

--Bob
 
Another way you could try is to override the "System.Object" members of your class. Since all classes derive from object, they're there for your use. You have the "Equals" method for comparison, the "GetHashCode" method to determine exact things and the "ToString" method to format any output.
 

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

Back
Top