Results 1 to 18 of 18

Thread: Public Class Issue ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Question Public Class Issue ?

    So... Why Wouldnt This Work ?

    Code:
    Module Module1
    Public Class One
    Sub Main()
    Console.Writeline("Hello")
    Console.Readline()
    End Sub
    End Class
    End Module
    Its Says , That The Sub Has To Be After The Module , But Then My Public Class Wont Work , Any Ideas

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

    Re: Public Class Issue ?

    Module1 no longer has a Sub called Main() as an entry point to the program is the problem. What are you trying to do?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: Public Class Issue ?

    Just Do A Concept Like What I Showed Above , What Would I Do To Fix this , If I Put Sub Main() Above Public Class , The Public Class Wouldnt Work

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

    Re: Public Class Issue ?

    The concept in your OP does not, as you've discovered, work. I'm not sure what it is about the code that you're trying to achieve. In what way does the class "not work"?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: Public Class Issue ?

    Okay So If I Did This

    Code:
    Module Module1
    Sub Main()
    Public Class One
    Console.Writeline("Hello")
    Console.Readline()
    End Class
    End Sub
    End Module
    In The Public Class One

    Only "Public" Would Light Up And "Class" And "One" Would Just be Normal Black Color. In Th Error Box , It Says " Keyword Is Not Valid As An Identifyer

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: Public Class Issue ?

    Okay So If I Did This

    Code:
    Module Module1
    Sub Main()
    Public Class One
    Console.Writeline("Hello")
    Console.Readline()
    End Class
    End Sub
    End Module
    In The Public Class One

    Only "Public" Would Light Up And "Class" And "One" Would Just be Normal Black Color. In Th Error Box , It Says " Keyword Is Not Valid As An Identifyer

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

    Re: Public Class Issue ?

    What's happening there is that you're now defining a Class inside a Sub, which is not allowed. Perhaps you mean this:

    vbnet Code:
    1. Module Module1
    2.     Sub Main()
    3.         Console.Writeline("Hello")
    4.         Console.Readline()
    5.     End Sub
    6.     Public Class One
    7.     End Class
    8. End Module

    Or even better, why not define the Class separately in its own code file and not have it nested in the Module? However, I'm still not seeing what the purpose of the class is... perhaps you mean this:


    vbnet Code:
    1. Module Module1
    2.     Sub Main()
    3.         Dim one As New One()
    4.         one.DoSomething()
    5.     End Sub
    6. End Module
    7.  
    8. Public Class One
    9.     Sub DoSomething()
    10.         Console.Writeline("Hello")
    11.         Console.Readline()
    12.     End Sub
    13. End Class

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: Public Class Issue ?

    So I Dont Even Need A Module At all ?

  9. #9
    Fanatic Member Toph's Avatar
    Join Date
    Oct 2014
    Posts
    655

    Re: Public Class Issue ?

    Yes you do need a module. The class should be defined outside the module. To put less confusion, create a new class file and do your stuff there. And just instantiate from your Sub Main() Module code.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Nov 2014
    Posts
    73

    Re: Public Class Issue ?

    oh ok , i see what i did now

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Public Class Issue ?

    You should be aware that a Module IS a class. It is just a special type of class with all the members Shared. There is nothing wrong with declaring one class within a second class. If you had made that a Private Class, then only code in the module would be able to create one and use it. Since you made it a Public Class, there is no good reason for it to be inside the Module at all. In fact, it is a bit strange. All the members of a Module are quietly turned into Shared members, but there is no such thing as a Shared Class, so the compiler must just treat the class as any other class declared within a different class. There's still no good reason for it, though.

    EDIT: I should add that I may be wrong about that. A public class defined within a module is something I have never considered, so I'm kind of guessing at how it would behave. Somebody may correct me about some part of that, but it just highlights the point: There isn't a good reason, because it is more likely to confuse than benefit.
    My usual boring signature: Nothing

  12. #12
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Public Class Issue ?

    I should add that I may be wrong about that
    I'm pretty sure you're not though. A public class is a public class, no matter where it's defined.

    I do occasionallydefine a public class inside another class but it's rare. An example I've got right in front of me is I've got some SQL Flavour classes that allow me to talk to different databases and abstract away the variance syntax nuances. Each flavour class implements the same "SQLFlavour" interface. Each flavour class has a variety of private helper classes that it needs to do it's job and, unsurprisingly, they all look pretty much the same. They also each expose their own version of a single public "DataWriter" class (ie a class which implements my IDataWriter interface). Somehow, because the implementation of that class is intrinsically tied to the implementation of the SQLFlavour class and because I've also got a bunch of private helper classes already wrapped up inside the SQL Flavour class it just kinda feels right to put the Data Writer class in there alongside the proivates, even though it's public. Notably, though, if I implement the Data Writer class outside of the Flavour class the behaviour doesn't change at all.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Public Class Issue ?

    Quote Originally Posted by FunkyDexter View Post
    I'm pretty sure you're not though. A public class is a public class, no matter where it's defined.
    Even inside a module? Although, I rarely use modules so I'm not sure I would have ever thought to put a public class (or ANY class inside of it).


    Quote Originally Posted by FunkyDexter View Post
    I do occasionallydefine a public class inside another class but it's rare. An example I've got right in front of me is I've got some SQL Flavour classes that allow me to talk to different databases and abstract away the variance syntax nuances. Each flavour class implements the same "SQLFlavour" interface. Each flavour class has a variety of private helper classes that it needs to do it's job and, unsurprisingly, they all look pretty much the same. They also each expose their own version of a single public "DataWriter" class (ie a class which implements my IDataWriter interface). Somehow, because the implementation of that class is intrinsically tied to the implementation of the SQLFlavour class and because I've also got a bunch of private helper classes already wrapped up inside the SQL Flavour class it just kinda feels right to put the Data Writer class in there alongside the proivates, even though it's public. Notably, though, if I implement the Data Writer class outside of the Flavour class the behaviour doesn't change at all.
    There are times when it does make sense. More often than not, when I'm using a class in a class, it's a supportive class and is usually private. The only exception is when I'm defining a custom EventArg class... for obvious reasons that needs to be public. I will usually put it inside the generating class just for organizational reasons, not functional.

    -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??? *

  14. #14
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,900

    Re: Public Class Issue ?

    Even inside a module?
    Oops, that's just me being slack - I wasn't considering modules. I have no idea if that would affect the behaviour or would even compile as I've never tried it but I imagine it would have no effect. If you take the view that a module is nothing but a static class then it shoudn't make any difference at all. I wouldn't take my word for it though and I'd try it out before making any assumptions.

    for organizational reasons, not functional
    Yep, same here. In some rare cases it makes sense from a functional perspective but I don't believe it makes any functional difference.
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Public Class Issue ?

    See why I added the wiggle room in that statement? A class in a class is familiar. A class in a module....I've never even tried it, or considered it. I think it would just be a class in a class, but I don't know for certain.
    My usual boring signature: Nothing

  16. #16
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Public Class Issue ?

    Ok... it so happened I had a console app lying about - which uses a module and Sub Main for its entry point. It also happened to implement a class, Room. I'd declared the class outside the module.
    The class was simple, included a Name and a Property called AdjacentRooms which was a List(Of Room) ... the main sub called an init method that loaded the main list, crteated rooms, linked them, etc (this was for a post about traversing nodes in a map). It then set a starting point, a destination, then walked the nodes, looking for paths, printing out results as it went along.

    So... I took the class, moved it into the module, ran it, and the console window popped up and the code ran as expected. But it was also all self-contained... I'm not sure how the access to the class would have been affected if it had been in a different module, or if it was in a module in a different project and referenced.

    -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??? *

  17. #17
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Public Class Issue ?

    After some tinkering...
    1) if the class remained in the same project I could access it just fine no matter what module it was in,.
    2) If I then move the class to a new Class Library project, stuff it into a module there... I no longer see the class. The only class I see in the intellisense is the main default class( Class1) that it adds as default. I do not see the Module or the class inside of it.
    3) AH! Once I change the Module to Public module! That was the key... NOW I see the Module AND the contained class in my intellisense.

    -tg


    edit - also just discovered I can Imports the module... Imports, not just for namespaces.
    * 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??? *

  18. #18
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Public Class Issue ?

    You can have a class within a class, of course. A module is just a static class (I can't recall if it's public or friend). You can use Reflector to see this; there are some minor differences, but it is a class, just the same.

    You can even put your Sub Main in a class, as long as you declare it as shared.

    In other words, a module is a convenient mechanism for 'sharing' application wide methods and variables, even if it is an anachronism from Classic VB.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

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