Module

if the function isnt static:

Dim myClass = new ClassName()
myClass.DoSomething()

or

Dim myClass = new ClassName()
Dim o As Object
o = myClass.ReturnSomething()

if the function is a static function you can just do you do not need the Dim myClass = new ClassName() portion just put:

ClassName.DoSomethingStatic()

or

Dim o As Object
o = ClassName.ReturnSomethingStatic()

Hope that is what you were looking for.
 
Code:
Public class Yoyo

Private Sub MySubProggy(ByVal hello As Byte)

Dim Class1 As New HELLO  (HELLO is in module)
...
...

   Max = Class1.Maximum(A, B, C)
   Min = Class1.Minimum(A, B, C)

After using the fuctions:
Do I set something to Nothing or do I dispose something ?
...
...


End sub 
End Class[/Vb]


[code=vb]Module mdlCam

    Public Class HELLO

        Public Function Maximum(ByVal a As Single, ByVal b As Single, ByVal c As Single) As Single
         ...
        End Function
        
       Public Function Minimum(ByVal a as single, ByVal b as single,ByVal c as single) As Single
           ...
        End function

    End Class

End Module[/VB]

like this ?  so do I have to set something to Nothing after using 
the functions ? 


and have I done something wrong  like is the code too public or something ?
 
Last edited by a moderator:
If you want the consumer of the class to be able to access
members (properties, methods, events, etc.), then declare them
puclicly as you have done. Otherwise declare them Private or
Protected.

You do not need to destroy of classes when you are done with
them to free memory, as was necessary in VB6. Objects that
implement the IDisposable interface (mostly GDI+ classes),
however, should be disposed of when they are done being used
to free their memory. You can identify these classes because
they have a Dispose() method.

Also try to avoid using Modules and organize your classes into
namespaces. To create "global" methods, declare members of a
Public class as Public Shared.
 
Thanks that helped a lot... also do you mean I should use more classes in same form instead of modules or what do you mean by namespaces ?
 
Instead of organizing your code into modules, organize it into
classes instead. You can create methods that do not require
an instance of the class to be run, just declare them as Shared.

If you would like to group several classes together for aesthetic
quality and overall organization, then place all the classes in the
same namespace (just declare Namespace (name) above the
classed and an End Namespace after them).
 
One thing more, is there any tutorial somewhere about sharing classes and functions...

or could you show me a little example of sharing a class ?
how about if i want to change values of class1 public variables in class2 ?
 
I think now I inherited first class to second class...and now im able to control first class varibles too(if they are public) ...cool..
 
Lets take your previous code as an example. Because the
two methods do not require access to instance-specific
members such as private variables in the class, you can declare
them as Shared (static in C#). That way you can call the method
without having to Dim a new instance of the class.

So, heres the modified class:
Code:
    Public Class HELLO

        Public Shared Function Maximum(ByVal a As Single, ByVal b As Single, ByVal c As Single) As Single
         ...
        End Function
        
       Public Shared Function Minimum(ByVal a As Single, ByVal b As Single,ByVal c As Single) As Single
           ...
        End Function

    End Class

And heres the code that calls it:
Code:
Public Class Yoyo

Private Sub MySubProggy(ByVal hello As Byte)
   No need for Class1 here
  Max = HELLO.Maximum(A, B, C)
  Min = HELLO.Minimum(A, B, C)
End Sub 

End Class

The same can be done for variables.
 
OK now I got a problem (not a first time tough lol)

I got two classes... class1 and class2...I got one sub program and two functions in class2. The sub program should be able to use the two functions in the same class. Also the sub program should be able to return/change values in class1 public variables. How do I make all this work lol ... im inheriting class1 in class2 because I noticed that way i could use class1 public variables.... if i make the sub program shared I cant use the functions in class2...but if i make the sub program non shared = public...I cant use class2 sub program in class1.... I hope someone understand this mess lol

[VB]
Namespace Experimental

Public class1

Public a as integer

Me.text = HiImclass2subfunction(32,32,32)

end class


Public class2

public sub HiImclass2subfunction(......) as string

a = a + 1 I want to change class1 variable
functionYO(23,23,23)

end sub

private function functionYO(a,b,c) as string
.....
end function

end class

end namespace[/code]
 
Last edited by a moderator:
If you declare all the methods of Class1 and Class2 as "Public Shared",
then you should have no problem accessing the methods between
the classes and between your sub. You do not need to inherit
Class1 just to access its variables; if they are Public Shared then
Class2 will be able to see them.

Both classes should also be in the same namespace. If you dont
define a namespace, then your project itself is a namespace and
it will group the classes.
 
hey I made some variables share in class1 ....but i still cant access them from class2 public sub proggy why ?

"sorry i edited this same time"
 
Last edited by a moderator:
You forgot the keyword "Class" when defining the classes
Experimental and RGB_HSL_RGB. You also dont declare the
class itself as Shared, you only declare the methods and
variables within it as Shared. It also helps to avoid confusion by
giving namespaces and classes different names; dont call them
both Experimental.
 
[VB]Namespace Experimental

Public class class1

Shared a As Integer

Me.text = HiImclass2subfunction(32,32,32)

End Class


Public class class2

Shared Sub HiImclass2subfunction(......) As String

a = a + 1 I want to change class1 variable
functionYO(23,23,23)

End Sub

Private Function functionYO(a,b,c) As String
.....
End Function

End Class

End Namespace[/VB]


If i make public sub program as shared sub program(as shown up there) somehow I cant use functionYO anymore, why ? also even a is shared at class1 I cant access it from shared sub program in class2.


When I point to functionYO it says: "cannot refer to an instance member or a class from within a shared method or shared member initializer without an explicit instance of the class" lol

when I point variable a in class2 it says "name a is not declared"

when I point class2 sub program in class1 it says "name HiImclass2subfunction is not declared"
 
Last edited by a moderator:
Ok I made the class2 sub function Private shared now im able to access it in class2..... only problem now is to access class1 variables... even i make them shared I cant manipulate em from class2 private shared fucntion.
 
[VB]Namespace Experimental

Public Class class1

Public Shared a As Integer class2 cant see/change me, why ?

Me.text = Class2.HiImclass2subfunction(32,32,32) this works

End Class


Public Class class2

Shared Sub HiImclass2subfunction(......) As String

a = a + 1 this doesnt work
functionYO(23,23,23) now this works

End Sub

Private Shared Function functionYO(a,b,c) As String
.....
End Function

End Class

End Namespace[/code]

I got some elemets working great... only thing bugs me now is that damn class1 variable, any help ?
 
Okay, Ive figured out the problem, and Ive been telling you
some misinformation. The Shared keyword works differently when
used on variables than it does when its used on methods.

When you have a Shared Sub or Function, that method can be
seen and used without initializing the class. Thats what I said.

When you have a Shared variable, however, that means the value
of that variable is shared between multiple instances of that
class.

Id reccomend you read [mshelp=ms-help://MS.MSDNQTR.2003FEB.1033/vbcn7/html/vaconSharedMembers.htm]this article[/mshelp] before I confused you further.
Sorry for the misinformation, and I hope the article clears things up.
 
After making class1 variable a Public shared AND inheriting class1 it started to work !!!!! but now im very suspicious.... is the code like a cheese with holes ? ... is there something I should change to make is more simple and not so dangerous ?
 
Back
Top