aewarnick Posted November 6, 2003 Posted November 6, 2003 Here is my Form class' constructor: public OptionsForm(a.graphics.DrawingSurface ds, ref Color transpCol)DrawingSurface is a class, Color is a structure. When I change any of ds's members they change the original members. Now, I know that all classes are passed by ref but I thought that to change the members permanantly you had to pass the class by ref. That is the first issue. The second is the real mind boggler, and frustrator. In the OptionsForm, I have a variable that is global and is initialized in the constructor to the transpCol variable. this.transpColor= transpCol; Later in the Form I set this.transpColor to a color value, which should change the value of the variable passed in the constructor, but it does not! However, if, in the constructor I change the value this.transpColor= Color.Black; it does change the color of the variable passed! Am I not understanding something? Here is the top part of OptionsForm: public class OptionsForm : System.Windows.Forms.Form { public Color transpColor= Color.Empty; private System.ComponentModel.Container components = null; public OptionsForm(a.graphics.DrawingSurface ds, ref Color transpCol) { InitializeComponent(); this.transpColor= transpCol; } Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 It doesn't work that way. Unless you change transpCol (the parameter, not the member) in the constructor, it will not change the referenced variable. Also, using the same name for the parameter and the member could be causing problems. For an options form, you should create a structure, likepublic struct DrawOptions { a.graphics.DrawingSurface drawSurface; Color transpColor; }Then, create a method to show the form, and return the options struct:public DrawOptions ShowOptions() { DrawOptions retVal; this.ShowDialog(); retVal.drawSurface = blah1; retVal.transpColor = blah2; return retVal; }Then use ShowOptions instead of ShowDialog. Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 (edited) Thanks VolteFace. Just to clarify, I did some more testing and found what you said about assigning to the passed variable directly will change the original. That is what I meant up in my post above And also, the variables are not the same, you just looked at them too quick:D. I did make the OptionsForm member public so that it can be accessed, that was my workaround. But I didn't think it was the best way to do things. I think in C++ you can do what I am trying to do, am I right? And if you can in C++ there is probably a way using maybe, unsafe code. Correct? Edited November 6, 2003 by aewarnick Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 You might be able to with references, but the way I suggested is the way you should go about it. Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 How do I return anything but DialogResult which is of type enum? Quote C#
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 Sorry, didn't read the code very well. Now I see exactly what you meant. I would change ShowDialogs return type at all. I am still going to try to do this the C++ way for future projects. If you can help with that, I would be happy. Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 What do you mean the C++ way? It would be the same no matter what language you use. Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 What I mean is being able to keep the address of the passed variable all throughout the OptionsForm and directly changing the value. Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 If you are using C#, I have my doubts as you whether you can accomplish it. If you were using C++, you could store the pointer to the variable easily and manipulate that. Using unsafe code is a possibility I suppose. I'll have to look at that. Is there a particular reason you want to use that way over the UDT way? Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 I want C++ flexibility. What is UDT? Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 A UDT is a user-defined type in VB. I meant to say "structure", sorry. Anyway, I really don't understand what you're talking about. What do you mean by "C++ flexibility"? Storing pointers and manipulating their values is no more flexible than returning a structure and assigning the values upon returning. Aside from that, using the struct is much easier and cleaner. Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 You're right, it is cleaner. I guess I just fantisize about C++ sometimes. It can be fun! I am looking up in help right now about how to allow unsafe code in my app so that I can start enjoying myself! Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 Maybe you should try C++.NET? Quote
aewarnick Posted November 6, 2003 Author Posted November 6, 2003 I have seriously considered it but never heard anything really good about it. Quote C#
*Experts* Volte Posted November 6, 2003 *Experts* Posted November 6, 2003 Perhaps you should do a search in the MSDN. You're sure to find something. 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.