How do I use a Structure in multiple classes? - vb.net

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

Related

Global array in module file

I have issue
file 1:
Module1 - file name
In this file I need to declare global array, something like
Dim array1(100) As Integer
And in another file, this is UserForm - I need change values of this array
Module1.array1(2) = 1995 //for example
How can I do it?
I have a compile error: wrong number of dimensions
When you want to define a global or public variable, you need to (a) define it outside of a function/sub definition, and (b) use global or public.
So in your module:
Public array1(100) As Integer
And in your form:
array1(0) = 1995

Variable structure visibility in vb.net

I've create a structure in my program like this:
Public Structure Team_Data
Public Shared punti_home As Integer
Dim punti_away As Integer
Dim goal_fatti As Integer
Dim goal_subiti As Integer
End Structure
I use the variable of this structure in a Width for valorize it after a regex parser control, and I don't encounter problems. But if I would use the structure variable in a function like a parameter, so:
pressure(punti_home)
The compiler tells me that the variables isn't declared. Why happean this?
You would need to refer to it as Team_Data.punti_home.

Create global variable in Visual Studio 2008

I have a program which needs user authentication. Now I store the logonname in a Public String, but I want to use more information about the user, like what language he chose to use, his name and stuff like this. I could create another Public String but I don't like that idea.
My goal is to save some data about the user when he enters his username and password.
I want to create something like this:
user.logonname
user.language
I thought a structure will do the trick, so I created this:
Public Structure user
Public logonname As String
Public languagetype As String
End Structure
But I can only access it like this:
Dim user1 as new user
But this cannot overwrite the Public Structure, just create an instance of it, therefore other forms won't have the information I need.
I saw something like this in VB6, but that program was using a DLL, containing this type of variable and I don't really understand it and I'm sure there is a better way.
Can someone help me with this?
Thanks in advance.
Maybe this will help you in your project. Good luck!
Public Type EmployeeName
FirstName As String
MidInit As String
LastName As String
End Type
Public Type EmployeeRecord
udtEmpName As EmployeeName
dtmHireDate As Date
sngHourlyRate As Single
dblQuarterlyEarnings(1 To 4) As Double
End Type
Dim udtEmpRec As EmployeeRecord
You would reference the EmployeeName fields as follows:
udtEmpRec.udtEmpName.FirstName
udtEmpRec.udtEmpName.MidInit
udtEmpRec.udtEmpName.LastName
Thanks to Plutonix, I figured it out. If someone needs it, I did it like this:
Public Module GeneralModule
Public Structure user
Public logonname As String
Public languagetype As String
End Structure
Public guser As New user
End Module
And I can access guser anywhere I want.
Example:
Private Function login
'Some other code
guser.logonname = Me.userid.text
guser.languagetype = Me.ComboBoxLanguage.Text
End Function
Create a module and declare your public variables in that module. Those variables will be available to the whole project then

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

Directories that exist - Visual Basic 2008

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