Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2003-10-23
4
Medium Priority
?
446 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 800 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

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

810 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