473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting values automatically

Hi,

I have the lKey object that can be used to enable different operation
systems such
Windows 2000
Linux
FreeBSD
etc.

Now I want to read a database table that contains key/values and if the key
is "OSTypes", I like to set the appropraite OS (more than one OS can be
set).

lKey.Enable(Com pany.OSTypes.Re dhat);
lKey.Enable(Com pany.OSTypes.Wi ndows);
....

I really don't want to use IF and SWITCH for every possible value. Is it
possible to set the ??? in lKey.Enable(Com pany.OSTypes.?? ??) to whatever
value read from the database?

Thank you
Maz.
Nov 17 '05 #1
1 1133
I can think of two ways to do this. The wrong way and the right way.
;-)

You could use Reflection to search for a name in the OSTypes
enumeration that matches the string in your database table. This is
what I call the "wrong way" because it ties your type names to your
database contents. If you change your database representation, or
rename an enum, your program will break. (Imagine, for example, that
Redhat is bought out by Microsoft and renamed Microhat. How are you
going to change your program enum if it's locked into your database
contents?)

You could also take a much more low-tech approach, which for me is the
"right way": simply create a static read-only hash table that maps the
database string (the key) to the matching enum value (the value). Then
you can just do this:

string os = (string)dbTable["OS_TYPE"];
IKey.Enable((Co mpany.OSTypes)O sTypeMapping[os]);

For future expansion purposes, you could have your hash table store a
class that contain the O/S type and a flag indicating whether this
mapping is the preferred mapping for going backward (from
Company.OSTypes to the database string):

internal class OSMapping
{
private static map = null;

Company.OSTypes _osType;
bool _preferredMappi ng;

private OSMapping(Compa ny.OSTypes osType, bool preferredMappin g)
{
this._osType = osType;
this._preferred Mapping = preferredMappin g;
}

static OSMapping()
{
OSMapping.map["Redhat"] = new OSMapping(Compa ny.OSTypes.Redh at,
true);
OSMapping.map["Windows"] = new
OSMapping(Compa ny.OSTypes.Wind ows, true);
...
}

public static Company.OSType GetOSType(strin g databaseOSType)
{
OSMapping mapping = (OSMapping)OSMa pping.map[databaseOSType];
if (mapping == null)
{
return Company.OSTypes .None;
}
else
{
return mapping._osType ;
}
}

public static string GetDatabaseOSTy pe(Company.OSTy pes os)
{
string result = null;
foreach (DictionaryEntr y de in OSMapping.map)
{
OSMapping mapping = (OSMapping)de.V alue;
if (mapping._osTyp e == os)
{
if (mapping._prefe rredMapping)
{
result = (string)de.Key;
}
else if (result == null)
{
result = (string)de.Key;
}
}
}
return result;
}
}

Then you can just say:

string os = (string)dbTable["OS_TYPE"];
IKey.Enable(OSM apping.GetOSTyp e(os));

or

dbTable["OS_TYPE"] =
OSMapping.GetDa tabaseOSType(Co mpany.OSTypes.R edhat);

While this may seem like a lot of code, the advantage is that the
values stored in your database are not in any way tied to the names of
the enum types. Plus, if you use the "preferredMappi ng" flag, you can
even _change_ some mappings, giving new database names to existing
O/S's, while leaving intact the ability to read the old names that are
already recorded in the databse.

Nov 17 '05 #2

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

Similar topics

4
9860
by: Sami Viitanen | last post by:
Hello, Is there any other way to set variables than os.putenv(). Putenv doesn't actually put any values to actual system variables.. I'm trying to set CVSEDITOR variable automatically from script so that user wouldn't have to set that him/herself. CVS can't use variable set with putenv().
4
1514
by: David Sworder | last post by:
With a standard enum, values are automatically assigned. public enum MyEnum{Happy,Sad,Angry,Grandpa}; ....but what about a bitflag enum? public enum MyEnum:int{ FalseTeeth=0x00000001 BrainCancer=0x00000002
2
3751
by: John Hargrove | last post by:
I'm having trouble setting decimal place values in the results field of a sample management database I'm building for an environmental testing laboratory. The degree of sensitivity varies among test methods; consequently, some results are reported to 2 decimal places, some to 3, etc. The Results subform consists of Test Parameter, Result, Report Unit, Analysis Date, Analyst and other fields. The test parameter control is a drop-down...
6
1751
by: HD | last post by:
Hello. For the following, I would appreciate if anyone could tell me: if it can be done, how it might done, and/or what search terms I could use to find the solution myself. I would like to create a database in which _adding a new record automatically creates a new field in the table_ (ideally, deleting a record would also delete the field that was created when that record was added). Thus, the number of fields in the table would...
0
947
by: Maziar Aflatoun | last post by:
Hi, I have the lKey object that can be used to enable different operation systems such Windows 2000 Linux FreeBSD etc. Now I want to read a database table that contains key/values and if the key
2
2086
by: Scott | last post by:
Hi I have done a program that is to work out travel insurance for single and annual trips. For single trips I put in a start and end date no problems. But for an annual trip I only need to insert the start date of the cover and I want the program to automatically work out when the last day of the cover is and make that non editable. I have an idea how to do this, but how do I go about coding this part to do this automatically??? ...
1
6509
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
41
2887
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the "item"
8
16097
by: Andrus | last post by:
..NET 2 Winforms application. How to create new setting and set it default value in userSettings section of app.config file or overwrite existing setting value ? I found code below in this list which modifies Application setting section but how to add new item to userSettings ? Andrus.
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10108
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.