Is possible create a method with an undefined type?

Matheus Machado

I am trying to create a method "Insert" to my WebService connected to external DataBase, but instead of create InsertPerson, InsertAge, Insert anything I am trying to do an unique Insert that the parameter type is defined automatically according with request, for example: if I want to insert a person in db, I call Insert("mytype" name), if after I want to insert age calling Insert("mytype" age) and this type is included when you click at specific button.

If it was confusing I will try explain with one more example: I click on "Insert Age" button who will send to "mytype" that is an integer and value of textbox by parameter, the code will connect to DB and insert that. Something like this:

Click event -> mytype = int
Insert(int age) -> "INSERT INTO db VALUE age"

So, after all this, is possible create something like this? Or exist another better alternative?

Josh K

Instead of boxing in object, you can do it generically:

public void Insert<T>(T input)
{
    // DB setup code goes here
    var parameter = new SqlParameter("@input", input);

    // Finish DB code here
}

// Call it like this
myClass.Insert<int>(integerInputData);
myClass.Insert<string>(stringInputData);

It's cleaner than boxing and unboxing in object.

https://msdn.microsoft.com/en-us/library/twcad0zb.aspx

However, if you are just passing it to something that expects object (like SqlParameter), then you can just box it in object.

public void Insert(object input)
{
   // DB setup code goes here
   var parameter = new SqlParameter("@input", input);

   // Finish DB code here
}

// Call it like this
myClass.Insert(integerInputData);
myClass.Insert(stringInputData);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

The method *****is undefined for the type***** in Java

From Dev

The method is undefined for the type array

From Dev

the method is undefined for the type string

From Dev

The method "X" is undefined for the type

From Dev

The method ***** is undefined for the type *******

From Dev

The method is undefined for the type SharedPreferences

From Dev

The method and() is undefined for the type HttpSecurity

From Dev

The method *****is undefined for the type***** in Java

From Dev

NoMethodError: undefined method `create'

From Dev

NoMethodError: undefined method `create'

From Dev

Possible to create a "shorthand" method?

From Dev

The method getParentFragment() is undefined for the type MyDialogFragment

From Java

Trouble with Method undefined for a type Java

From Java

Why is my method undefined for this type?

From Dev

The method getSupportFragmentManager() is undefined for the type Context

From Dev

The method onPause( ), onResume( ) is undefined for the type

From Dev

The method getNextQuote() is undefined for the type QuoteServerThread

From Dev

The method execute() is undefined for the type InvoiceTemplateGet

From Dev

The method getDispatcherType() is undefined for the type HttpServletRequest

From Dev

The method createGUI() is undefined for type JFrame

From Dev

The Method is undefined for the type object error

From Dev

Why is my method undefined for this type?

From Dev

Trouble with Method undefined for a type Java

From Dev

The method isEmpty() is undefined for the type Optional

From Dev

(Array of objects) The method is undefined for the type

From Dev

The method abortBroadcast() is undefined for the type SMSReceiver

From Dev

The method onPause( ), onResume( ) is undefined for the type

From Dev

The method getCurrentSession() is undefined for the type SessionFactory

From Dev

The method onOptionsItemSelected is undefined for the type Object

Related Related

  1. 1

    The method *****is undefined for the type***** in Java

  2. 2

    The method is undefined for the type array

  3. 3

    the method is undefined for the type string

  4. 4

    The method "X" is undefined for the type

  5. 5

    The method ***** is undefined for the type *******

  6. 6

    The method is undefined for the type SharedPreferences

  7. 7

    The method and() is undefined for the type HttpSecurity

  8. 8

    The method *****is undefined for the type***** in Java

  9. 9

    NoMethodError: undefined method `create'

  10. 10

    NoMethodError: undefined method `create'

  11. 11

    Possible to create a "shorthand" method?

  12. 12

    The method getParentFragment() is undefined for the type MyDialogFragment

  13. 13

    Trouble with Method undefined for a type Java

  14. 14

    Why is my method undefined for this type?

  15. 15

    The method getSupportFragmentManager() is undefined for the type Context

  16. 16

    The method onPause( ), onResume( ) is undefined for the type

  17. 17

    The method getNextQuote() is undefined for the type QuoteServerThread

  18. 18

    The method execute() is undefined for the type InvoiceTemplateGet

  19. 19

    The method getDispatcherType() is undefined for the type HttpServletRequest

  20. 20

    The method createGUI() is undefined for type JFrame

  21. 21

    The Method is undefined for the type object error

  22. 22

    Why is my method undefined for this type?

  23. 23

    Trouble with Method undefined for a type Java

  24. 24

    The method isEmpty() is undefined for the type Optional

  25. 25

    (Array of objects) The method is undefined for the type

  26. 26

    The method abortBroadcast() is undefined for the type SMSReceiver

  27. 27

    The method onPause( ), onResume( ) is undefined for the type

  28. 28

    The method getCurrentSession() is undefined for the type SessionFactory

  29. 29

    The method onOptionsItemSelected is undefined for the type Object

HotTag

Archive