Abbas
Abbas

Reputation: 5044

Generic function in VB.net

I have 2 different user account type and they both are stored in their respective tables (Members in Member table and Admin in Administrator table).

Now i want to create a common function to access user info for any type of user, so i was looking a for generic function but i am stuck with returning respective class,

I have create 2 class MemberInfo for normal users and AdminInfo for Admin users

Now if the generic class passed to this function is MemberInfo than it should process normal user details and return MemberInfo class, and if it's admin users, then it should return AdminInfo class.

Here is something what i have tried but unable to achieve my goal.

Public Function GetAllMembers(Of T)(ByVal accountType As AccountType) As List(Of T)
        Dim T_ReturnValue As List(Of T)
        Dim returnType As Type = GetType(T)
        If returnType Is GetType(MemberInfo) Then

            Dim _list As New List(Of MemberInfo)
            With New OleDbDataAdapter("SELECT ACCOUNT_NO, COUNTRY FROM Member", Globals.DatabaseConnection)
                Dim dt As New DataTable
                .Fill(dt)
                For Each row As DataRow In dt.Rows
                    Dim memberInfo As New MemberInfo
                    memberInfo.AccountNo = row("Account_No").ToString
                    memberInfo.Country = row("Country").ToString
                    _list.Add(memberInfo)
                Next
            End With
            Return DirectCast(_list, List(Of T))
        End If
    End Function

Can anyone help me how i can return respective class, for now i wanted to return memberinfo class only.

Upvotes: 0

Views: 183

Answers (3)

dotNET
dotNET

Reputation: 35400

Right. Before addressing your specific question, I want to start at the lower level.

In theory, an admin is a user, so at database level this should rather be implemented so that there is a [Users] table that stores all kinds of users including admins. Then you should add another table called [Admins] which links to the [Users] table through an FK and stores all additional fields that relate to admins only. This is called ISA / inheritance relation in RDBMS theory.

At application level, this will translate to two business classes, one for [User] and one for [Admin], where [Admin] will inherit from [User] class. You can then write a function that returns a [User] object. Since [Admin] inherits from [User], polymorphism will allow you to return [Admin] object from the same function. Then your caller can confirm the returned object type either through type checking, or you can store a boolean field in [Users] table called IsAdmin that will be true for administrators.

Upvotes: 0

Arindam Nayak
Arindam Nayak

Reputation: 7462

You can follow these steps.

  1. Create an abstract class say "User" . And then Member and Admin has to extend that base class user. Assuming, both has same set of properties and that is why you have started using T to make it generic. But as you have said both has different DB table store.
  2. If you different methods defined for Member and Admin, you can segregate them by using interface. Say Member can Send Friend request, so you can have an interface ( Say ISendRequest), that will have Send method definition only. And if Admin can Add new member ,then you can have interface say IAddMember, and Admin will implement IAddMember , Member will implement, ISendRequest.

KEY point Now, define an interface say IGetAllUser with method GetAllUser and User class has to implement that, but it will have abstract method GetAllUser. So point here is you have to have to write this one GetAllMembers, instead each derived class will have method to get corresponding List .

Sample code snippet. This can even accommodate the scenario if both Member and Admin has different properties.

But if you have same properties, then you can define a function in Base class, that takes Datatable and just sets required properties, as both member and admin has same properties. So the sole purpose of GetAllUsers implemented in Member and Admin class is to pass required table name to Data Access layer and get DataTable and pass that to function defined in base class to set required properties and build List of User.

public interface IGetAllUsers
{
    List<User> GetAllUsers();
}

abstract class User : IGetAllUsers
{
    public abstract List<User> GetAllUsers();
}


class Member : User
{
    public override List<Member> GetAllUsers()
    {
            // Assuming there is data access layer, to get details
    }
}


class Admin : User
{
    public override List<Admin> GetAllUsers()
    {
        // Get all admin
    }
}

Upvotes: 0

xpda
xpda

Reputation: 15813

Two ways:

  1. You can have two overloaded functions that return different classes.

  2. You can declare the function as Object, and return either class.

Upvotes: 1

Related Questions