![]() |
![]() |
#1 (permalink) |
Pure Chewing Satisfaction
Location: can i use bbcode [i]here[/i]?
|
[VB.Net] Overloading a class' Constructor
I want to write a class that was two different constructors. One version of the constructor takes a string parameter, the other thakes an array of strings. My plan was to have the first version *create* an array of strings of size one (containing the one string that was passed in), then call the other constructor with it.
However, two things stand in my way:
So, what am I left to do? Is my only option to fully write out what I want each constructor to do? I suppose I could have them both call a utility function to do what has to be done... but this is kinda annoying. I guess my problem is that I don't see why this can't be done... seems like a basic thing. Here's an example of what I'm trying to do (just doing this shorthand, might have syntax errors)... Constructor that takes array: Code:
Public Sub New(ByVal myStrings() as String) 'Do stuff End Sub Code:
Public Sub New(ByVal myString as String) Dim temp() as String = { myString } Me.New(temp) End Sub And the second option is to somehow make that array and do the call in one line to get around the issues. Well, more of rant than anything else, I guess. Any thoughts?
__________________
Greetings and salutations. |
![]() |
![]() |
#2 (permalink) |
Insane
Location: Michigan
|
Here is the answer you are lookin for
![]() Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load mysub("asdf") End Sub Public Sub mysub(ByVal myStrings() As String) 'Do stuff End Sub Public Sub mysub(ByVal myString As String) Me.mysub(New String() {myString}) End Sub
__________________
Patterns have a habit of repeating themselves. |
![]() |
![]() |
#3 (permalink) |
Pure Chewing Satisfaction
Location: can i use bbcode [i]here[/i]?
|
For some reason, I've never known that you could do: Me.mysub(New String() {myString}) I've tried and tried to create arrays on the fly, and I guess I've never typed it like that. I think I might have been putting an equals sign in there, as in Me.mysub(New String() = {myString})
*sigh*.... why oh why can't my job just let me do C#? thanks.
__________________
Greetings and salutations. |
![]() |
Tags |
class, constructor, overloading, vbnet |
|
|