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
Related
After quite some frustrating research into VB auto properties. I decided to just ignore them. So I had this code.
Public Class Categoria
Private _nome As String
Public ReadOnly Property Nome As String
Get
Return _nome
End Get
End Property
Public Sub New(nome As String)
Me._nome = nome
End Sub
End Class
But Visual Studio, being helpful, suggested to use auto-properties. I agreed, I even tried that before but he offered help in the form of a click here and I will do it. So I did. And he did exactly the same code I was not having any luck with before, char by char.
Public Class Categoria
Public ReadOnly Property Nome As String
Public Sub New(nome As String)
Me.Nome = nome
End Sub
End Class
Can someone shed some light as to why the last piece of code fails to compile with error BC30126: 'ReadOnly' property must provide a 'Get'.?
Aparently VS is missing the same as I am, so I don't feel so stupid anymore.
As point out by #Heinzi and following a previous post he supplied.
How can I use the latest VB.NET language level in an ASP.NET web site project?
Updating the Microsoft.CodeDom.Providers.DotNetCompilerPlatform NuGet package fix it, in my dev machine at least.
When using vb.net, if code is contained inside "< >" signs, like a namespace, what is it telling the compiler to do? Also, what would these be signs be called when used like this?
To give clarity to the question; I know that parentheses "( )" are generally used for arguments and that brackets "[ ]" are used to declare a new type, but I cannot find what the less than/greater than signs do when used in a similar capacity.
I've looked through my reference books and attempted to research this through the internet but I haven't come up with an answer. Most likely because I don't know what exactly these would be named. I always results that talk about the relational operators, which is not what I'm looking for.
Here is an example of what I'm looking at:
Imports System.ComponentModel.Design
'<CLSCompliant(True)>
<System.ComponentModel.DefaultEvent("DataReceived")> _
Public Class SerialDF1forSLCMicroCon
Inherits MfgControl.AdvancedHMI.Drivers.DF1ForSLCMicroPLC5
Implements System.ComponentModel.IComponent
Implements System.ComponentModel.ISupportInitialize
Private Shared ReadOnly EventDisposed As New Object()
Public Event Disposed As EventHandler Implements System.ComponentModel.IComponent.Disposed
Protected m_synchronizationContext As System.Threading.SynchronizationContext
Specifically I am looking at the line that contains
<System.ComponentModel.DefaultEvent("DataReceived")> _
That is an attribute. It is a way of attaching metadata (additional information) to your code that can be queried later using reflection.
For example, let's say you have a series of classes (e.g. Customer, Contact, Order, Product, etc.), each of which corresponds to a database table, and inherits from a DbTable base class that has a common DeleteAll() method.
Now, it might be that your database table names don't match your class names. In that case you can define an attribute that adds additional information to your class, providing the table name, as shown here:
<DbTableName("CUST01")>
Public Class Customer
Inherits DbTable
...
End Class
This indicates that your "Customer" objects are stored in the "CUST01" table in the database.
You might implement the attribute like this:
Public Class DbTableNameAttribute
Inherits System.Attribute
Public Property Name As String
Public Sub New(value As String)
Name = value
End Sub
End Class
Lastly, in your base DbTable class, you would implement DeleteAll() like this:
Public MustInherit Class DbTable
Public Sub DeleteAll()
' Use reflection to retrieve the attribute.
Dim attributes = Me.GetType().GetCustomAttributes()
Dim dbTableNameAttribute = attributes.FirstOrDefault(Function(x) x.GetType() = GetType(DbTableNameAttribute)
If dbTableNameAttribute IsNot Nothing Then
Dim tableName As String = CType(dbTableNameAttribute, DbTableNameAttribute).Name
' tableName will contain the value specified in the attribute (e.g. "CUST01")
Dim sql As String = "delete from " & tableName
' ... at this point you would send the delete command to your database ...
End If
End Sub
End Class
Now, in the specific example you cite: <System.ComponentModel.DefaultEvent("DataReceived")>
What is likely happening is that the SerialDF1forSLCMicroCon class probably has multiple events, and the attribute is providing a hint to the designer that the "DataReceived" event is the default one. You'll see a similar sort of thing with a Windows Forms Button. If you click the events for a Button, there are many, but the "Click" event is always highlighted by default, as it is the most commonly used one.
I am trying to do this using VB.NET
I want to use a function in my codes like the following:
'Read all the content from the text file - Function One
'Write to SQL Table - Function Two
I am reading content of the text file form the Function one, but I also want to pass the values to function two.
I have more than 25 parameters of passing from function one to function two.
It is a simple question. Anyone can help to solve this issue.
You haven't give any sample so it's hard to help. You should definitely learn how to use classes. Group your variable together that have the same context.
Class Student
Public Property Age As Integer
Public Property Name As String
' ...
End Class
When loading your data, you put it in an object.
Dim student As New Student
student.Age = 20
student.Name = "Bob"
After that you just need to pass the class as parameter to your function
Func(student)
Sub Func(ByVal student As Student)
End Sub
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
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