Directories that exist - Visual Basic 2008 - vb.net

How would I be able to make a variable that holds the value of the directories that exist in the C:\?

The DirectoryInfo class has member functions to do that - GetDirectories in this case.
Imports System.IO
Dim fTarget As New IO.DirectoryInfo("C:\")
Dim arrAnswer as DirectoryInfo()
arrAnswer = fTarget.GetDirectories()

The simplest option is to call GetDirectories on the static type Directory. This can be found in System.Io. It returns a string array of directories that it finds. You can also specify if it should drill down into subcategories when it is called
See msdn for more info http://msdn.microsoft.com/en-us/library/system.io.directory.getdirectories.aspx

Related

OOP pattern for sharing reference (using VBA)

I'm starting to learn OOP so forgive me (or offer suggestions) if my UML is not keeping to standards.
Currently writing in VBA due to business restrictions so I don't have access to inheritance but it does have support for events and interfaces.
I have a Folder class (in blue) which has an object mFileSystemObject of type FileSystemObject
The Folder class holds many File objects in an array called Files.
Each File object has a FileDetails object (green).
I'd like to use the functions from the FileSystemObject class in the FileDetails class.
I'd like to use the same instance of the FileSystemObject in the Folder class as I don't want to instanciate a new one for every FileDetails class and fill up the heap and I'm not doing any work concurrently.
So I'd like to pass the reference to the FileDetails class so they all use the same one.
The only way I can think of doing this is to pass it through the File object even though File will not use it which just seems a bit ugly.
My question is - is there a pattern that solves this "passing through" problem - I've searched online and looked at several design patterns but they don't seen to offer any suggestions for a solution.
Any help would be greatly appreciated.
Many Thanks
Nick
So this would be a singleton pattern; there are a few ways to do this in VBA. I'll give you two.
1)
Create a new Module, let's call it FileDetailsBuilder. In this module, give it a private instance of your FileDetails class. Then expose a public function that will return its instance , or create a new one if it doesn't exist.
Option Explicit
Private mFD As FileDetails
Public Function GetObject() As FileDetails
If mFD Is Nothing Then
Set mFD = New FileDetails
End If
Set GetObject = mFD
End Function
Now when you create new File objects, you can set their FileDetails member using a line of code like this:
Set mFileDeatils = FileDetailsBuilder.GetObject()
The mFD instance member will live on as long as your application is running.
2)
You can alter some code in your FileDetails class to make it be treated like a single, pre-declared object. To do this, you need to export the class and alter some code that you can't see from the VBE.
In a text editor, at the top of the .cls file that you exported, you would see some attribute assignments. One of which is VB_PredeclaredId. Well my friend, you can edit this .cls file and set that attribute to true:
Attribute VB_PredeclaredId = True
Save the file, and import it back into your VBE project. Now, you don't have to instantiate this object. Just use it like a static object. When you create new File objects, assign their member like this:
Set mFileDeatils = FileDetails '// note the lack of new keyword

ZXing.NET EAN8Writer getChecksum

I want to check my (bar)code or get missing checksum number before creating a barcode picture in VB.NET. This is critical parts of minimized example on how this look like:
Imports ZXing
Imports ZXing.Common
Imports ZXing.OneD
...
Dim writer As EAN8Writer = New EAN8Writer
Dim data As String = "1234567" '(0)
Dim check As Integer = UPCEANReader.getStandardUPCEANChecksum(data)
I find that part of code in a various examples on the net. But on my system I get error 'getStandardUPCEANChecksum is not a member of UPCEANReader'. Why this don't work as expected?
Is here any other way to get checksum for such case except to calculate it manually?
The method UPCEANReader.getStandardUPCEANChecksum() is declared as "internal" and can't be accessed from outside the library (without using reflection or similar stuff).
You can copy the source code into your own application if you want to use it.

How to select an image from resources via a string?

I'm coding a pokedex type deal as practice for my class.
Basically, I have a class titled "pokemon". One of the properties of the class is "ImgName" Which I want to use to display an image from the resources with the same name.
VB doesn't allow me to call the ImgName as a string and then use 'My.Resources.ImgName'
How can i do this, or what are some alternative options to it? I want it to be determined by a property in the pokemon object, and i don't want to have to hard code in an if-elseif statement for every single pokemon.
One way is you can have a resource file added to your project. Then drop the resource into it. You will be able to address it like this:
My.Resources.Resource1.ImgName
Resource1 is your resource file name, and ImgName is the resource name here. But you need to do hard code for every call. However, you get full intellisense support with type checking.
If you don't want hard code, here is a stripped down version of my production code:
Imports System.Reflection
Imports System.Xml.Linq
Public Class EmbeddedResourceManager
Private Class EmbeddedResourceManagerCore
Private Shared _executingAssembly As Assembly
Private Shared _resourcePrefix As String
Shared Sub New()
_executingAssembly = Assembly.GetExecutingAssembly
_resourcePrefix = _executingAssembly.GetName.Name & "."
End Sub
Public Shared Function GetStream(resourceRelName As String) As IO.Stream
Return _executingAssembly.GetManifestResourceStream(_resourcePrefix & resourceRelName)
End Function
End Class
Public Shared Function GetImage(ByVal resourceName As String) As Bitmap
Return New Bitmap(EmbeddedResourceManagerCore.GetStream(resourceName))
End Function
End Class
So whenever you need, just call EmbeddedResourceManager.GetImage and pass the resource name, as it appears in your project (your image file needs to be attached to a project). You need to have Build Action for an image in question to be set to Embedded Resource.
This piles up all your resource into an executable, which has both benefits and drawbacks, depending on the situation. Still, it should work for your needs, since I am assuming number of different pokemons is limited and does not change throughout the game (i.e. downloaded from a 3rd party server in real time etc.).
BackgroundImage = My.Resources.ResourceManager.GetObject(aString)
10 time easier than previous answer imho

