Results 1 to 12 of 12

Thread: Classes advice

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Classes advice

    Hey guys

    I'm just wondering, how do i make a class link into my main code, it is in the same project so no need to reference it

    ive set up the the class and typed

    dim X as new XY

    X(being name of variable, XY being the name of the new class)

    but they aren't linking/ working

    many thanks

    R.vb
    Last edited by richardMorris.vb; Feb 7th, 2012 at 12:34 PM.
    A programmer in need, is a programmer in deed

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: Classes advice

    What is not working? Dim is used at a local level, if you want it available to the whole form/module, you have to declare it at the top Private x as New Xy.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Classes advice

    is the class in the same project? Or did you put it into another project? If it's in another project, then you need to reference it first. If it's in the same project, then you may need to provide some actual code, not some theoretical code...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Re: Classes advice

    tg thanks for the help.....

    grimfort thank you very much that's just what i needed so to call a class instead of dim i use private in its place?

    thank you
    R.Vb
    A programmer in need, is a programmer in deed

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Classes advice

    No, not quite. Dim is the same as Private in almost every case. Normally, variables that are declared outside of any method are declared with Private, Protected, Friend or Public. You can also declare them with Dim, which acts the same as declaring them as Private. Dim is most commonly used for declaring variables that are declared inside of methods.

    From your initial post, it wasn't clear where you were declaring that variable X. The use of Dim suggests that you were declaring it inside of a method, in which case it will not be visible to any code outside of the method. The syntax of the variable declaration was fine, and would work. Where that variable is visible is determined by the scoping rules, and that is probably where your problem lies. That's what TG was getting at. The variable is there, it is properly declared and properly instantiated, but that doesn't mean it is visible just anywhere. If it is declared that way outside of any method (at class scope), then it will only be visible to other methods inside the same class. If it is declared that way inside a method, then it will only be visible inside that method.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Re: Classes advice

    hey thanks for the insight it was very helpful

    i've put the class it self as Private,

    it was to make a savefiledialog box, i have it working in the main project,
    but want to put it in a class to make it look neater and easier on the eyes

    many thanks

    R.Vb
    A programmer in need, is a programmer in deed

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Classes advice

    It is rare to have a class be Private, though it is entirely possible. Normally that is done when the class is defined inside another class, something like this:
    Code:
    Public Class Outer
     'Stuff for the outer class
    
     Private Class Inner
      'Stuff for the inner class
     End Class
    End Class
    In such a case, the Inner class can only ever be created or used within Outer. This greatly limits the versatility and usefulness of Inner, which is why it is rarely done.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Re: Classes advice

    so should i leave the class as public?
    then after the class call upon it via the private function?

    Thank you for the help

    R.Vb
    A programmer in need, is a programmer in deed

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: Classes advice

    I don't think there is an answer for that. You can't call a private function of a class from outside the class, regardless of whether the class itself is public or private. If you have a private function outside the class, it doesn't matter whether the class itself is public or private. So, I'd say that at this point we are just talking in generalities, but that's about it, and having said that, I'm going to disappear for a day or three.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Re: Classes advice

    thanks for the help,
    have fun hiking

    R.Vb
    A programmer in need, is a programmer in deed

  11. #11
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Classes advice

    Quote Originally Posted by Shaggy Hiker View Post
    In such a case, the Inner class can only ever be created or used within Outer. This greatly limits the versatility and usefulness of Inner, which is why it is rarely done.
    But is precisely the reason that it is done when it is.

    I had a need for a private inner class just the other day, in fact. I needed to allow non-UI elements to take part in our UI contextualising system (disabling certain controls when the function they control is not available due to application state). I wrote a contextualisation controller that worked against a new Interface I devised rather than against UI controls.

    The implementation of that interface would be specific to each of the non-UI classes that I wanted to interact with. Furthermore, the implementation would need to access internal state of the class instances that I did not want to expose. Implementing the interface directly on the class was not desirable because the class did not need that extra responsibility, so I defined a private nested class that implemented the interface, had each instance of the outer class create an instance passing in a reference to itself so the inner class could control it, then had the outer class expose a read-only property typed as the interface that returned the instance of the private class. Wow, it sounds convoluted when I write it like that, but in code it was an elegant solution to the problem.

  12. #12

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Location
    Newcastle
    Posts
    13

    Re: Classes advice

    hey evil giraffe.... i have no idea what you just said!!! haha
    but thanks for commenting
    A programmer in need, is a programmer in deed

Tags for this Thread

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