Answered by:
VB.NET equivalent

Question
-
User-744535898 posted
Hi to all,
I have a piece of code in C# which i would like to have in vb.net.
data.Add(new { name = fi.Name, url = "../../Shared/images/thumbs/" + fi.Name, size = fi.Length, lastmod = fi.LastAccessTime });
I am using Visual Studio 2005 professional edition but it is not working...actually i am trying to run a coolite sample but with vb.net
Following is the complete code in C#:
protected void Page_Load(object sender, EventArgs e) { string path = Server.MapPath("../../Shared/images/thumbs"); string[] files = System.IO.Directory.GetFiles(path); List<object> data = new List<object>(files.Length); foreach (string fileName in files) { System.IO.FileInfo fi = new System.IO.FileInfo(fileName); data.Add(new { name = fi.Name, url = "../../Shared/images/thumbs/" + fi.Name, size = fi.Length, lastmod = fi.LastAccessTime }); } this.Store1.DataSource = data; this.Store1.DataBind(); }
Please help...
Tuesday, December 29, 2009 5:04 AM
Answers
-
User-158764254 posted
The equivalent (in VB9 or above) is:Assuming that the OP might be using an older version of VS, a slightly more brute force approach may be required for him. We could create an explicit object to hold the file data like this:
Public Class MyFileData Private _name As String Private _url As String Private _size As Long Private _lastMod As Date Public Sub New(ByVal name As String, ByVal url As String, ByVal size As Long, ByVal lastMod As Date) _name = name _url = url _size = size _lastMod = lastMod End Sub Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Public Property Url() As String Get Return _url End Get Set(ByVal value As String) _url = value End Set End Property Public Property Size() As Long Get Return _size End Get Set(ByVal value As Long) _size = value End Set End Property Public Property LastMod() As Date Get Return _lastMod End Get Set(ByVal value As Date) _lastMod = value End Set End Property End Class
Then that pesky little line that won't compile due to its use of "With {...}" would change to this:data.Add(New MyFileData(fi.Name, "../../Shared/images/thumbs/" & fi.Name, fi.Length, fi.LastAccessTime))
Not as elegant, but it should play on older versions of VS/VB
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 29, 2009 11:26 AM
All replies
-
User42760572 posted
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim path As String = Server.MapPath("../../Shared/images/thumbs")
Dim files As String() = System.IO.Directory.GetFiles(path)
Dim data As New List(Of Object)(files.Length)
For Each fileName As String In files
Dim fi As New System.IO.FileInfo(fileName)
data.Add(New With {.name = fi.Name, .url = "whatever",.size = fi.length, .lastmod = fi.LastAccessTime})
Next
Me.Store1.DataSource = data
Me.Store1.DataBind()
End Sub
i dont knw what is "Me.Store1" is though.
Tuesday, December 29, 2009 7:13 AM -
User-744535898 posted
I have used the code that you gave but i am getting error message (Keyword does not name a type.) underlined for the statement :
data.Add(New With
Please help .....
Tuesday, December 29, 2009 9:42 AM -
User397347636 posted
The problem is that you need VB9 or above (VS 2008 or above) to compile the equivalent.
The equivalent (in VB9 or above) is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim path As String = Server.MapPath("../../Shared/images/thumbs") Dim files() As String = System.IO.Directory.GetFiles(path) Dim data As New List(Of Object)(files.Length) For Each fileName As String In files Dim fi As New System.IO.FileInfo(fileName) data.Add(New With {Key .name = fi.Name, Key .url = "../../Shared/images/thumbs/" & fi.Name, Key .size = fi.Length, Key .lastmod = fi.LastAccessTime}) Next fileName Me.Store1.DataSource = data Me.Store1.DataBind() End Sub
Tuesday, December 29, 2009 11:06 AM -
User-158764254 posted
The equivalent (in VB9 or above) is:Assuming that the OP might be using an older version of VS, a slightly more brute force approach may be required for him. We could create an explicit object to hold the file data like this:
Public Class MyFileData Private _name As String Private _url As String Private _size As Long Private _lastMod As Date Public Sub New(ByVal name As String, ByVal url As String, ByVal size As Long, ByVal lastMod As Date) _name = name _url = url _size = size _lastMod = lastMod End Sub Public Property Name() As String Get Return _name End Get Set(ByVal value As String) _name = value End Set End Property Public Property Url() As String Get Return _url End Get Set(ByVal value As String) _url = value End Set End Property Public Property Size() As Long Get Return _size End Get Set(ByVal value As Long) _size = value End Set End Property Public Property LastMod() As Date Get Return _lastMod End Get Set(ByVal value As Date) _lastMod = value End Set End Property End Class
Then that pesky little line that won't compile due to its use of "With {...}" would change to this:data.Add(New MyFileData(fi.Name, "../../Shared/images/thumbs/" & fi.Name, fi.Length, fi.LastAccessTime))
Not as elegant, but it should play on older versions of VS/VB
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 29, 2009 11:26 AM -
User42760572 posted
yes u need, VS 2008 or equivalent and .Net framework 3.0 or above. the c# code that u gave is also of the same
version, if u cant do that then go ahead and make properties as "mbanavige" suggested.
Wednesday, December 30, 2009 12:12 AM -
User-744535898 posted
Thanks to everyone .....
I have tested in VS 2008 New With statement works without any problems...
But as i have the VS 2005, I have created a class as suggested by mbanavige and its working...
Wednesday, December 30, 2009 9:23 AM