0

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.

Bogdan
  • 656
  • 15
  • 25
  • _"Hope you understand [..] what I need to do."_ - No. What _are_ you trying to do? – CodeCaster Oct 14 '13 at 08:11
  • I want to write a generic method GetDefaultValue (in UI), which no need to have a swich statement for tableName, and don't need to put every table name I will create in future. and execute the right method GetDefaultValue for tableName (from BmiSqlTables namespace) – Bogdan Oct 14 '13 at 08:31

2 Answers2

0

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

Kris Vandermotten
  • 10,111
  • 38
  • 49
  • I found something which is more appropriate for solve my problem. I will create one Class InputTable, with subclasses Person, Client, etc. Now the problem is to create instance using the table Name: something like this InputTable table = New InputTable( tableName). and then I will be able to execute methods for Person, Client, or any table I need. Of course I don't wont to use switch statement. – Bogdan Oct 14 '13 at 09:58
  • Thanks I solve my problems. What I did: I create a class Input Tables for template and subclasses for each table I need. In UI, I declare a Dictionary< string, InputTable> InputTablesDictionary When I need to execute a method of specific tables, I use code bellow: dynamic it = InputTablesDictionary[tableName]; return it.GetDefaultValue(fieldName, valueType); – Bogdan Oct 14 '13 at 10:47
  • 1
    as far as i remember my answer was the one suggesting dictionaries but oh well... – Adrian Zanescu Oct 14 '13 at 11:10
0

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.

Adrian Zanescu
  • 7,907
  • 6
  • 35
  • 53
  • I use xml only for read windows form definitions (fields name, captions, formating), forms which are building dynamic when the user click the menu option (Person, Clients, Invoice, Orders etc), For storing data I use System.Data.Common class, with generic methods for connect, update, etc to database, no matter which provider is. this si already made. – Bogdan Oct 14 '13 at 09:29
  • it was an example of an external data source. You might consider using a full DB engine for this. It's your decision on what maps well for your app. Anyway by the way you are describing the problem you are trying to hold data inside code and that might not be a good idea – Adrian Zanescu Oct 14 '13 at 09:34
  • I want to right a one code for any kind of database I use. If I right code in database for default values, validating fields, and records, that will be specific for each database type, this is good when you are using just one database engine. That why I decide to create code in application for validating, getting defaultvalues on add records, etc. – Bogdan Oct 14 '13 at 09:48