How do I use a Structure in multiple classes?

I want to use this Structure in multiple .vb files within my project:
Structure MyStruct
Dim Name As String
Dim Good As Boolean
End Structure
If I define it in one file, the other files can't use it. How do I define it globally?
Make this Structure Public
Public Structure MyStruct
Dim Name As String
Dim Good As Boolean
End Structure
and add this Structure in a Module File so that it can be accessed from anywhere, but give the Module name as a namespace in your class
using Module1.vb

How to prevent VBA variables from being shared across Word documents?

I have a VBA template project that runs automatically when a Word document is opened. However, if I open multiple documents, they all share the variables values. How can declare these variables to be only associated with the active window or active document?
I tried declaring them in a Class Module, but that did not help. Switching between opened document I can see that these variables are shared.
Any input is appreciated...
This what I have in my Module:
Option Private Module
Dim CurrentCommand As String
Public Function SetCurrentCommand(command)
CurrentCommand = command
End Function
Public Function GetCurrentCommand()
GetCurrentCommand = CurrentCommand
End Function
More Info: The code/Macro start at AutoExec like this:
Public Sub Main()
Set oAppClass.oApp = Word.Application
If PollingRate <> "" Then Application.OnTime Now + TimeValue(PollingRate), "CaptureUserViewState"
End Sub
And the CaptureUserViewState is a Sub that resides in a different Module and does all teh checks (comparing new values to last recorded ones) and here how this Sub does the check:
If WL_GetterAndSetter.GetLastPageVerticalPercentage <> pageVerticalPercentScrolled Then
'Update the last value variable
WL_GetterAndSetter.SetLastPageVerticalPercentage (pageVerticalPercentScrolled)
'log change
End If
You don't give us much information, but I assume you declared public variables at module level like this:
Public myString As String
Public myDouble As Double
From VBA documentation:
Variables declared using the Public statement are available to all procedures in all modules in all applications unless Option Private Module is in effect; in which case, the variables are public only within the project in which they reside.
The answer is to use Option Private Module.
When used in host applications that allow references across multiple projects, Option Private Module prevents a module’s contents from being referenced outside its project.
[...] If used, the Option Private statement must appear at module level, before any procedures.
EDIT You have now clarified that you declare your variables using Dim at module level. In this case, Option Private Module is irrelevant.
Variables declared with Dim at the module level are available to all procedures within the module.
i.e. regardless of whether you're using Option Private Module or not.
If you're finding that the values are retained between runs, then that must be because you are running a procedure from the same module from the same workbook. You may think you're doing something else, but in reality this is what you're doing.
EDIT
In your class module, instead of Dim CurrentCommand As String try Private CurrentCommand As String. Without more information it's hard to debug your program. I'm just taking random potshots here.
What you need to do is store multiple versions of the variables, one set per document.
So I would suggest that you create a simple class to hold the different values.
You then store them in a collection mapping the data-set with the document name or similar as the key.
In classmodule (MyData), marked as public:
Public data1 as String
Public data2 as Integer
In module with the event-handlers:
Dim c as new Collection 'module global declaration
Sub AddData()
Dim d as new MyData 'Your data set
d.data1 = "Some value"
d.data2 = 42
c.add Value:=d, Key:=ActiveDocument.name
End Sub
Then when you enter the event-handler you retrieve the data and use the specific set for the currently active document.
Sub EventHandler()
Dim d as MyData
set d = c.item(ActiveDocument.name)
'use data
'd.data1...
End Sub
Please not that this code is just on conceptual level. It is not working, You have to apply it to your problem but it should give you some idea on what you need to do. You will need to add alot of error handling, checking if the item is already in the collection and so on, but I hope you understand the concept to continue trying on your own.
The reason for this is because, as I understand the situation from your question, you only have one version of your script running, but multiple documents. Hence the script have to know about all the different documents.
On the other hand, If each document would have their own code/eventhandlers, hence having multiple versions of the script running, then you don't need the solution provided above. Instead you need to be careful what document instance you reference in your script. By always using "ThisDocument" instead of "ActiveDocument" you could achieve isolation if the code is placed in each open document.
However, as I understood it, you only have one version of the script running, separate from the open documents, hence the first solution applies.
Best of luck!
You might want to store the Document Specific details using
The Document.CustomDocumentProperties Property
http://msdn.microsoft.com/en-us/library/office/aa212718(v=office.11).aspx
This returns a
DocumentProperties Collection
Which you can add new Properties to Using
Document.CustomDocumentProperties.Add(PropertyName, LinkToContent, Value, Type)
And then Read From using
Document.CustomDocumentProperties.Item(PropertyName)
A downside, or bonus, here is that the properties will remain stored in the document unless you delete them.
This may be a good thing or a bad thing