Question

I'm new in C# and I need some hints to solve this problem: I start to develop an application in C# an Windows Form Application (until this summer, I work 20 years in Visual Fox) The UI is generic (has an grid and controls for show details for current record), and is build dynamic from definitions I stored in an Xml File.

I want to create a class for each table I use for create methods for getting default values, validate fields, and record, etc). I use for this classes a namespace: BmiSqlTables.

Now I need in UI on adding record to get default values, validate fields, etc. The problem i have is to replace switch case statements which can became a very large one (for 50 tables) with something using substitution like in Foxpro

private void GetDefaultValue ( string TableName, string fieldName)
{
switch (tableName)
{
case "person":
  BmiSqlTables.Person.GetDefaulValue( fieldName, valueType);
  break;
case "client":
  BmiSqlTables.Client.GetDefaulValue( fieldName, valueType);
  break;
default:
break
}
}
namespace BmiSqlTables
{
  public static class Person
 {
  public static dynamic GetDefaultValue( string fieldName, string valueType)
  {
  dynamic defaultValue = null;
  switch (fieldName.toLower())
  {
  case "field1":
  case "field2":
  default:
   switch (valueType.ToLower())
    {
    case "string":
    case "varchar":
    case "char:"
      return "";
    case "int":
      return 0;
    default:
      return  null;
    }
  .......
  }
  }
  }
}

In Foxpro the GetDefaultValue can be something like this:

Function GetDefaultValue( tableName, fieldName, valueType)
return BmiSqlTable.&tableName..GetDefaultValue( fieldName, valueType)

Any advice to this problem (and project) will be apreciate. Hope you understand my english, and what I need to do. Thanks in advance.

Was it helpful?

Solution

I understand that in FoxPro you would store the values in a table and use "substitution (or whatever that means)" to get them back. Do the same in .NET. Store the values in some external source (DB or xml file or whatever) and read them in the method implementation.

Keep in mind that while FoxPro was a DB engine as well and you kinda got data for free in .NET you need to choose what technology you want to use for data storage.

For your particular example a simple xml file holding name/value pairs should do fine. Look up System.Xml.XDocument for one of the multiple ways you can interact with XML documents.

Edit: If you still want to hold the data inside the code and not some external source consider using one/multiple static dictionaries (System.Collections.Generic.Dictionary) in place of your huge switch cases.

OTHER TIPS

Take a look at the strategy pattern: https://www.google.be/search?q=strategy+pattern

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top