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

Thread: c# Form Arguments?

  1. #1
    Join Date
    Apr 2006
    Posts
    48

    c# Form Arguments?

    im working on a private project which im running into a little bit of trouble with... in a nutshell, its an application which keeps track of acquisitions and dispositions of specific items, while keeping track of who the item was acquired from, and who it was transferred to. because, in this case, the third parties are kept in a separate table, i've got a form which allows the user to select previous customers from that table, and it will autocomplete the data in the nessicary form....

    the ideal solution in my mind would be to use arguments both ways, just like you can use arguments to pass data between functions in a single class, but i have no idea if this sort of thing is even possible?

    is it? and how where should i start?

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: c# Form Arguments?

    The fastest solution is:

    use the arguments from your main,
    put it into a static class and read those values
    from any other point in your application.

    The static class will not loose its values and is
    accessible from everywhere.


    It is much easier than passing the
    values over Form constructors which sux
    if you have to do it all the time.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: c# Form Arguments?

    Quote Originally Posted by MNovy View Post
    The fastest solution is:

    use the arguments from your main,
    put it into a static class and read those values
    from any other point in your application.

    The static class will not loose its values and is
    accessible from everywhere.


    It is much easier than passing the
    values over Form constructors which sux
    if you have to do it all the time.
    I...would not do this. Static, publicly accessible data can lead to other issues. Why not use the event based mechanisms which are built into the language for just such a problem? You can also expose properties on your child form which may be read from the mainform.

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: c# Form Arguments?

    To add to BigEd781's contribution I would say that the use of static variables tends to hide the dependencies between the differen parts of the application (and most likely produces tight coupling between the classes that could be avoided). It can also make the different elements of the system difficult to test independently. The ease of testing is a desireable thing as it will be easier to build the system gradually and pinpoint errors when they arise.

  5. #5
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: c# Form Arguments?

    It depends on what you aim is.

    If you only want to store some arguments for delayed reading,
    a static calss is a good choice. Especially if you read only from
    one point there is no negative issue with it.

    Otherwise you have to put your arg[] from the main to to the Form constructor.
    But if you call this Form a lot, you always have to insert a null array,
    which is completely nonsense and unneeded extra code.

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: c# Form Arguments?

    Quote Originally Posted by MNovy View Post
    It depends on what you aim is.

    If you only want to store some arguments for delayed reading,
    a static calss is a good choice. Especially if you read only from
    one point there is no negative issue with it.

    Otherwise you have to put your arg[] from the main to to the Form constructor.
    But if you call this Form a lot, you always have to insert a null array,
    which is completely nonsense and unneeded extra code.
    Both your points are valid. I would just add that idea of passing arguments to constructors is to ensure that the class is initialised with all the data that it needs to work correctly rather than having separate initialisations methods which may not be called. You said it well it depends on what your aim is. If I were building a medium to large system where data needs to be shared between different components I would keep my use of static variables to a minimum. At the end the original poster will have to decide what is best for his situation.

  7. #7
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: c# Form Arguments?

    Static storage is not a good idea for this inquiry. It is not thread safe, if the app goes that route, and is so commonly mis-used and abused that it's just asking for trouble.

    I don't know/understand the application requirements, but based on the replies if I had to choose between storing data statically in your application, and passing instanced information across forms through the use of constructors, I'd go with the latter any day...

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,220

    Re: c# Form Arguments?

    Another option is to use a singleton class to pass the data around between forms. A singleton class can be make thread safe if need be.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)