Click to See Complete Forum and Search --> : C# Design...


Jason Isom
August 2nd, 2005, 07:54 PM
public class Foo
{
public static void Main()
{
int x = 100;
Bar b = new Bar(ref x);
b.func();
Console.WriteLine(x); // I'm trying/wanting this to output 110
}
}

public class Bar
{
private ref int y; // I am aware that this does not work

public Bar(ref int x)
{
y = x;
}

public void func()
{
x += 10;
}
}

Can I do anything to achieve this functionality? I know that it doesn't work this way...

The reason I ask is because if it's not possible, I'm going to have to rethink my design completely.

I posted earlier about my problem with maintaining multiple forms...and this is a continuation of that problem.

I have a couple of forms: a LoginForm and a MainForm.

What happens is that the user keeps trying to Login until he/she gets a successful login. At that time I'd like to pass the User structure I've created to the MainForm as an argument to the constructor.

Now this isn't that big of a problem because MainForm doesn't change anything in the User class, however MainForm eventually does create another form that does alter the User structure. Now it's important that it does change the original structure in LoginForm because when LoginForm calls the Closing event, it serializes my ArrayList of Users and it should be an updated ArrayList.

MadHatter
August 2nd, 2005, 08:34 PM
I would probably rethink the design (IMO). you can create a globally accessible structure that can hold the credential info so that after login, any subsequent form can access this object in its constructor without having to pass or maintain it.

passing by ref in managed code has a little more overhead with it because its a managed reference as opposed to dereferencing a pointer.

ex:

public class UserDetails {
private static UserDetails _instance;
public static UserDetails Instance {
get {
if(_instance == null) _instance = new UserDetails();
return _instance;
}
}
string _user="", _password="";
public string UserName {
get { return _user; }
set { _user = value; }
}
public string Password {
get { return _password; }
set { _password = value; }
}
private UserDetails() {}
}

then any form can use it:

login:
// ... when user presses ok button after entering user / password

UserDetails.Instance.UserName = userName.Text;
UserDetails.Instance.Password = password.Text;

then any subsequent form can check:

// ... inside the form's constructor:
if(UserDetails.Instance.UserName != "sam" && UserDetails.Instance.Password != "i am")
Dispose();

InitializeComponents();

its a way to refer to the same data from several different places without having to pass its reference. of course you would want to make it thread safe (which I did not) because you have the *possibility* of accessing this info from several places @ the same time.

Jason Isom
August 2nd, 2005, 09:10 PM
Awesome! Thanks for the information.