Results 1 to 28 of 28

Thread: [RESOLVED] Form class variables and static functions

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2004
    Posts
    185

    [RESOLVED] Form class variables and static functions

    I'll start by saying that I'm new to C# and to .NET, I'm used to VB6 where I've never worried about alot of this stuff much.

    So, I have a class, say, Form1. Form1 has some public variables, say, variable1, and variable2. Now I also have a static function: public static int function1(int input1). Within function1's definition, I cannot access Form1.variable1, or Project1.Form1.variable1, and I was confused as to why. To work around, I added to the beginning of function1:

    C# Code:
    1. Form1 TempForm = (Form1)Form1.ActiveForm;

    This works, but only to a point, and it comes down to the ActiveForm, I think. If I run the project and the activeform changes (in my case an activex control can open a form with settings for that control), the program crashes with a NullReferenceException at the point where it attempts to access TempForm.variable1. Presumably this is because the active form does not have this variable?

    So my question is, how could/should I be accessing variables within a Form Class? Are you not meant to? This isn't limited to static functions either, I don't think, I really can't access them ever without using this or creating an instance like I did inside my example of function1. If what I did is alright to do, then how should I handle the activeform changing when you select a different form within the same project?

    I guess I'm really just unfamiliar with creating instances properly in general, but since I've been using Forms alot, I've found it frustrating to access their variables, etc.

    I also forgot to mention that I get warnings doing it the way I'm doing, talking about the possibility of a runtime exception regarding accessing the variables because they are fields of a Marshal-by-reference class. Can someone explain Marshaling briefly and how it is used?

    I know this is alot to ask, but I think some simple code that works and is "right" can clear this all up for me.

    Andrew
    Last edited by pjrage; Apr 26th, 2007 at 06:30 AM. Reason: RESOLVED

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