|
JP_Rocks wrote: I have many constants, each recurring as per country and declared in class as below:
class CountryRequests
{
public const string JAP_ABC="JAP_012686";
public const string JAP_PQR="JAP_012457";
I'd go for an enum, like this;
enum CountryRequests
{
JAP_012686,
JAP_012457
}
That way they're grouped, like constants in a class, and you can still use the string-version by calling JAP_012686.ToString() - that would give you the same result as the content of the constant.
JP_Rocks wrote: What is best coding here
That depends on your type of application. Long lookup-lists are happy in a local embedded database, other times you might want to centralize the information (so that updating it is easy), and move to a central database, or a resource-assembly.
JP_Rocks wrote: I cannot change from CountryRequests class to collection as it is used by multiple projects.
That the class is used often is not an excuse. You can easily write a new class that can be used as a substitute; dive into the "L" part of the SOLID-principle
Bastard Programmer from Hell
|
|
|
|
|
Just refractor the if statement into the CountryRequests class to return the code. I'm guessing there would need to be an extra parameter to handle the ABC, PQR suffix.
public void GetCountryRequestCode(string countryCode)
{
switch (countryCode)
{
Case "JAP":
return JAP_ABC;
....
}
}
"You get that on the big jobs."
|
|
|
|
|
JP_Rocks wrote: I cannot change from CountryRequests class to collection as it is used by multiple projects. One question: is the CountryRequests class in a library that is now being compiled, and which the other projects are now using by embedding a reference to the compiled class ? In other words, are all the projects using CountryRequests under your source-control scope ?
I ask this because of the fact that the compiler in-lines const types where they are referenced, and unless both the CountryRequests class, and all other projects using it are re-compiled together, a client class may persist an out of date value[^].
The idea of using Reflection to pre-compile ... say by using 'Emit ... a "hard-coded" look-up to replace a big Switch statement might be worthwhile if these constant values were being accessed with great frequency, but I doubt that's the case here.
Another idea, based on the constraint that you can't touch 'CountryRequests," would be to create a kind of "singleton" class that effectively "extends" it through inheritance.
disclaimer: experimental: this code tested quickly in VS 2010 Pro, .NET 4.0 Client FrameWork; I have no idea what the implications of using this type of inherited class with call to a private (internal) constructor done by a public static method in a variation on the "singleton" technique, and using a mix of static and public classes might mean in a real-world use case of multi-threaded environment, remoting, multiple consumers of the class, etc.:
public class CountryRequestsServer : CountryRequests
{
internal static CountryRequestsServer instanceCRServer;
internal static Dictionary<string, string> countryLookUp;
internal CountryRequestsServer()
{
countryLookUp = new Dictionary<string, string>()
{
{"JAP", JAP_ABC},
{ "USA", US_POP},
{ "UK", ENG_POP}
};
}
public static string getCountry(string request)
{
if(instanceCRServer == null)
{
instanceCRServer = new CountryRequestsServer();
}
return countryLookUp.ContainsKey(request)
?
countryLookUp[request]
:
"No Match: Input = " + request;
}
} Tested like this:
string myCountryRequest = CountryRequestsServer.getCountry("JAP");
good luck, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges
|
|
|
|
|
BillWoodruff wrote: using Reflection ... with great frequency
In which case I recommend caching the results of the Reflection. Perhaps you meant that, but didn't make it clear.
I don't see that a Singleton gives you any benefit over a static class in this example.
|
|
|
|
|
What you want to do is change this into a family of 'pseudoconstant' instances, or a database table:
public class CountryInfo {
public readonly string ABC, POP, PQR, ...;
private CountryInfo(string abc, string pop, string pqr ...){
ABC = abc; POP = pop; PQR = pqr;
}
public static readonly CountryInfo
JAP = new CountryInfo(CountryRequests.JAP_ABC, CountryRequests.JAP_POP, CountryRequests.JAP_PQR, ...),
UK = new CountryInfo(CountryRequests. UK_ABC, CountryRequests. UK_POP, CountryRequests. UK_PQR, ...),
US = new CountryInfo(CountryRequests. US_ABC, CountryRequests. US_POP, CountryRequests. US_PQR, ...),
}
You still have to define all the constants, but now you don't need to put a switch everywhere. Just pass a CountryInfo (e.g. CountryInfo.JAP) and access the strings through the instance:
void CountryDependentFunction(CountryInfo countryInfo){
someFunction(countryInfo.POP);
}
Even if you don't have enough control to replace or modify CountryRequests, you should be able to put this on top of it. If CountryRequests changes frequently you can write a script to update the lower part of CountryInfo, or you could put some static initialiser reflection in there to load entries at runtime (but then you would have to use a dictionary and indexing and it wouldn't be as nice as using properties, as well as being needlessly complex for most situations).
|
|
|
|
|
Hi, CP. I have a database hosted on an instance of SQL Server 2008 Express. I am using C# (.NET 4.0) and VS2010. I created a junction table in order to use a many-to-many relationship between two tables. I retrieve and manipulate data primarily using the main table but a part of the application requires me to find the most common value for a particular field, which is stored in a junction table.
For example, I call a SELECT statement which retrieves the fields from the table [Repair Data]. The junction table, [Failure Codes], contains the fields [Serial Number] and Failure. In order to find the most common [Failure Codes].[Failure] value based on a [Repair Data] record ([Repair Data] includes a field [Serial Number], which is how the junction table knows which [Repair Data] record it belongs to) would I do something like this:
reader = SELECT * FROM [Repair Data]
foreach (Record r in reader)
{
}
or is there a simpler method for achieving these results? I think I explained it correctly. I am currently not at work and I try to forget work when I'm at home . So I do not recall exactly what code I left off with. But I am quite positive I followed the "code" above.
Thanks CP.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
I don't know if I understand correctly your issue. I understand the issue is, that you have tables:
1. [Repaired item] ([Serial Number], [Some data about item]) - stores information about items, that are repaired (car, or whatever).
2. [Failure Codes] ([Failure Code], [Information about failure]) - stores information about failures, that can be repaired.
3. [Repair Data] ([Serial Number], [Failure Code]) - stores connection between [Repaired item] and [Failure Codes].
With the structure above you want (for the given [Serial Number]) find the failure that occurs most often.
If the above is correct, then I think you should do this in SQL using the following query:
SELECT
[Serial Number],
[Failure Code],
COUNT(*) AS [Counter]
FROM
[Failure Codes]
WHERE
[Serial Number] = @SerialNumber
GROUP BY
[Serial Number],
[Failure Code]
ORDER BY
[Counter] ASC
And you can retrieve it easily from C# without any loops.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
My treeview is populated by an xml file. When I go to add a node, I'm actually altering the xml file (that's what I want). To refresh the treeview, I tried
treeSidebar.Refresh(); when that didn't work, I did
treeSidebar.Nodes.Remove(treeSidebar.Nodes[0]);
doc.Load("Subscription.xml");
treeSidebar.Nodes[0].Nodes.Add(name);
I got an error that says the file I want is being used by another process. How can I close a file so I can open it with another process?
|
|
|
|
|
XmlDoc doc = new XmlDoc();
using(FileStream fileStream = new FileStream("your file")){
doc.Load(fileStream);
}
|
|
|
|
|
Member 8069795 wrote: file I want is being used by another process
There's not much you can do about that.
Member 8069795 wrote: doc.Load("Subscription.xml");
Will close the file, so that shouldn't be the problem.
Just what is it you're trying to do? What creates the file? How many clients are trying to work with it?
|
|
|
|
|
It's an xml file that already exists, I load it once when the form loads, then when I refresh a treeview that it populates, that's the second time I want to use it.
|
|
|
|
|
If it's not shared then I don't see how another process would have it open. Maybe a threading issue? Dunno.
|
|
|
|
|
Load, parse and close.. this is some of my vb code, but it's the same principal for C#
//Load and parse tree nodes
defXmlDoc.Load(xmlPath)
TreeView1.Nodes.Clear()
//Ignore root node and add level 1
For Each node As XmlNode In defXmlDoc.DocumentElement.ChildNodes
AddNode(node, TreeView1.Nodes)
AddNodeTree(node, TreeView1.Nodes.Item(TreeView1.Nodes.Count - 1))
Next
defXmlDoc = Nothing
Private Sub AddNode(ByVal xmlNode As XmlNode, ByVal NodeCol As TreeNodeCollection)
Dim id, label, desc As String
label = xmlNode.Name
desc = ""
id = "Unknown"
If Not xmlNode.Attributes("label") Is Nothing Then
label = xmlNode.Attributes.GetNamedItem("label").Value
End If
If Not xmlNode.Attributes("description") Is Nothing Then
desc = xmlNode.Attributes.GetNamedItem("description").Value
End If
If Not xmlNode.Attributes("id") Is Nothing Then
id = xmlNode.Attributes.GetNamedItem("id").Value
If id > maxId Then maxId = id
End If
NodeCol.Add(id, label)
NodeCol(id).Tag = desc
End Sub
Private Sub AddNodeTree(ByVal xmlNode As XmlNode, ByVal treeNode As TreeNode)
Dim i As Integer
Dim xNode As XmlNode
Dim tNode As TreeNode
Dim nodeList As XmlNodeList
If (xmlNode.HasChildNodes) Then
nodeList = xmlNode.ChildNodes
For i = 0 To nodeList.Count - 1
xNode = xmlNode.ChildNodes(i)
AddNode(xNode, treeNode.Nodes)
tNode = treeNode.Nodes(i)
AddNodeTree(xNode, tNode)
Next
Else
End If
End Sub
|
|
|
|
|
I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader.
I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr["OrderId"];
}
}
But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader?
Thanks
|
|
|
|
|
You can try the other overload[^] of the DbDataReader's Item[] property:
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr[1];
}
}
|
|
|
|
|
dr.GetOrdinal("fieldName")
|
|
|
|
|
There should never be any reason to insist that the columns are in a particular order. You're doing something wrong.
|
|
|
|
|
+5, since the order of the columns in a record could be coming from the server in a random order. And life is soo much easier if you just decide the order in the Presentation Layer - preferably having the user choose one.
OTOH, this is an Excel-sheet, and it might come without headers. In that case, only the position of the column would indicate what cell (dbfield) you're actually editing.
Things become harder if the user is allowed to move those things around. In an ideal world, each row would have a "caption" on top of the file.
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote: it might come without headers
Then you're hosed.
|
|
|
|
|
if you need help i develop program with visual c and visual basic my e mail is albertodiprima@hotmail.com
|
|
|
|
|
... and you have been a member of CodeProject long enough to know that the forums are not the appropriate place for free advertising.
The best things in life are not things.
|
|
|
|
|
i write and study code i'm not playing
|
|
|
|
|
Why you want to get clients here in CodeProject?! In your place where you live there people don't use computers?
I Love T-SQL
"VB.NET is developed with C#.NET"
If my post helps you kindly save my time by voting my post.
www.cacttus.com
|
|
|
|
|
I am planning a memory game (as an exercise for myself) and thought of loading pictures, probably gifs or pngs into PictureBox controls like Microsoft does in this tutorial.
http://msdn.microsoft.com/en-us/library/dd492173.aspx
I would like to have the opinion of more experienced members in this forum, and mostly an answer to the following. During the game, two pieces will be revealed to the player for X seconds and then, in case he/she fails, they will have to be covered again. What does this mean in programming terms? I think that loading a picture and then loading a blank one would be a solution but probably a bad one, provoking unnecessary traffic. I have no better idea for the time being What do you think?
Thanks in advance.
PS: In those tutorials the last one is a Memory game, but with fonts.
http://msdn.microsoft.com/en-us/library/dd553233.aspx
What I am trying to do is to combine the techniques of those two tuts.
|
|
|
|
|
Sounds like a good enough solution to me. You could either set the visibility of the picture box control to show/hide it, or replace the image with a "blank" one you already have - it all depends on the aesthetic you're going for.
I would suggest pre-loading all the images (including the blank one) at startup and keeping them in memory (remembering to free them on close, GC may handle this for you, can't quite remember with images...). If you have a reasonable number of images, then this should be fine. Remember you'll only need one in-memory copy of the blank one although it may be used for all tiles.
Another solution would be simply to draw something over the top of the images in order to hide them, for example a black rectangle or another image. This may be another solution worth looking into.
Seems like you're on the right track though. Good luck!
Typical n-tiered architecture:
DB <-> Junk(0) <-> ... <-> Junk(n-1) <-> Pretty
modified on Wednesday, July 27, 2011 6:55 AM
|
|
|
|
|