Use global variables inside dlls in vb6 - dll

I have a dll where i would like to access the global variables of the Project.
How can this be done in VB6?

You can not access anything in a different project unless you explicitly pass it between the projects via COM.

While it is syntactically correct to use a global variable it is a very poor idea. Add either a module or a class file and encapsulate your variable inside a property. If you want to access your properties from within your project a bas file is fine. If you are describing making an ActiveX dll and accessing properties with another, separate project you should make a class module. You will need to set the class Instancing property to something other than private.
'myproject.bas code
Option Explicit
private mblnIsDirty as boolean
Public Property Let IsDirty(ByVal vIsDirty)
mblnIsDirty = vIsDirty
End Property
Public Property Get IsDirt() As Boolean
IsDirty = mblnIsDirty
End Property

Related

Class Module vs Module in VBA

I am new and trying to understand VBA.
What is the different between modules and class modules? How come I can only have Macros inside Modules. (I think I am wrong here, but I don't know how to add a macro from Class modules)
In the end I'm having to add all my event handlers inside class modules and have all my macro code inside modules.
Modules are global through out the code. By default recorded Macros are saved in Modules, though you can cut the code and paste the Sub/Function into a class.
Mod1.DoSomething() ' Mod1 doesn't need to be instantiated, its global through the app, there is only one instane of it and that's called a Singleton pattern.
Classes are a concept where you need to instantiate them and their scope is limited.
Public globalScope as New Class0 ' <-- class0's methods are available in and outside a Module or Class.
Private memberScope as New Class1 ' <-- class1's methods are available inside the whole Module or Class.
Dim localScope as New Class2 ' <-- class2's methods are only available inside a Sub/Function.
You'll probably want to keep your event handlers in a Module as that's global. If you dispose a class with Events those events won't be available.
ps in .Net coding "Modules" are called Static Classes
Ps2 an fyi the difference between a Subroutine and a Function is; a Function returns a value where as a Subroutine just does something.

Why can't i reference to a method/property? VB.NET

Please take a look at the 2 photos i attached, i wish to call the methods from one project to another. Both projects are in the same solution. I have already made the reference to the project containing the method in the project i want to call the method.
http://postimg.org/image/m42dlc28r/
http://postimg.org/image/w03gkz80r/
In your main project, go to
Project -> Add Reference
In that window, click browse and find the compiled version of your other project (Probably in the Release or Debug folder)
In your main projects window add this to the very top of your code (even above your class declaration)
Imports SecondProjectRootNamespace
That should give you enough information on how to do what your trying to do, but if anything was unclear, I am going to need all the details to provide a more precise answer.
Also, make sure your methods/functions are NOT declared as private.
Private = Method is only visible from within the same class
Friend = Method is only visible from any class within the same assembly (same .exe or .dll or etc)
Public = Method has no access restrictions
There are a few others but those are the basics
Try to find the dll file of the method that you want, then add it as reference in the properties of your application

re-declaring readonly property in class extensions

I am reading this document to learn objective-C: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html#//apple_ref/doc/uid/TP40011210-CH6-SW1
I the topic "Use Class Extensions to Hide Private Information" (page 73 of pdf) it says:
Class extensions are often used to extend the public interface with additional private methods or properties for use within the implementation of the class itself. It’s common, for example, to define a property as readonly in the interface, but as readwrite in a class extension declared above the implementation, in order that the internal methods of the class can change the property value directly.
What i dont understand in this statement is that since we can change the readonly property from within any private method defined in class extension without that property being re-declared as readwrite in class extension, what does it achieve by re-declaration of that property as readwrite?
You can always change the property through its instance variable (_ivar = ...), but you won't be able to change it using the dot-property syntax (self.myProp =...) unless you redeclare it as readwrite. You will also need to provide additional information like whether the property is strong or weak in this case.
Actually my assumption that "we can change the readonly property from within any private method defined in class extension" is wrong. Class extension cannot use instance variables synthesized automatically for a given readonly property, because they are private (NOT protected) by default.
Even though the document i mention says that instance variable automatically synthesized for a given property has a leading underscore (_Make) in front of it. It actually is not true ( at least not in Xcode 4.6.3 ). It bears the same name has the property itself (unless you synthesize the instance variable itself #synthesize Make = _Make;
Please correct me if i am wrong

How to make a global variable in Visual Basic

I have a Mysql login system in Visual Basic , and I want to store the username in a global variable after a succesful login but when the app will close I want that variable to be deleted.. can you show me some example? I'm a beginner at visual basic.
If you're developing on Windows, then use the Windows Registry to persist the value.
See http://msdn.microsoft.com/en-us/library/aa289494(v=vs.71).aspx for more details, and examples.
Take care if caching a password though; you'll need to encrypt that.
Just create a class (in your project) that will not be instantiated right...and then have a variable in that class with access modifier
Public Shared.
Like for me I made a class called Globals and in it was a variable called currentUser .
So to access the variable from any class I just had Globals.currentUser =txtUser.TextAnd declare it like Public Shared currentUser as String
Try this, in your form file outside of the main class, or in a separate module file:
Public Module Globals
Public UserName As String = ""
End Module
Now you can access it in any code throughout your project. It will dispose when the app is closed. If you wanted to make doubly sure, even though it would be redundant, add this to the main form that closes the whole app:
Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
UserName = ""
End Sub

Can't access my class from code-behind. Class is App_Code folder

I have a very simple class that is located within my App_Code folder in my VS2008 web application project. I am trying to instantiate an instance of this class from my code-behind file. Intellisense does not seem to be seeing my class and I am not sure why. I am using VB.NET which I am admittedly not that familiar with as compared to C#. Perhaps I am missing something. I would bet it has something to do with something I am missing in VB.NET.
Here is my simple class (for testing):
Public Class mySimpleClass
'Private member variables whose data is obtained from user input
Private mUserID as String
'Class Properties
Public Property UserID() as Integer
Get
Return mUserID
End Get
Set(ByVal Value as Integer)
mUserID = Value
End Set
End Property
'Class Methods
Public Function DisplayUserID() as String
Return this.UserID
End Function
End Class
Here is how I try an instantiate it from the codebehind ...
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim obj As New mySimpleClass()
End Sub
End Class
What I ended up doing was deleting my App_Code folder and creating a new "AppCode" folder. I also selected properties for the class file and set the Build Action property to "Compile". Once I did that and recompiled the project my class showed up.
you should change Return this.UserID to Return Me.UserID (VB.Net ;-))
rebuild the solution and see if it works
I'm not as familiar with the app_code folder and Websites in general, i'm always using WebApplications. I would suggest to convert it to a WebApplication too, here are further informations why: ASP.NET Web Site or ASP.NET Web Application?
Actually, I think if you add namespace to your project, it should work as well. I seem to remember having that problem every so often in C# asp.net as well. I could be way wrong though
Agree with Gustyn (sort of).
Add a "using namespace;" line to the web form code behind
Dave
Going to each file holding the "public" class(es) in the "App_Code" folder and setting Build Action from Content to Compile will do the trick.
It works for Web Application type projects.
All the stuff on whether to use a module(VB) or namespace or static(C#) won't help until you set your class files to compile (no matter what folder they are in).