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

Overload base on return value?

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?
Nov 16 '05 #1
4 1832

"FrzzMan[at]vnOCzone.com" wrote...
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
Nov 16 '05 #2
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 Abelli wrote:
"FrzzMan[at]vnOCzone.com" wrote...

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

Nov 16 '05 #3
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

"FrzzMan[at]vnOCzone.com" <"FrzzMan[at]vnOCzone.com"> wrote in message
news:Oh**************@TK2MSFTNGP11.phx.gbl...
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).

Nov 16 '05 #4
Vas
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.
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chiller | last post by:
Ok, thanks to some good assistance/advice from people in this group I've been able to further develop my Distance class. Since previous posts I've refined my code to accept the unit measurement...
17
by: Chris | last post by:
To me, this seems rather redundant. The compiler requires that if you overload the == operator, you must also overload the != operator. All I do for the != operator is something like this: ...
10
by: Benny Raymond | last post by:
I'm trying to change the way a treeview works just a bit, i'm pretty new to C# and now i'm running into overloading. I tried the following code and it's yelling at me saying that no overload...
4
by: Alex Stevens | last post by:
Hi All, I'm writing a data access class in which I expose an SQlClient.SQLCommand's Parameters Collection via a property. How would I go about overloading the parameters.add method with one of...
9
by: Tony | last post by:
I have an operator== overload that compares two items and returns a new class as the result of the comparison (instead of the normal bool) I then get an ambiguous operater compile error when I...
5
by: Shea Martin | last post by:
I have a struct like so: struct MyStruct { public: void Value( int newValue ) { mValue = newValue; } int Value() const { return mValue; } private: int mValue;
32
by: Panks | last post by:
Hey All Its one of basic questions? i read in a book "Thinking in C++" it says " Overloading solely on return value is a bit too subtle, and thus isn't allowed in C++." on page 323. While i...
2
by: sri | last post by:
class Base { public: virtual void f(int) { std::cout<<"base.f(int)\n";}; virtual void f(std::complex<double>) { std::cout<<"derived.f \n"; }; }; class Derived : public Base {
3
by: Goran | last post by:
Hi @ all! Is it possible to overload a member variable? Example: class ClassA_t {
2
by: xtrigger303 | last post by:
Hi to all, I was reading Mr. Alexandrescu's mojo article and I've a hard time understanding the following. Let's suppose I have: //code struct A {}; struct B : A {};
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.