G.Torres
G.Torres

Reputation: 23

translating to c# code the use of a class method trough an object variable

I have just two years I started programming (mostly C#), but there's still a lot I don´t know about the language, I'm trying to translate a web project from VB.NET code to C# code, but I got to a section which I don't know how to translate.

The class in the VB code uses a sub procedure to do an INSERT to a database, to do this, in the class there is a db variable declared as an Object, then the db variable uses a function from a module to change the class type depending on the conection type and database is going to be used (this is determined from getting a value from the Web.config file), then after building the query, the db variable uses another function from the new class type assigned to execute the query.

To better ilustrate, here is the VB code.

This is the part where the db variable is used:

Public class MyClass1
   Private Sub _AddDB()
      Dim db As Object
      Dim query As String

      db = TypeDB()
      query = "Some Query"
      db.ExecuteDB(query)
   End Sub
End Class

As I said, the TypeDB() function is in a Module:

Module MyModule
   Public Function TypeDB() As Object
      Dim Str As String = ConfigurationSettings.AppSettings.GetValues("strConnDB").GetValue(0)
      Dim db As Object

      Select Case Str
        Case "SQL"
          db = New DBSql_Class
        Case "ODBC"
          db = New DBOdbc_Class
        Case "OLE"
          db = New DBOle_Class
      End Select
      TypeDB = db
   End Function
End Module

Each of the classes mentioned on the module has its own ExecuteDB function so there is no problems in executing the line db.ExecuteDB(query). I read that the closest thing to a Module in C# is a static class, so that was what I did with MyModule, I already have the rest of the classes, but when I try:

public class MyClass1
{
   private void _AddDB()
   {
      object db;
      string query;

      db = MyModuleClass.TypeDB();
      query = "Some Query";
      db.ExecuteDB(query);
   }
}

I immediately get the error that the db objetc does not contain a definition for the method ExecuteDB.

My first idea was to do a switch statement inside the _AddDB method, but it will not be optimal, because there is already a switch statement in the MyModuleClass to select the new type of class for the db variable, I tried searching for a solution, but all I found was only solutions that only uses one class.

I want to do this as close to the original project as posible, but this kind of interaction is all over the VB project.

Upvotes: 1

Views: 188

Answers (2)

Al Kepp
Al Kepp

Reputation: 5980

Object-oriented clean solution to this problem in C# is to define an interface with ExecuteDB() operation and implement it in those three DB classes. Then return that interface from your static method, and then you can call it directly.

interface IDatabase {
  object ExecuteDB(string);
}

class DBSql_Class : IDatabase {
  public object ExecuteDB(string query) {
    ...
  }
}

static class MyModuleClass {
  public static IDatabase TypeDB() {
    switch(...) {
      ...
      return new DBSql_Class();
      ...
    }
  }        
}

Just a note: your class names are terrible. Please try to follow standard naming scheme when programming in .NET. :-)

Upvotes: 3

Dave Doknjas
Dave Doknjas

Reputation: 6542

You need to declare 'db' as dynamic in C# to achieve the same sort of late-binding that the VB code is doing. e.g.,

  dynamic db = null;
  string query = null;

  db = MyModule.TypeDB();
  query = "Some Query";
  db.ExecuteDB(query);

Or, better yet, use the appropriate types instead of 'object'.

Upvotes: 2

Related Questions