-
May 31st, 2010, 01:32 PM
#1
Thread Starter
Addicted Member
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:
Something I do wrong here?
PHP Code:
$im_addicted_to_programming = true;
-
May 31st, 2010, 03:08 PM
#2
Re: Module = Class?
There is no concept of Module in C#. You would need to use STATIC (i.e. SHARED in VB) class members.
-
May 31st, 2010, 05:38 PM
#3
Re: Module = Class?
Thread moved from 'C /C++' forum to 'C#' forum
-
May 31st, 2010, 06:34 PM
#4
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.
-
Jun 1st, 2010, 03:34 AM
#5
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".
-
Jun 1st, 2010, 12:06 PM
#6
Thread Starter
Addicted Member
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;
-
Jun 1st, 2010, 12:28 PM
#7
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).
-
Jun 1st, 2010, 02:25 PM
#8
Thread Starter
Addicted Member
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;
-
Jun 1st, 2010, 02:56 PM
#9
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);
}
-
Jun 1st, 2010, 11:44 PM
#10
Thread Starter
Addicted Member
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;
-
Jun 2nd, 2010, 12:00 AM
#11
Re: Module = Class?
Originally Posted by worqy
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.
-
Jun 2nd, 2010, 05:32 AM
#12
Thread Starter
Addicted Member
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;
-
Jun 2nd, 2010, 06:11 AM
#13
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.
-
Jun 2nd, 2010, 12:52 PM
#14
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|