how to create a method to generate the name you want in VB

Alright so I have a project which contains Name, ModuleID, type and day. The ModuleID, Type and day are in a combobox format which the user can chose specific input from.

However the Name depends on these 3, it should be in the following format

(ModuleID)/(first letter of the Type with number 1 or 2)/Day

I'm trying to generate a function to do it but i cant even do half of it

Public Function generateName(ByVal len As Integer)
    Dim s As String
    Dim s1 As String
    s = txttype.SelectedItem
    s1 = s.ToString.Substring(0, 1)
    txtname.Text = txtmoduleID.ToString + s1
    Return txtname.Text
End Function

any ideas because i cant find anything online that helps me with that

Upvotes: 1

Views: 54

Answers (2)

beercohol
beercohol

Reputation: 2587

I would do something simple like this:

Public Shared Function generateName(moduleId As String, type As String, day As String, typeSuffix As String)
    Return String.Format("{0}/{1}{2}/{3}", moduleId, type.Substring(0, 1), typeSuffix, day)
End Function

Call it like this:

    Dim myName As String = generateName(cboModuleID.SelectedItem, cboType.SelectedItem, cboDay.SelectedItem, "1")

Keeping your combo box references outside the function helps keep your business logic and UI code separate, and a Shared (static in C#) function makes the code more efficient. You can put it in a Shared utility class.

Note that this code has no validation, and would throw an error if any of the combo boxes do not have items selected!

Upvotes: 0

Jmnstr
Jmnstr

Reputation: 80

Try this?

    Dim s As String = ""
    Dim mID As String = "123" 'ModuleID (txtModuleID.SelectedItem)
    Dim Type As String = "ExampleType" 'Type (txttype.SelectedItem)
    Dim Day As String = "Monday" 'Day (txtDay.SelectedItem)

    s += mID
    s += Type.Substring(0, 1) & "1" 'or 2?
    s += Day

    Return s '123E1Monday

Sorry if this isn't what you're looking for. I did the best I could with your description. Let me know if this didn't quite answer your question.

Upvotes: 1

Related Questions