Results 1 to 16 of 16

Thread: instanciating classes ??

  1. #1

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    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

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What? Can you explain that a little better, sorry.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    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.

  4. #4

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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

  5. #5

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    well Athley , I was think of doing that but does this work with variables inside the class ??

  6. #6
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    Yes, you can use shared fields (static variables) in a class.

  7. #7

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    you seem guys not getting my point
    look , in my class I have this Class
    VB Code:
    1. Public Class Resizing
    2.         Public Width As Integer
    3.         Public Height As Integer
    4.         Public frm As New Form()
    5.  
    6.         Public Sub New(ByVal width As Integer, ByVal height As Integer)
    7.             Me.Width = width
    8.             Me.Height = height
    9.         End Sub
    10.        
    11.     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!!

  8. #8

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    is it still vague ???

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Well as he said you should use SHARED variables and methods but in your example you didn't.

    Try:
    VB Code:
    1. Public Class Resizing
    2.         Public Shared Width As Integer
    3.         Public Shared Height As Integer
    4.         Public Shared frm As New Form()
    5.  
    6.         'No sense having a constructor on a shared class
    7.         'It'll never get used
    8.         'Public Sub New(ByVal width As Integer, ByVal height As Integer)
    9.             'Me.Width = width
    10.             'Me.Height = height
    11.         'End Sub
    12.        
    13. End Class

  10. #10

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    man , I've tried this before but gives none changes.
    well , another unresolved problem .acually not a problem rather than query.
    thanx Edneeis .

  11. #11
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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

  12. #12
    Member
    Join Date
    Sep 2002
    Location
    California
    Posts
    52
    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.

  13. #13
    Registered User
    Join Date
    Nov 2002
    Location
    Växjö, Sweden
    Posts
    314
    My feeling is absolutely mutual. It just has to work.

  14. #14
    Lively Member
    Join Date
    Jan 2002
    Location
    Malaysia
    Posts
    64
    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.

  15. #15
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    I still do not think this is clear, but maybe you are trying to use inheritence.

    Something like this:

    VB Code:
    1. Public MustInherit Class Form3
    2.     Inherits System.Windows.Forms.Form
    3.  
    4.  
    5.     Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.     End Sub
    8.  
    9.     Public Overridable Sub MyNew(ByVal lngHeight As Long, ByVal lngwidth As Long)
    10.         Me.Height = lngHeight
    11.         Me.Width = lngwidth
    12.  
    13.     End Sub
    14. End Class

    VB Code:
    1. Public Class Form4
    2.     Inherits Form3
    3.  
    4.  
    5.  
    6.     Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.  
    8.     End Sub
    9.  
    10. End Class

    And then to call it:

    VB Code:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim j As New Form4()
    3.         j.MyNew(77, 888)
    4.         j.Visible = True
    5.  
    6.     End Sub

  16. #16

    Thread Starter
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    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
  •  



Click Here to Expand Forum to Full Width