URGENT: Program Messed up by #define ?!? HELP!
I have been working on a project and I wanted to have some features available depeding on if there was something defined... so... I went:
#if( VERSION_2 )
private System.Windows.Forms.GroupBox gbVersion2;
....
....
#endif
at the beginning of my MainForm class... (I didn't add it any place else yet)
VERSION_2 was NOT defined yet and I wanted to change something change a name of something and I went to look at my Main Form in design view. Obviously it gave an error about gbVersion2 not being defined. I clicked okay, the groupBox didn't show up... I changed what I need to then went back to my code, commented out the #if( ... ) and #endif so it was just like it was when I started... now that groupBox does not show up on my form, when I try to run my app it says something about a null reference exception for my groupbox... I checked the code and it is exactly the same as it was before I added the #if... What happened and how can I reverse the changes? I'm desperate....
[1052 byte] By [
lskuff] at [2007-11-19 19:57:11]

# 1 Re: URGENT: Program Messed up by #define ?!? HELP!
well, first off, the syntax is:
#if VERSION_2
no paren's
second, your resource file is probably hosed. dont open the form in design view, but make sure your objects are initialized & all the properties (that you need) are set in InitializeComponents()
if everything is correct from a code standpoint, delete the resx file, and re-open the form in design view (it should re-build your resource file)
the designer (especially in vs 2002 / 2003) are quick to delete things from your class, so be carefull when you open it, especially if you have pre-processor directives anywhere on your form (in fact, if you have preprocessors dont open it in designer at all).
# 2 Re: URGENT: Program Messed up by #define ?!? HELP!
thanks for your response.... After spending a while cleaning up that resx file I think I got my app up and running again... what a pain in the ***.
Anyways, is there a better way to do what I want to do in C# (hide some of the features from the user, but have in place for when I release the next version of the software) besides using preprocessor directives? I'm used to C where I would use them a lot.
Thanks -
lskuff at 2007-11-9 11:21:19 >

# 3 Re: URGENT: Program Messed up by #define ?!? HELP!
you can use the preprocessor, but the because of the way the designer works, its going to hose your projects if you use the preprocessor in designer code. I use them all the time, just get things the way you want the full version to look in the designer (so you dont have to go back to it), then close out the designer and #if away.
if you have to have the preprocessor and design time code working in tandem, then your best bet would be to #if #else the entire initialize components method.
good luck.
# 4 Re: URGENT: Program Messed up by #define ?!? HELP!
Interfaces would do what you want. They started with COM interfaces like IErrorInfo. I believe there is a similar concept in C# but I have yet to make use of it. You can query an object to see what interfaces are available: for example: object has InterfaceVersion1 and InterfaceVersion2. This saves backwards compatibility (ie InterfaceVersion1) and can use extended functionality (ie InterfaceVersion2) if that object has that interface available.
Tron at 2007-11-9 11:23:22 >

# 5 Re: URGENT: Program Messed up by #define ?!? HELP!
interfaces wont solve his problem. he's trying to conditionally compile his source code, not adhear to one interface or another.
# 6 Re: URGENT: Program Messed up by #define ?!? HELP!
Your right in the way the question is stated. My point is if he wants to have only one build of the program he could accomplish the same thing by compiling always the same way but just have the additional functionality in a second interface, then make that interface available via a .CONFIG setting or however.
I really don't see a need to conditionally compile the code ala C-style when interfaces are available in C# and they would do the same thing much cleaner.
Your point is correct though, just trying to point out another option he may not have been aware of.
Tron at 2007-11-9 11:25:19 >

# 7 Re: URGENT: Program Messed up by #define ?!? HELP!
with interfaces you still have to implement it. Recently, I wrote an ftp client. one version was standard ftp the other supported ftps. if I used interfaces, the 2 assemblies would be materially different. using the preprocessor, they were the same assembly (meaning their base types were identical) but one included ssl / tls in its tcp layer, api wise, they were identical.
theoretically he could extract some interface, but then he still has the same problem, and that is, the actual implementation of that interface (where he was when he posted). using an interface still requires him to implement what he already has. when you contitionally compile, all you need to do is include a define.
# 8 Re: URGENT: Program Messed up by #define ?!? HELP!
Now I'm curious or maybe just confused...
Versioning was a big reason for the creation of interfaces. When I wrote COM interfaces in C++ I had my own interface say IMyInterface. When additional functionality was added to the object I just created a second interface and inherited it from my original interface IMyInterface, like IMyVersion2Interface : IMyInterface.
Why would there be a problem with the two assemblies being materially different? Or is that only an issue for certain programming applications?
I do understand that precompiler directives were left in C# for a reason and they do serve a purpose.
Tron at 2007-11-9 11:27:25 >

# 9 Re: URGENT: Program Messed up by #define ?!? HELP!
versioning is different in .net
whatever reason microsoft chose to include interfaces in .net for I dont think it was for version control, more like lack of multiple inheritance. interfaces allow you (among other things) to enforce a template definition across objects, and use that interface as a type so that you can access types that may be unknown other than that they implement an interface. I'm sure it has something to do with COM as well, since it uses that
versioning is done based on quite a lot more info in .net (version #, type, strong name and so on). creating interfaces like that was more of a way around the lack of multiple versioning in COM more than it was "the way it was supposed to be done." because you couldnt have 2 objects that differed in version only, you had to declare another interface so that you didnt stomp on another assembly who used the orignial one.
# 10 Re: URGENT: Program Messed up by #define ?!? HELP!
Looks like I have some research to do before creating version 2 of my C# projects.
Thanks for the input!
Tron at 2007-11-9 11:29:21 >
