473,396 Members | 2,010 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.

how to avoid hard-coding of database field names?

I have several functions that retrieve information from databases based
on the fieldname passed in (e.g. GetValue(dbUsers, userID, "Fax"))

what is the best way to not hard-code field names?

Something like this would be nice:

Enum UserTable
Fax = "Fax"
Phone = "Phone"
End Enum

where I can use GetValue(dbUsers, userID, UserTable.Fax) and just
modify the Enum if I change field names?

Thanks...
Eych

Nov 21 '05 #1
4 1840
created a new module, moduleTables, that has:

Public class Table1
Public const Fax = "Fax"
Public const Phone = "Phone"
....
End Class

etc. for other tables.
So, I can call
Dim tmpClass as Table1
GetValue(dbUsers,userID, Table1.Fax)

Is this good coding standards?

Nov 21 '05 #2
"Eych" <ey******@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Something like this would be nice:

Enum UserTable
Fax = "Fax"
Phone = "Phone"
End Enum

GetValue( dbUsers, userID, UserTable.Fax )

and just modify the Enum if I change field names?


What you've got /can/ be adapted, something like this :

Enum UserTable
Fax = 0
Phone
End Enum

It doesn't actually matter what the numbers are, but it means you
/can/ use

GetValue( dbUsers, userID, UserTable.Fax )

BUT - your function received the Enum "value" (0, 1, etc. ) but
you need the Enum's "name" ("Phone", "Fax", etc.) and, believe it
or not, we can do that these days!

Function GetValue( ... , ByVal eaField as UserTable )
Return SomethingBasedOn( eaField.ToString() )
End Function

HTH,
Phill W.
Nov 21 '05 #3
that'll work!

thanks!

Eych

Nov 21 '05 #4

"Eych" <ey******@hotmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
created a new module, moduleTables, that has:

Public class Table1
Public const Fax = "Fax"
Public const Phone = "Phone"
...
End Class

etc. for other tables.
So, I can call
Dim tmpClass as Table1
GetValue(dbUsers,userID, Table1.Fax)

Is this good coding standards?

IMHO, this is a far better solution than using the other method proposed
(extracting the name of the enum field itself).
The main reason is flexibility. What if you wanted to change the name of the
field, say change Fax to Fax2. You would still have to do a find and replace
to locate all instances where you explicitly used Table1.Fax to Table1.Fax2.
Plus other possibilities for quirks and confusion.

Now if you created your "enumeration" as a class like you propose, then no
other code changes would be required. It would not matter what the variable
name is, just the value returned. So if you change the "Fax" field name to
"Fax2", then just change: Public const Fax = "Fax" to Public const Fax = "Fax2"

and that's it.

It makes your code easier to maintain, for yourself and anyone else that
might need to.
Say at some point in the future, you wanted the ability to dynamically map
the field names via a config file. Well, if the field name is returned by a
class, all you need to do is alter the class to read the values from the
file. Again, you could leave your other code untouched.

So, personally I would, and have, taken the class route. Now, the other
point of discussion would be the scoping of the class itself. Should it
contain only static methods? Should it be a child class of possibly the
class that handles all the other DB interaction, if any. Etc. That can only
really be answered by considering your overall architecture and such.

Gerald

Nov 21 '05 #5

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

Similar topics

383
by: John Bailo | last post by:
The war of the OSes was won a long time ago. Unix has always been, and will continue to be, the Server OS in the form of Linux. Microsoft struggled mightily to win that battle -- creating a...
10
by: teddysnips | last post by:
Application is a Work Tracking/Timesheet database. The increments of work are stored in the TimesheetItem table. This contains, inter alia, the Work Code, the Start and the Duration that the...
8
by: Pietro | last post by:
I find a lot of .Net decompilers that brings me the full source code, there is a way to avoid this ? Pietro
13
by: Sameer | last post by:
Hi friends, I am beginner in C++. I am using g++ compiler. below is my code which gives error as " invlid conversion from 'char' to 'const char*' ..Plz help me with this. #include <iostream.h>...
0
by: academic | last post by:
Applying a mask I avoid the pixels on the boundary in case the entire image is being filtered, so as not to access areas outside the image. I'd like to do a better job - for example it the Roi...
10
by: Andrew Trevorrow | last post by:
No response to my last message, so I'll try a different tack... Does anyone know of, or even better, has anyone here written a C++ application for Mac/Windows that allows users to run Python...
3
by: Susan Rice | last post by:
How can I rewrite this code to avoid the possibility of a buffer overflow? sprintf(errbuf, "%s\nError is: %u: %s\n", errmsg, dwErrCode, s ); Here: errmsg = a string dwErrCode = a number s =...
7
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
16
by: mike3 | last post by:
(I'm xposting this to both comp.lang.c++ and comp.os.ms- windows.programmer.win32 since there's Windows material in here as well as questions related to standard C++. Not sure how that'd go over...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
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...

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.