Answered by:
Using Functions

Question
-
I have reiseved this function on an previos thread but I dont know how to use functions.
This might sound stuped, but how do I get it to do its thing by forinsance clicking a button?
Function GetImageNames(ByVal num As Integer) As String() Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim filter As String = "* " & num.ToString("00000") & ".png" Return IO.Directory.GetFiles(folder, filter).Select(Function(s) IO.Path.GetFileName(s)).ToArray End Function
Hendri Bissolati noviceprogrammer@vodamail.co.zaThursday, August 25, 2011 2:48 PM
Answers
-
Hello Hendri Bissolati,
I have reiseved this function on an previos thread but I dont know how to use functions.
This might sound stuped, but how do I get it to do its thing by forinsance clicking a button?
Function GetImageNames(ByVal num As Integer) As String() Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim filter As String = "* " & num.ToString("00000") & ".png" Return IO.Directory.GetFiles(folder, filter).Select(Function(s) IO.Path.GetFileName(s)).ToArray End Function
Hendri Bissolati noviceprogrammer@vodamail.co.za
an simple example for to use your FunctionPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.TextBox1.Text = GetImageNames(2).ToString End Sub
where I put in as a parameter to a two, but you can enter a number you think is useful for your research
Regards.
Carmelo La Monica http://community.visual-basic.it/carmelolamonica/
WordPress http://carmelolamonica.wordpress.com/
- Marked as answer by Hendri Bissolati Thursday, August 25, 2011 3:27 PM
Thursday, August 25, 2011 3:05 PM
All replies
-
Hello Hendri Bissolati,
I have reiseved this function on an previos thread but I dont know how to use functions.
This might sound stuped, but how do I get it to do its thing by forinsance clicking a button?
Function GetImageNames(ByVal num As Integer) As String() Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim filter As String = "* " & num.ToString("00000") & ".png" Return IO.Directory.GetFiles(folder, filter).Select(Function(s) IO.Path.GetFileName(s)).ToArray End Function
Hendri Bissolati noviceprogrammer@vodamail.co.za
an simple example for to use your FunctionPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.TextBox1.Text = GetImageNames(2).ToString End Sub
where I put in as a parameter to a two, but you can enter a number you think is useful for your research
Regards.
Carmelo La Monica http://community.visual-basic.it/carmelolamonica/
WordPress http://carmelolamonica.wordpress.com/
- Marked as answer by Hendri Bissolati Thursday, August 25, 2011 3:27 PM
Thursday, August 25, 2011 3:05 PM -
Hi Hendri,
That function is returning an array of type STRING as indicated by
As String()
at the end of the Function. Note the parentheses or rounded brackets.
So I would do this in case you get more than one string returned in the returned array.
In the example given above you will only get
System.String[]
being shown in TextBox1.
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim imageNamesArray() As String imageNamesArray = GetImageNames(1) If imageNamesArray.Count > 0 Then For index As Integer = 0 To imageNamesArray.GetUpperBound(0) MessageBox.Show(imageNamesArray(index).ToString)) Next End If End Sub Function GetImageNames(ByVal num As Integer) As String() Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim filter As String = "* " & num.ToString("00000") & ".png" Return IO.Directory.GetFiles(folder, filter).Select(Function(s) IO.Path.GetFileName(s)).ToArray End Function End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
- Proposed as answer by Carmelo La Monica Thursday, August 25, 2011 9:45 PM
Thursday, August 25, 2011 9:43 PM -
Hi Hendri,
That function is returning an array of type STRING as indicated by
As String()
at the end of the Function. Note the parentheses or rounded brackets.
So I would do this in case you get more than one string returned in the returned array.
In the example given above you will only get
System.String[]
being shown in TextBox1.
Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim imageNamesArray() As String imageNamesArray = GetImageNames(1) If imageNamesArray.Count > 0 Then For index As Integer = 0 To imageNamesArray.GetUpperBound(0) MessageBox.Show(imageNamesArray(index).ToString)) Next End If End Sub Function GetImageNames(ByVal num As Integer) As String() Dim folder As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments Dim filter As String = "* " & num.ToString("00000") & ".png" Return IO.Directory.GetFiles(folder, filter).Select(Function(s) IO.Path.GetFileName(s)).ToArray End Function End Class
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.
As soon as I place this 3 lines of code on form1 it gives me 22 errors allover form1.Option Strict On Option Explicit On Option Infer Off
Hendri Bissolati noviceprogrammer@vodamail.co.zaFriday, August 26, 2011 10:38 AM -
Hendri,
please read this: http://msdn.microsoft.com/en-us/library/zcd4xwzs(v=VS.90).aspx
so you will understand why it is good practice to set Option Strict On
Hannes
If you have got questions about this, just ask.
In a perfect world,
users would never enter data in the wrong form,
files they choose to open would always exist
and code would never have bugs.
C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/Friday, August 26, 2011 10:52 AM -
As soon as I place this 3 lines of code on form1 it gives me 22 errors allover form1.
Option Strict On Option Explicit On Option Infer Off
Hendri Bissolati noviceprogrammer@vodamail.co.zaHi again Hendri,
Please post your entire code for Form1, thanks. :-)
Regards,
Click this link to see the NEW way of how to insert a picture into a forum post.
Installing VB6 on Windows 7
App Hub for Windows Phone & XBOX 360 developers.Sunday, August 28, 2011 3:30 AM