CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: C# Design...

  1. #1
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    C# Design...

    Code:
    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.
    Last edited by Jason Isom; August 2nd, 2005 at 08:11 PM.
    Jason Isom
    .NET 2.0 / VS2005 Developer

  2. #2
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: C# Design...

    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:

    Code:
    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:
    Code:
    // ... 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:

    Code:
    // ... 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.

  3. #3
    Join Date
    May 2004
    Location
    Greenville, NC
    Posts
    193

    Re: C# Design...

    Awesome! Thanks for the information.
    Jason Isom
    .NET 2.0 / VS2005 Developer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured