locked
How to store the class result in the variable created the class? RRS feed

  • Question

  • Hi

    Well, I was just wondering about this 

    If I code

    Dim mydt As New Datatable

    ......

    Later on, I can code 

    Mydatagridview.Datasource = mydt

    If you note, I used the variable mydt which create the class Datatable without using any property/sub/function

    If I create my own class

    Public Class mydatatable
        Property my_prop_dt As DataTable
    End Class

    the only way (As I know) to use it like that

    Dim oMyClass As New mydatatable
    mydatagridview.datasource = oMyClass.my_prop_dt

    The same if I used function instead of property

    What I want or looking to see if that is applicable is :

    Dim oMyClass As New mydatatable
    mydatagridview.datasource = oMyClass

    Can I use the variable oMyclass to store the result I want same behaviour as Dim oMyClass as New Datatable ?



    .Net Blog VFP Blog

    Monday, August 12, 2013 8:33 AM

Answers

  • I want to create or add my own custum properties/sub/function to myclass which is based on Datatable 

    You can do that - that's what Inherits is all about.   Once you have created your custom class that inherits from a datatable you can add methods and functions to enhance it.  For instance, you might create a method that initialises it (the Clear method) and then loads some data into it (using Load) and so on.   For your example code above you would provide a method that returns a new datatable

    Public Class MyDatatable
        Inherits DataTable
        Function ExecuteToDatatable(TableName As String,Query As String) as MyDataTable
            Dim dt As New MyDataTable(TableName)
            '.... some code to fill the dt
            Return dt
        End Function
    End Class

    so your application code is

            Dim oMyClass As New MyDatatable
            ...
            oMyClass = oMyClass.ExecuteToDatatable(SourceTable, QueryString)

    • Edited by Acamar Monday, August 12, 2013 10:36 PM code
    • Proposed as answer by Carl Cai Tuesday, August 13, 2013 8:28 AM
    • Marked as answer by Samir Ibrahim Tuesday, August 13, 2013 4:55 PM
    Monday, August 12, 2013 12:51 PM
  • @Acamar : Nice Trick :)

    I was testting code and that works of how I want it.

    Thanks for your effort.

    Dim o As New MyDatatable
    Me.DataGridView5.DataSource = o

    Public Partial Class MyDatatable

        Inherits datatable
        Sub New
            MyBase.Columns.Add("A",GetType(Integer))
            MyBase.Columns.Add("B",GetType(String))
            MyBase.Rows.Add({1,"one"})
        End Sub
    End Class


    .Net Blog VFP Blog


    Monday, August 12, 2013 2:59 PM

All replies

  • Hi,

    Yes .. you have tyo inherit it from datatable

    Regards,

    Stygen

    Monday, August 12, 2013 9:11 AM
  • @Stygen

    I thought about that, but... 

    Public Class MyDatatable
        Inherits DataTable
        Sub new
            MyDatatable = New DataTable() ' <-- error Mydatatable is type and cannot be used as an expression
        End Sub
    End Class

    I got error in bolded line

    How/Where to store the result to the class itself?


    .Net Blog VFP Blog

    Monday, August 12, 2013 9:17 AM
  • I got error in bolded line

    How/Where to store the result to the class itself?

    There is no need for the sub New.    Creating your class and inheriting from the type DataTable means that your class instance can be used just like a DataTable object.   Why do you want to use your own class?

    Option Strict On
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            Dim oMyClass As New MyDatatable
            mydatagridview.datasource = oMyClass
    End Class
    
    Public Class MyDatatable
        Inherits DataTable
    End Class
    

    Monday, August 12, 2013 9:31 AM
  •    Why do you want to use your own class?

    Hi Acamar

    I want to create or add my own custum properties/sub/function to myclass which is based on Datatable such as

    Partial Class MyDatatable
        Inherits DataTable
        Function ExecuteToDatatable(TableName As String,Query As String)
            Dim dt As New DataTable(TableName)
            '.... some code to fille the dt
            ' Save the result of dt to Class <---
        End Function
    End Class

    so Later I can code 

    mydatagridview.datasource = oMyClass

    This is a thought I was thinking in to see if its doable, that is all.


    .Net Blog VFP Blog


    Monday, August 12, 2013 9:44 AM
  • I want to create or add my own custum properties/sub/function to myclass which is based on Datatable 

    You can do that - that's what Inherits is all about.   Once you have created your custom class that inherits from a datatable you can add methods and functions to enhance it.  For instance, you might create a method that initialises it (the Clear method) and then loads some data into it (using Load) and so on.   For your example code above you would provide a method that returns a new datatable

    Public Class MyDatatable
        Inherits DataTable
        Function ExecuteToDatatable(TableName As String,Query As String) as MyDataTable
            Dim dt As New MyDataTable(TableName)
            '.... some code to fill the dt
            Return dt
        End Function
    End Class

    so your application code is

            Dim oMyClass As New MyDatatable
            ...
            oMyClass = oMyClass.ExecuteToDatatable(SourceTable, QueryString)

    • Edited by Acamar Monday, August 12, 2013 10:36 PM code
    • Proposed as answer by Carl Cai Tuesday, August 13, 2013 8:28 AM
    • Marked as answer by Samir Ibrahim Tuesday, August 13, 2013 4:55 PM
    Monday, August 12, 2013 12:51 PM
  • @Acamar : Nice Trick :)

    I was testting code and that works of how I want it.

    Thanks for your effort.

    Dim o As New MyDatatable
    Me.DataGridView5.DataSource = o

    Public Partial Class MyDatatable

        Inherits datatable
        Sub New
            MyBase.Columns.Add("A",GetType(Integer))
            MyBase.Columns.Add("B",GetType(String))
            MyBase.Rows.Add({1,"one"})
        End Sub
    End Class


    .Net Blog VFP Blog


    Monday, August 12, 2013 2:59 PM
  • The real answer to my question was using MyBase.Rows.Add()

    Which I found while I was trying.

    Thanks every one.


    .Net Blog VFP Blog

    Tuesday, August 13, 2013 4:57 PM