Is there any way around to get ride of instanciating classes and class members?just calling the function or the variable from the class not from module
thanx in advance
Printable View
Is there any way around to get ride of instanciating classes and class members?just calling the function or the variable from the class not from module
thanx in advance
What? Can you explain that a little better, sorry.
To be able to use a function in a class without instntiating you have to declare the function as Shared, if that was what you meant.
umm , It's as simple as this :
instead of doing this :
Dim myobject as new ClassName
Dim myfunction as new ClassName.CountFunction
Dim myVariable as new ClassName.HoldVariable
I want to use my class directly without declaring new instance of that class or function or the variable inside the class like this
Myfunction(......)
MyVariable=...........
hope you got it by now ;)
well Athley , I was think of doing that but does this work with variables inside the class ??
Yes, you can use shared fields (static variables) in a class.
you seem guys not getting my point
look , in my class I have this Class
VB Code:
Public Class Resizing Public Width As Integer Public Height As Integer Public frm As New Form() Public Sub New(ByVal width As Integer, ByVal height As Integer) Me.Width = width Me.Height = height End Sub End Class
If I want to use that then I have to do the following :
make new instance of that Class
Dim Myresizing as new Class1. Resizing
to access the subs and functions and data members of that class .
let's say I have tens of Classes , then I have to do that with every Class .I want my Function behaviour as if it is stored in a module ????
:rolleyes: I dunno If you're still messing that up!!
is it still vague ???:( :(
Well as he said you should use SHARED variables and methods but in your example you didn't.
Try:
VB Code:
Public Class Resizing Public Shared Width As Integer Public Shared Height As Integer Public Shared frm As New Form() 'No sense having a constructor on a shared class 'It'll never get used 'Public Sub New(ByVal width As Integer, ByVal height As Integer) 'Me.Width = width 'Me.Height = height 'End Sub End Class
man , I've tried this before but gives none changes.
well , another unresolved problem .acually not a problem:D rather than query.
thanx Edneeis .
What do you mean no change? If you do it the way Edneeis showed you it should work. Instead of instantiating the class, just do this
Resizing.Width = 2 etc.
Do the same thing for methods also.
Edneeis' code works fine. If something is not working correctly for you then please paste the code you are using and tell us EXACTLY what the problem is.
My feeling is absolutely mutual. It just has to work.
Yeah, Edneeis's code absolutely should work but I don't think it's working the way pirate wants it to do.
Just to show that Edneeis's code works, why don't you try something more to the point like.
Public Class Calculator
Public Shared Function Add(ByVal Num1 as Integer, Num2 as Integer) as Integer
Return Num1 + Num2
End Function
End Class
then your main form try calling the calculator class like so
Debug.WriteLine (Calculator.Add(50,11))
But like in your case, i think none of us have any ideas as to what you are trying to do with the Resizing class. BTW, Resizing is a terrible name for a class.
Classes should be names like Form, Textbox, Table, Roof, House, etc etc. Or if in your case, you just want to group functions/subs together , I would name them like GeneralRoutines, FinancialRoutines, etc. etc.
Resizing is a good name for a function or subroutine but not for a class.
I still do not think this is clear, but maybe you are trying to use inheritence.
Something like this:
VB Code:
Public MustInherit Class Form3 Inherits System.Windows.Forms.Form Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Public Overridable Sub MyNew(ByVal lngHeight As Long, ByVal lngwidth As Long) Me.Height = lngHeight Me.Width = lngwidth End Sub End Class
VB Code:
Public Class Form4 Inherits Form3 Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class
And then to call it:
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim j As New Form4() j.MyNew(77, 888) j.Visible = True End Sub
oh I was wrong guys . I dunno what's happened to me.
Thanx a lot for your posts guys.Indeed Edneeis's code worked.