|
-
Dec 6th, 2002, 12:24 AM
#1
Thread Starter
Sleep mode
instanciating classes ??
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
-
Dec 6th, 2002, 01:15 AM
#2
What? Can you explain that a little better, sorry.
-
Dec 6th, 2002, 03:15 AM
#3
Registered User
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.
-
Dec 6th, 2002, 03:20 AM
#4
Thread Starter
Sleep mode
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
-
Dec 6th, 2002, 03:21 AM
#5
Thread Starter
Sleep mode
well Athley , I was think of doing that but does this work with variables inside the class ??
-
Dec 6th, 2002, 03:41 AM
#6
Registered User
Yes, you can use shared fields (static variables) in a class.
-
Dec 6th, 2002, 05:55 AM
#7
Thread Starter
Sleep mode
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 ????
I dunno If you're still messing that up!!
-
Dec 6th, 2002, 07:44 PM
#8
Thread Starter
Sleep mode
is it still vague ???
-
Dec 6th, 2002, 10:22 PM
#9
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
-
Dec 6th, 2002, 10:33 PM
#10
Thread Starter
Sleep mode
man , I've tried this before but gives none changes.
well , another unresolved problem .acually not a problem rather than query.
thanx Edneeis .
-
Dec 6th, 2002, 11:08 PM
#11
Frenzied Member
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.
Dont gain the world and lose your soul
-
Dec 7th, 2002, 01:16 AM
#12
Member
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.
-
Dec 7th, 2002, 06:17 AM
#13
Registered User
My feeling is absolutely mutual. It just has to work.
-
Dec 7th, 2002, 10:49 AM
#14
Lively Member
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.
-
Dec 7th, 2002, 11:34 AM
#15
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
-
Dec 7th, 2002, 11:40 AM
#16
Thread Starter
Sleep mode
oh I was wrong guys . I dunno what's happened to me.
Thanx a lot for your posts guys.Indeed Edneeis's code worked.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|