EFileTahi-A Posted August 8, 2004 Posted August 8, 2004 So, here is the deal, i want to call a variable that is inside a class, that is outside my form... How can i do it? (the class is not in a form but in a class file) In vb6 was truly simple like "form / moduleName.VarName... Tks for ur help! Quote
Denaes Posted August 8, 2004 Posted August 8, 2004 So, here is the deal, i want to call a variable that is inside a class, that is outside my form... How can i do it? (the class is not in a form but in a class file) In vb6 was truly simple like "form / moduleName.VarName... Tks for ur help! myClass ClassInstance = new myClass(); //where myClass is the name of your class - class and filename should be the same. myClass in myClass.cs //Inside of a procedure MessageBox.Show(ClassInstance.MemberProperty); This works for getting default settings from a class. If you did something more complex like set the settings on Form3 and wanted to read them on Form2, you need to declair the variables as static (I believe - Shared in VB.Net) and the variable will be available throughout every instance of a class. This is good for things like connectionstrings. Another alternate method is to use serialization to save your values, so when you close your Form2 and release the instance of the class, it saves it's variables to the disk. when another instance is opened, it loads it's variables back to the form. little tricks I had to use to get a LOT of global variables available globally without making them public in a module. Quote
EFileTahi-A Posted August 8, 2004 Author Posted August 8, 2004 t_Construction ClassInstance = new t_Construction(); this is what I have... it just don't work... wound not suppose my class appear in the list that pop ups as soon I Write "= new " ? Well i can use the class inside a my form to solve this but... that means I have to create everything inside forms only... tks very much for posting, hope i can hear more from u... cheers! Quote
EFileTahi-A Posted August 8, 2004 Author Posted August 8, 2004 Well ignore the last one... my class was listed... i though i would encouter my class file name in the list... but it appears the class name itself... now trying to change the var to constant to see if it appear in my class.varname... tks once more... oh one more thing... never used constant vars... what the advantage? Quote
Denaes Posted August 8, 2004 Posted August 8, 2004 Well ignore the last one... my class was listed... i though i would encouter my class file name in the list... but it appears the class name itself... now trying to change the var to constant to see if it appear in my class.varname... As a rule of thumb, each public class should have it's own file which matches the name of the class. This helps you tell what you're looking at without opening up the file. It's okay to have 3 (or however many) private classes or structs in the file to help it out, but if it's public, you want to keep it simple. oh one more thing... never used constant vars... what the advantage? Say you're making SuperGeometryClass. Anyone knows Geometry has Pi. To what precision do you care? You're going to use it in like 80 functions. So you make a constant variable. Every time it's used, your class knows Pi will be 3.14. It doesn't need to check. It's a little faster. Now when you decide you need more precision, say to 8 decimal places, you just have to change it in that one place, rather than in 80 functions. A constant (private const string = "hi";) is something that you're not going to need to change on the fly, but you may need to change at some point. Rather than just typing in the literals (typing in 3.14 in 80 functions) you type in the variable. constants usually arn't that common. I've used them mostly for global variables that cannot be changed after compilation. Quote
EFileTahi-A Posted August 8, 2004 Author Posted August 8, 2004 tks for posting out m8.. truly heplfull... yet.. another question (simple one) when i change my class name it will not be updated! that is, when i type namespaceName, it lists me the old class name instead the new one.. strange... How can i walk around this? am truly noob in .NET tks once more Quote
Denaes Posted August 8, 2004 Posted August 8, 2004 C# is no VB.Net. 2002 and 2003 have rudimentary intellisense. I found that basically you have to rebuild (or just normal Build) the application after changes to have intellisense pick it up. So that's good practice to get into in C#. C# 2005, you can download the beta off of the Visual Studio homepage, has VB.Net quality Intellisense. It's really nice :) Quote
EFileTahi-A Posted August 8, 2004 Author Posted August 8, 2004 C# is no VB.Net. 2002 and 2003 have rudimentary intellisense. I found that basically you have to rebuild (or just normal Build) the application after changes to have intellisense pick it up. So that's good practice to get into in C#. C# 2005, you can download the beta off of the Visual Studio homepage, has VB.Net quality Intellisense. It's really nice :) Well am building it and rebuild it and he have not updated my new class name... Am begining to be frustated... Quote
Joe Mamma Posted August 8, 2004 Posted August 8, 2004 Well am building it and rebuild it and he have not updated my new class name... Am begining to be frustated... In the Class View Explorer, drill down your namespace to hilight the class in question. In the properties window, change the name there. BTW, Class View has some wizards available in C# that arent in vb.net making C# a much freindlier language to develop on. . . right click some elements in the class view and you will see what I am talking about. Quote Joe Mamma Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized. Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.