Answered by:
Public Sub

Question
-
User1636054312 posted
Hello,
i'm quite new to asp.net using vb.
at the moment i've got created a sub to write some actions to a xml log file.
everything works fine, the only thing is that i need the source code on multiple pages, is there a way to include this code from a sepperate file, so when i've got some changes to make to the sub i only have to change it once?
Code:
Private sub WriteXML(ByVal XMLAction As String, ByVal XMLType As String) Dim f As File Dim FileName As String = Server.MapPath("log\log.xml") Dim lines As List(Of String) = New List(Of String)(File.ReadAllLines(FileName)) 'Remove Last Line lines.RemoveAt(lines.Count - 1) File.WriteAllLines(FileName, lines.ToArray()) Dim fstream As FileStream If f.Exists(FileName) Then fstream = New FileStream(FileName, FileMode.Append, FileAccess.Write) Else fstream = New FileStream(FileName, FileMode.Create, FileAccess.ReadWrite) End If Dim sWriter As New StreamWriter(fstream) 'Write Lines To XML File sWriter.Write("<event>") sWriter.Write(Environment.NewLine) sWriter.Write("<Type>" + XMLType + "</Type>" + Environment.NewLine) sWriter.Write("<User>" + Session("User") + "</User>" + Environment.NewLine) sWriter.Write("<Speaker>" + "" + "</Speaker>" + Environment.NewLine) sWriter.Write("<Session>" + "" + "</Session>" + Environment.NewLine) sWriter.Write("<Presentation>" + "" + "</Presentation>" + Environment.NewLine) sWriter.Write("<Date>" + now() + "</Date>" + Environment.NewLine) sWriter.Write("<Info>" + XMLAction + "</Info>" + Environment.NewLine) sWriter.Write("</event>" + Environment.NewLine) sWriter.Write("</log>") sWriter.Close() End Sub
Thanx in advanced
Best Regards,
Eltjo Schol
Friday, October 1, 2010 2:58 AM
Answers
-
User-660870441 posted
When i put the function as shared sub in my MasterPage.Master.vb i get the following error:
BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
On the following line: Dim FileName As String = Server.MapPath("log\log.xml")
Thanx So far :)
Hi,
To resolve this issue, please change this code to:
Dim FileName As String = HttpContext.Current.Server.MapPath("log\log.xml")
After defined the method as shared, then you can call it by this way:
MasterPage.WriteXML()
If you like, you can create a new class to hold this method.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 4, 2010 11:42 PM -
User-952121411 posted
You can create a C# classRight idea but this is a VB.NET forum
is there a way to include this code from a sepperate file, so when i've got some changes to make to the sub i only have to change it once?Good question - the process here is to create a class (or module, pretend I didn't say module) and mark it with an access level that can be seen throughout the application. The easiest way to do this is to create a 'Public' class and mark the methods within it 'Public Shared' like the code in the following block:
Public Class MyMethods Public Shared Function GetWinUserInformation() As String Return System.Security.Principal.WindowsIdentity.GetCurrent.Name() End Sub End Class
Now from your .aspx page .vb behind you will be able to call it using code like you see below:Dim UserInfo As String = String.Empty 'Call shared method on 'MyMethods' class UserInfo = MyMethods.GetWinUserInformation() 'Maybe post the name to the string Me.lblUser.Text = UserInfo
As mentioned you can place this class in the 'App_Code' folder, but in reality it can go anywhere in your project. Just right-click your project in the VS.NET Solution Explorer IDE and select Add -> New Item. Add a class in your project and if you use the proper 'Access Level' on your class methods, you will be able to call it from anywhere you need. Shared methods in public classes do not require instantiation either; you can call them directly as I did above.For more information on the access levels in VB.NET, please read the following:
Access Levels in Visual Basic:
http://msdn.microsoft.com/en-us/library/76453kax.aspx
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 5, 2010 3:48 PM
All replies
-
User1872574491 posted
Hi,
Just put this function in any class and make it as shared.
So it will be accessed with ease.
Friday, October 1, 2010 3:41 AM -
User1636054312 posted
When i put the function as shared sub in my MasterPage.Master.vb i get the following error:
BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
On the following line: Dim FileName As String = Server.MapPath("log\log.xml")
Thanx So far :)
Friday, October 1, 2010 3:47 AM -
User1872574491 posted
Hi,
what i mean put it in different class in app_code folder.Not on master page or page class.
Friday, October 1, 2010 3:52 AM -
User-660870441 posted
When i put the function as shared sub in my MasterPage.Master.vb i get the following error:
BC30369: Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
On the following line: Dim FileName As String = Server.MapPath("log\log.xml")
Thanx So far :)
Hi,
To resolve this issue, please change this code to:
Dim FileName As String = HttpContext.Current.Server.MapPath("log\log.xml")
After defined the method as shared, then you can call it by this way:
MasterPage.WriteXML()
If you like, you can create a new class to hold this method.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 4, 2010 11:42 PM -
User-1148431695 posted
Hi,
You can create a C# class inside App_Code folder and mark it as shared. Then add your function in the class and mark it as shared.
Then you can call the method from anywhere using the following syntax:
class.methodname
Monday, October 4, 2010 11:52 PM -
User-952121411 posted
You can create a C# classRight idea but this is a VB.NET forum
is there a way to include this code from a sepperate file, so when i've got some changes to make to the sub i only have to change it once?Good question - the process here is to create a class (or module, pretend I didn't say module) and mark it with an access level that can be seen throughout the application. The easiest way to do this is to create a 'Public' class and mark the methods within it 'Public Shared' like the code in the following block:
Public Class MyMethods Public Shared Function GetWinUserInformation() As String Return System.Security.Principal.WindowsIdentity.GetCurrent.Name() End Sub End Class
Now from your .aspx page .vb behind you will be able to call it using code like you see below:Dim UserInfo As String = String.Empty 'Call shared method on 'MyMethods' class UserInfo = MyMethods.GetWinUserInformation() 'Maybe post the name to the string Me.lblUser.Text = UserInfo
As mentioned you can place this class in the 'App_Code' folder, but in reality it can go anywhere in your project. Just right-click your project in the VS.NET Solution Explorer IDE and select Add -> New Item. Add a class in your project and if you use the proper 'Access Level' on your class methods, you will be able to call it from anywhere you need. Shared methods in public classes do not require instantiation either; you can call them directly as I did above.For more information on the access levels in VB.NET, please read the following:
Access Levels in Visual Basic:
http://msdn.microsoft.com/en-us/library/76453kax.aspx
Hope this helps!
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, October 5, 2010 3:48 PM -
User1636054312 posted
This was exact what i was looking for :)
Thanx for all the good Advise :) everything works right now
Friday, October 8, 2010 5:15 AM -
User-1148431695 posted
Hi,
Thanks for the correction. Since I am working on C#, I just wrote C# by mistake. Forgot that the issue was in VB.NET forum :)
Friday, October 8, 2010 5:42 AM