Results 1 to 14 of 14

Thread: Module = Class?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Module = Class?

    Hi. I have before used "module" in Visual Basic, and now when I'm starting out in C#, I can't find it. So does the "class" do the job of the module?.
    And how do I use it.

    I have tried to store a value in my class:
    Code:
    functions_class functions = new functions_class();
    functions.Nano = textBox1.Text;
    The string is called Nano...

    Now when I try to pop it up:
    Code:
    public void printData()
            {
                MessageBox.Show("Nano: " + Nano);  
            }
    I just get this message:
    Nano:
    Something I do wrong here?
    PHP Code:
    $im_addicted_to_programming true

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Module = Class?

    There is no concept of Module in C#. You would need to use STATIC (i.e. SHARED in VB) class members.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Module = Class?

    Thread moved from 'C /C++' forum to 'C#' forum

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Module = Class?

    In VB a module is a type that you cannot create an instance of, so you access all members via the type itself. The C# equivalent of that is a static class. A static class is one where the class itself is declared static, rather than just its members being declared static. If a class is not declared static then its members can be declared static or not. If the class is declared static then all of its members MUST be declared static. A static class with a non-static member will not compile. In VB you cannot declare a class Shared. If you want all Shared members then you declare a module.

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Module = Class?

    In other words, with your above code, you would need to declare your class as
    Code:
    public static class functions_class
    {
        public static string Nano {get; set;}
        //...
    }
    and use it like
    Code:
    functions_class.Nano = textBox1.Text;
    
    MessageBox.Show("Nano: " + functions_class.Nano);
    As far as I know, you cannot omit the "functions_class" name in front of "Nano" as you could in VB using a module, but the concept is the same.


    By the way, I advice you to adhere to the .NET coding style: keep class names in CamelCase. That means: the name starts with a capital, and any consecutive words start with a capital as well. Don't use underscores, and preferably don't use the name "Class" in your name as that doesn't mean anything. With that, the name of your class should be "Functions".

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Re: Module = Class?

    Thankx, this is just what I'm looking for.
    Now one last question:
    If I have a webbrowser in form2 and I wan't to set its navigation location in form1, how do I do it?
    PHP Code:
    $im_addicted_to_programming true

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Module = Class?

    You expose either the property or method you need via a public member.

    By 'set its navigation', you can either mean set the URL, or navigate to some URL. In the first case, you could do this in Form2
    Code:
    public string WebbrowserUrl
    { 
        get { return webBrowser1.Url; }
        set { webBrowser1.Url = value; }
    }
    and this in Form1
    Code:
    Form2 f = new Form2();
    f.Show();
    
    f.WebbrowserUrl = "www.google.com";

    In the second case, instead of creating a property you create a method. In Form2:
    Code:
    public void Navigate(string url)
    {
        webBrowser1.Navigate(url);
    }
    and in Form1:
    Code:
    Form2 f = new Form2();
    f.Show();
    
    f.Navigate("www.google.com");
    In both cases, the property/method simply acts as a 'pass-through' to the private (inaccessible) web browser. The Url property simply returns the Url property of the web browser in its getter, and sets the value of the Url property of the web browser in the setter. The Navigate method simply calls the Navigate method on the web browser.

    You cannot access the webbrowser directly from Form1, because it is declared private by default. You could declare it public instead, then you could use it directly, but I advice you to leave it private. If you only need to ever set the Url property, then only expose the Url property instead of the entire web browser object. Only if you need nearly every single property of the web browser could you consider exposing the entire webbrowser (otherwise you'd have dozens of these 'pass-through' properties).

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Re: Module = Class?

    Code:
    public void Navigate(string url)
    {
        webBrowser1.Navigate(url);
    }
    This would be the best for me, but if I wan't to put this in a class, how can I tell it that the webbrowser is in form 2?
    PHP Code:
    $im_addicted_to_programming true

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Module = Class?

    I see no reason to put that in a class other then Form2 (Form2 is also a class!). If you do think you have a good reason, you can pass the WebBrowser along for the ride:
    Code:
    public void Navigate(WebBrowser wb, string url)
    {
       wb.Navigate(url);
    }

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Re: Module = Class?

    Okey. And why does this code don't work:
    Code:
    public void button1_Click_1(object sender, EventArgs e)
            {
                Login Login = new Login();
                Login.Close(); 
            }
    I have a form named "Login", so shouldn't this code close the form?
    PHP Code:
    $im_addicted_to_programming true

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Module = Class?

    Quote Originally Posted by worqy View Post
    Okey. And why does this code don't work:
    Code:
    public void button1_Click_1(object sender, EventArgs e)
            {
                Login Login = new Login();
                Login.Close(); 
            }
    I have a form named "Login", so shouldn't this code close the form?
    If you already have a car, and you go and buy a new car and then sell it, does that mean you sold the original car? The same goes here. If you have a Login form already, creating a new Login form and closing that isn't going to have any affect on the existing Login form. If you want to close the existing Login form then you have to close the existing Login form, not a new Login form.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Re: Module = Class?

    I see... The question is, how?
    Because I can't use the close() if I don't create a 'new form'.
    And I looked on the options have if I only use Login.*** I and couldn't find any of those that may have worked...
    PHP Code:
    $im_addicted_to_programming true

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Module = Class?

    For future reference, please keep each thread to a single topic. We're now onto our third unrelated subject in this one thread. Please make it the last.

    You can't conjure a reference to an object out of thin air. If you want to be able to use an object later, you have to keep a reference to it when you create it. That means assigning your original form to a variable or property when you create it so that you can access the same object later.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2009
    Posts
    176

    Re: Module = Class?

    Ok, thank you.
    PHP Code:
    $im_addicted_to_programming true

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