سؤال

I have been building various web based programs and things for a while, but am quite new to .NET and doing things "properly." As I am completely self taught, with help from sites like this and so on, my understanding of fundamentals is limited.

So, I have a series of functions which return data that I want depending on parameters put in, this is very basic stuff, and obviously all works. However, I am trying to make it easier to call these functions by using Classes.

So, say I have a function which returns a populated DropDownList converted to an HTML string

Function GetList(ListRequired as String) as String
    ' Do stuff to return a dropdownlist whos contend is determined by `ListRequired`, converted to an HTML string
End Function

In this example, it works fine, but to use it I must know what to enter for "ListRequired" to get what I want.

So, let's say, the options for the ListRequired para are "mastercategory", "brandlist", "priceranges" to return a different set of lists - each option would send the code off the retrieve information from a database and return accordingly.

Suppose I want a third party developer to be able to call this function with the most basic amount of "instruction" required, and not even have to tell him the list of available ListRequired by making it available as a Class.

Public Class DropDownLists

    Public Property MasterCategory
    Public Property BrandList
    Sub New()
        Me.MasterCategory = HTMLControls.RenderSearchFilters("mastercategory")
        Me.BrandList = HTMLControls.RenderSearchFilters("brandList")
    End Sub

End Class

A developer can then call this from Visual Studio/VWD etc very simply:

Dim dd As New DropDownLists
Dim list1Html as String = dd.MasterCategory 
Dim list2Html as String = dd.BrandList

Because VWD etc creates all the handy helpers and displays which properties the Class exposes, it is very easy to use this code without have to constantly refer to a manual.

However... when creating a new instance of the Class:

Dim dd As New DropDownLists

This will cause the server to process all the functions within the class which create the Properties, which would be desperately inefficient if there are lots of properties.

So I have tried using my own interpretation of the logic and written this:

Public Class DropDownLists

    Shared Property Master
    Shared Property Brand
    Sub New()
    End Sub

    Public Class MasterCategory 
        Sub New()
            DropDownLists.Master = HTMLControls.RenderSearchFilters("mastercategory")
        End Sub
    End Class

    Public Class BrandList
        Sub New()
            DropDownLists.Brand = HTMLControls.RenderSearchFilters("brandList")
        End Sub
    End Class

End Class

Hoping I'd be able to create the HTML for a Master Category drop down like:

Dim dd as New DropDownLists.MasterCategory

But that doesn't work, and upon reflection I think I can see why... it's not returning the string, but creating a new type.

So... my question is...

What is the correct way to achieve what I am looking for, which is, to be able to create these string outputs by typing

Dim dd As New DropDownLists
Dim list1Html as String = dd.MasterCategory 
Dim list2Html as String = dd.BrandList

Without having to pass potentially unknown string parameters, or causing ALL properties to be created each time the DropDownLists Class is created, ie, only run the code for the output I need.

هل كانت مفيدة؟

المحلول

I'm expanding my comment to give you a clearer idea of what I meant:

   Public Class DropDownLists
    Enum ListType
        Undefined
        MasterCategory
        Brandlist
    End Enum


    Public Shared Function GetList(ListRequired As ListType) As String

        Select Case ListRequired
            Case ListType.Brandlist
                Return . . . 
            Case ListType.MasterCategory
                Return . . .
            Case ListType.Undefined
                Throw New . . . .
        End Select


    End Function

End Class
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top