NekoManu Posted July 23, 2004 Posted July 23, 2004 I'm very new to C# and .NET. I'm trying to rewrite my old VB applications in C# and I started with creating a library, forms and a few controls. I actually have a few problems. 1) Error with displaying form I have a form based on Form, displaying that with ShowDialog is no problem. I also have a second form based on my first form, displaying that with ShowDialog gives an error. namespace soForms { public partial class MNLForm : Form { private byte _DBStatus = 0; // soSystem.cUnChanged; public MNLForm() { InitializeComponent(); } public byte DBStatus { get { return _DBStatus; } set { _DBStatus = value; } } } } namespace soForms { public partial class MNLAboutForm : MNLForm { public MNLAboutForm() { InitializeComponent(); this.Icon = Owner.Icon; this.Text = "About "; // + soSystem.AppTitle } } } In another application I want to display the form: soForms.MNLForm dlgAbout; dlg = new soForms.MNLForm(); ==> No Problem soForms.MNLAboutForm dlgAbout; dlgAbout = new soForms.MNLAboutForm(); ==>Errior: NullReferenceExeption was unhandled. Object reference not set to an instance of an object. 2) Using a class in my controls I have a class soSystem. And I want to use this class in my forms, controls, ... namespace soSystem { public class soSystem { // Properties public static string ApplicationTitle = ""; public static string CompanyName = "MNL-Software"; } } In another solution: private void button1_Click(object sender, EventArgs e) { MessageBox.Show(soSystem.CompanyName); } This does not work. Can someone tell me why and how to make it work? Quote
Administrators PlausiblyDamp Posted July 23, 2004 Administrators Posted July 23, 2004 If you step through the code in a debugger which line of code does it actually crash on - i would check the value of Owner.Icon isn't null before assigning it to the icon. Also when you say MessageBox.Show(soSystem.CompanyName); doesn't work - doesn't work in what way? Also if you are developing in a beta version of a product then you can expect problems - beta software isn't really designed to be used (in fact often the licensing agreement forbids it) in a production environment. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted July 23, 2004 Author Posted July 23, 2004 I tried removing the line with: this.Icon = Owner.Icon; and it seems to work. The error I get with the soSystem is: The type or namespace name 'CompanyName' does not exist in the namespace 'soSystem' (are you missing an assembly reference?) Thanks. Quote
Administrators PlausiblyDamp Posted July 23, 2004 Administrators Posted July 23, 2004 You probably need to use soSystem.soSystem.CompanyName - I would personally recomend against giving a namespace the same name as an enclosed class. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted July 23, 2004 Author Posted July 23, 2004 Thanks a lot; it works! I'll remember that about the namespace. Quote
Arch4ngel Posted July 23, 2004 Posted July 23, 2004 By the way... don't give direct access to your variable. Make property read-only (get{} in c# without set{}) Quote "If someone say : "Die mortal !"... don't stay to see if he isn't." - Unknown "Learning to program is like going out with a new girl friend. There's always something that wasn't mentioned in the documentation..." - Me "A drunk girl is like an animal... it scream at everything like a cat and roll in the grass like a dog." - Me after seeing my girlfriend drunk and some of her drunk friend. C# TO VB TRANSLATOR
*Experts* mutant Posted July 23, 2004 *Experts* Posted July 23, 2004 You could use the assembly information that you can set to store the program title, company name. Then retrieve those strings using the System.Windows.Forms.Application class (It will automatically display that info too if someone views the file properties in Explorer). You don't need a new class for that :). Look for a file called AssemblyInfo.cs in your project and fill in that information there. Just a little observation :). Quote
NekoManu Posted July 25, 2004 Author Posted July 25, 2004 Arch4ngel I have a question for you. It seems like a very good idea to make these properties read-only, but I have a little problem with that. It doesn't really work and how can I enter the values for those properties? What I want to do is create a class with properties and methods that I use in every application. At the start of an application I enter the applicationname, companyname and so on. I also would like to get the user and usercompany from the registry when the application starts. I can't use the constructor of the class, because at that time the ApplicationName is not known yet. So I thought of getting the User and UserCompany when I fill in the ApplicationName. The problem is that I can't use a get/set on a static variable and if I make my variables not static, I can't access them in my applications. This is my soSystem: namespace MNLSystem { public class soSystem { // Constants // Properties public static string ApplicationTitle = ""; public static string ApplicationVersion = "V"; public static string ApplicationCopyright = "Copyright © 2004"; public static string LicenseUser = "Unknown User Name"; public static string LicenseCompany = ""; public static string LicenseSerialNumber = ""; } } Quote
Administrators PlausiblyDamp Posted July 25, 2004 Administrators Posted July 25, 2004 As Mutant stated earlier assemblyinfo.cs provides standard ways of entering some of this information - if you fill in the attributes there this information will be accessable through code and also appear in the .exes propertypages in explorer. Also just as an aside if don't go the AssemblyInfo.cs route you may as well mark the strings that don't change as const to make them constants rather than variables. One thing you could do is mark the others readonly - this means you can assign values to them within the constructor but not modify them afterwards. i.e. public class soSystem { static soSystem() { //assign values to readonly values here ApplicationTitle= "Test"; //etc } // Properties public readonly static string ApplicationTitle = ""; public const string ApplicationVersion = "V"; public const string ApplicationCopyright = "Copyright © 2004"; public readonly static string LicenseUser = "Unknown User Name"; public readonly static string LicenseCompany = ""; public readonly static string LicenseSerialNumber = ""; } you would just have to use your own code to set the values. Personally I would use the assemblyinfo for those attributes that are not needing to be dynamically assigned (although possibly providing wrapper methods in your soSystem class to provide a single logical point of access) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted July 26, 2004 Author Posted July 26, 2004 Can someone tell me what is wrong with this code? When I run this I get "Missing method void set_ApplicationTitle(string)" on the following line: soSystem.ApplicationTitle = "My Application"; This is the class I use: namespace MNLSystem { public class soSystem { private static string FApplicationTitle=""; public static string ApplicationTitle { get { return FApplicationTitle; } set { FApplicationTitle = value; LicenseUser = "Neko"; } } public static string ApplicationVersion = "V"; public static string ApplicationCopyright = "Copyright © 2004"; public static string LicenseUser = ""; public static string LicenseCompany = ""; } } Quote
Administrators PlausiblyDamp Posted July 26, 2004 Administrators Posted July 26, 2004 Just cut and pasted your code into a new project and it ran fine here. Could you post some more of the calling code or is it just that one line? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted July 26, 2004 Author Posted July 26, 2004 I think it is that line, but here are the first lines of code: namespace MNL_Test_Application { partial class frmMainForm : Form { public frmMainForm() { InitializeComponent(); soSystem.ApplicationTitle = "Multimedia Administration System"; } } } Quote
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.