GenericUserName
asked on
VB.NET global array? Or alternate solution...
Alright, I'm finally moving to .NET (have used VS6 awhile), and am having some trouble thinking this through. I'm running into a problem with using a global-esque variable array. The end goal is to be able to store an array of application log entries, to give a time-based log of what the application is doing, for debugging purposes. In VB6, this was easily accomplished by throwing up a global variable array and accessing it. I can't figure out for the life of me how to best accomplish this with .NET.
Basically, I have an array of log entries, defined by:
Public Structure LogEntry
Public Enum _EntryType. . .
Public Timestamp As DateTime
Public EntryType As _EntryType
Public FileName As String
Public ModuleName As String
Public LineNumber As Integer
Public Description As String
End Structure
Then, I'm instantiating a variable array in a class:
Public Shared _log() As LogEntry = New LogEntry(0) {}
And then access it from other classes via the shared property. I've also tried keeping it private, and passing it by reference to other classes, but that didn't work either. Basically, it simply doesn't retain all the array entries I add to it; I add like 6-7 entries, and when stepping through they seem to add to the array, until it returns to the main class, when it knocks it down to 2 entries.
So, I need to know the best way this should be accomplished, as VB.NET seems to offer no alternative to the Global keyword. The only other thing I can think of is to write my own LogEntry class, and keep the array as a static member of that class, and simply reinstantiate that class in each other class of my application. If this is the case, can someone give me a pointer (God, I wish I could be using C pointers for this...) to what I need to do? Or if you can think of a better solution for this?
Thanks in advance!
Basically, I have an array of log entries, defined by:
Public Structure LogEntry
Public Enum _EntryType. . .
Public Timestamp As DateTime
Public EntryType As _EntryType
Public FileName As String
Public ModuleName As String
Public LineNumber As Integer
Public Description As String
End Structure
Then, I'm instantiating a variable array in a class:
Public Shared _log() As LogEntry = New LogEntry(0) {}
And then access it from other classes via the shared property. I've also tried keeping it private, and passing it by reference to other classes, but that didn't work either. Basically, it simply doesn't retain all the array entries I add to it; I add like 6-7 entries, and when stepping through they seem to add to the array, until it returns to the main class, when it knocks it down to 2 entries.
So, I need to know the best way this should be accomplished, as VB.NET seems to offer no alternative to the Global keyword. The only other thing I can think of is to write my own LogEntry class, and keep the array as a static member of that class, and simply reinstantiate that class in each other class of my application. If this is the case, can someone give me a pointer (God, I wish I could be using C pointers for this...) to what I need to do? Or if you can think of a better solution for this?
Thanks in advance!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Note: This code is in C#
I believe that the only differences are in the syntax. But all this is also supported in VB.NET
Apart from that, I find C# easier to code..... so I would really consider it.
Hope this helps
Tincho
I believe that the only differences are in the syntax. But all this is also supported in VB.NET
Apart from that, I find C# easier to code..... so I would really consider it.
Hope this helps
Tincho
ASKER
Alright, thanks. I wound up creating:
---
Public Class Globals
Public Shared Log() As LogEntry = New LogEntry(0) {}
Private Shared _entry As LogEntry
Public Shared Sub AddEntry(ByVal eType As LogEntry._EntryType, ByVal eDesc As String, _
ByVal eFile As String, ByVal eLine As Integer, ByVal eModule As String)
_entry = New LogEntry()
_entry.Timestamp = Now()
_entry.EntryType = eType
_entry.Description = eDesc
_entry.FileName = eFile
_entry.LineNumber = eLine
_entry.ModuleName = eModule
Log(Log.Length - 1) = _entry
ReDim Preserve Log(Log.Length)
End Sub
End Class
---
This way, it's not only better encapsulated for safety, it's also easier to add an Entry, so it's better overall. I don't know why I missed that in my thinking, but oh well, thanks a bunch :)
And I'm currently learning C# (I know Java and C well, so it's an easy transition). However, my coworkers use VB, so for code readability, it's easier for a multiple-developer environment. However, we will be porting almost everything to C# soon, but are not willing to fully migrate to .NET until MATLAB is able to port to .NET, as we have lots of processing code utilizing MATLAB. In this current project, I'm writing each class in VB and then rewriting it in C# once it works correctly, so luckily we're moving in the right direction.
But thanks again!
---
Public Class Globals
Public Shared Log() As LogEntry = New LogEntry(0) {}
Private Shared _entry As LogEntry
Public Shared Sub AddEntry(ByVal eType As LogEntry._EntryType, ByVal eDesc As String, _
ByVal eFile As String, ByVal eLine As Integer, ByVal eModule As String)
_entry = New LogEntry()
_entry.Timestamp = Now()
_entry.EntryType = eType
_entry.Description = eDesc
_entry.FileName = eFile
_entry.LineNumber = eLine
_entry.ModuleName = eModule
Log(Log.Length - 1) = _entry
ReDim Preserve Log(Log.Length)
End Sub
End Class
---
This way, it's not only better encapsulated for safety, it's also easier to add an Entry, so it's better overall. I don't know why I missed that in my thinking, but oh well, thanks a bunch :)
And I'm currently learning C# (I know Java and C well, so it's an easy transition). However, my coworkers use VB, so for code readability, it's easier for a multiple-developer environment. However, we will be porting almost everything to C# soon, but are not willing to fully migrate to .NET until MATLAB is able to port to .NET, as we have lots of processing code utilizing MATLAB. In this current project, I'm writing each class in VB and then rewriting it in C# once it works correctly, so luckily we're moving in the right direction.
But thanks again!
public class LogEntry
{
public string description;
public string moduleName;
public int lineNumber;
// Rest of the class
}
public class GlobalVariables
{
public static LogEntry[] logEntryArray = new LogEntry[10];
}
And for instance to have a messageBox with the content of the first element of the array you would access it like this
MessageBox.Show( GlobalVariables.logEntryAr
MessageBox.Show( GlobalVariables.logEntryAr
MessageBox.Show( GlobalVariables.logEntryAr