Another mind boggling ref question!

aewarnick

Senior Contributor
Joined
Jan 29, 2003
Messages
1,031
Here is my Form class' constructor:
C#:
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.
C#:
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
C#:
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:
C#:
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;
                                 }
 
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, like
C#:
public struct DrawOptions {
  a.graphics.DrawingSurface drawSurface;
  Color transpColor;
}
Then, create a method to show the form, and return the options struct:
C#:
public DrawOptions ShowOptions() {
  DrawOptions retVal;

  this.ShowDialog();
  retVal.drawSurface = blah1;
  retVal.transpColor = blah2;

  return retVal;
}
Then use ShowOptions instead of ShowDialog.
 
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?
 
Last edited:
You might be able to with references, but the way I suggested is the way you should go about it.
 
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.
 
What do you mean the C++ way? It would be the same no matter what language you use.
 
What I mean is being able to keep the address of the passed variable all throughout the OptionsForm and directly changing the value.
 
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?
 
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.
 
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!
 
Back
Top