Hack Forums
[.NET] How to create new constructor? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] How to create new constructor? (/showthread.php?tid=5415791)



How to create new constructor? - SirNoobHack - Sep 25, 2016

Code
My.Computer.FileSystem.CreateDirectory("adasd")
        My.Computer.FileSystem.CreateDirectory("afasfas")
        My.Computer.FileSystem.CreateDirectory("dsgdsf")
        My.Computer.FileSystem.CreateDirectory("adasdsasd")
        My.Computer.FileSystem.CreateDirectory("adaaaf")
        My.Computer.FileSystem.CreateDirectory("asdadassd")
        My.Computer.FileSystem.CreateDirectory("gdfsgdfg")
        My.Computer.FileSystem.CreateDirectory("adasfs")
        My.Computer.FileSystem.CreateDirectory("afafssaf")
        My.Computer.FileSystem.CreateDirectory("afsafgsd")

How can I shorten the class length if you understand what I mean.
Dim CreateD() = My.Computer.FileSystem.CreateDirectory(Of String) was the best I could think of but it was incorrect. I don't even know what it's called that I'm trying to do :/
So to create a new directory I would only have to type:
CreateD(folderName)

Any sort of help would be GREATLY appreciated! 😉


* - assault0017 - Sep 25, 2016

Code
for each dir as string in {"ads", "afd", "asopfdj", etc...}
    my.co....createdirectory(dir)
next

?


* - My Computer - Sep 25, 2016

Do you mean something like this OP?

Code
Public Sub CreateDirectory(ByVal directory As String)
        Try
            My.Computer.FileSystem.CreateDirectory(directory)
        Catch ex As Exception
            Console.WriteLine("Exception thrown while trying to create a directory. Exception message " + ex.Message)
        End Try
    End Sub

Then you would just call it like this:

Code
CreateDirectory("C:\Users\Owner\Desktop\Test\")



* - SirNoobHack - Sep 25, 2016

(Sep 25, 2016 11:45 AM)My Computer Wrote:  Do you mean something like this OP?

Code
Public Sub CreateDirectory(ByVal directory As String)
        Try
            My.Computer.FileSystem.CreateDirectory(directory)
        Catch ex As Exception
            Console.WriteLine("Exception thrown while trying to create a directory. Exception message " + ex.Message)
        End Try
    End Sub

Then you would just call it like this:

Code
CreateDirectory("C:\Users\Owner\Desktop\Test\")

I thought about it but it wasn't really what I wanted, I must've thought that you could do what I've imagined in some way. Creating a method to make it is the best solution though, cheers mate!


* - idb. - Sep 25, 2016

Code
Public Sub CreateDirectory(ParamArray path As String())
    Array.ForEach(path, Sub(p) If Not Directory.Exists(p) Then Directory.CreateDirectory(p))
End Sub

ParamArrays are your friend.

Code
CreateDirectory("C:\Foo1")
CreateDirectory("C:\Foo1", "C:\Foo2")
CreateDirectory("C:\Foo1", "C:\Foo2", "C:\Foo3")