Solved

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

Posted on 2003-10-23
4
428 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 200 total points
ID: 9609042
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
ID: 9609088
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
ID: 9609104
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
ID: 9609507
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

690 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