Solved

VB.NET global array? Or alternate solution...

Posted on 2003-10-23
4
404 Views
Last Modified: 2012-08-14
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!
0
Comment
Question by:GenericUserName
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 200 total points
Comment Utility
I'm not sure that VB.NET supports global arrays................

and in the case it supports them, I believe that there are different ways to achive it.

If I were you I would have a class with a static variable representing the array.

for example.....

public class GlobalVariables
{
      public static int[] integerArray = new int[10];
}

and for example, you would access the first element with

GlobalVariables.integerArray[0];

Hope this helps

Tincho
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
In your case you would have

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.logEntryArray[0].description );
MessageBox.Show( GlobalVariables.logEntryArray[0].lineNumber.ToString() );
MessageBox.Show( GlobalVariables.logEntryArray[0].moduleName );
0
 
LVL 9

Expert Comment

by:tinchos
Comment Utility
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

0
 
LVL 1

Author Comment

by:GenericUserName
Comment Utility
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!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now