Question

When writing this post i was checking all questions asked with related subject and couldent find a simple answer to this Newbs C# issue

i would like ... if i may , to have as much as it can be a well-explaind answer (Please!)

i made a public static class

        public static class MyLoadedBtmpToolBox
        {
            public static string FnameToLoad;
            public static bool PutLoadedByteArrInPicbox;
            public static string[] LoadedRefrnce;
            public static int SourceX_Loaded, SourceY_Loaded, RectWidth_Loaded, RectHeight_Loaded;

--->            public static int tst = Str2Int(LoadedRef[0]);

            public static byte[] LoadedByteArr;

        }

this calss as usually does by default, is within the main form i am using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.IO;
using System.Security.Cryptography;
using WindowsInput;

namespace MyScrCupTry1
{
    public partial class MyForm1 : Form
    {

            public static class MyLoadedBtmpToolBox
            {


              public static int testStr2Int = Str2Int("100");


            }




          public int Str2Int(string STR)
          {

            int strInt = Convert.ToInt32(null);
            if (isntEmpty(STR))
            {
                strInt = Convert.ToInt32(STR);

            }
            else
            {
                MessageBox.Show("theString " + STR + " is Null");
            }
            return strInt;

     }

}

i can't assing the testStr2Int a value , by using my public "helper Method" Str2Int() from the main form i am Getting Error :

Error 1 An object reference is required for the non-static field, method, or property 'MyScrCupTry1.MyForm1.Str2Int(string)' G:\RobDevI5-Raid-0\Documents\Visual Studio 2010\Projects\WindowsFormsApplication2\WindowsFormsApplication2\MyForm1.cs 95 45 MyScrCuptry1

What is the right way for accessing Main Form Public elemets from a static Class if it is possible / (not illegal)...

  • ReEditing

after Those two answers ...

I tried to implement the code from first answer without expected results i guess i didn't know the right code structure with override OnLoad(..) thing ...

BUT ! i have turnd the method Str2Int(STR) from public into public static

so now elements from the form itself still have access (i am surprised) to Str2Int() and from the static Class I Can Access it too....

and its all thanks tp making it into static too ,

am i missing somthing else is there "hidden" drawback when changing Str2Int() from public into public static ?

Was it helpful?

Solution

The point of static code is that belongs to itself, rather than any other object. For this to be true, when you set something as static, everything that depends on it must also be static!

This is what your error message is trying to tell you. You are declaring something as static, but in the computation of that static, you are using something that is not static. Str2Int is not tagged as static, so that is an immediate problem, and it's possible that LoadedRef is also not static. I am half-sure you actually meant to use LoadedRefrnce there, in which case you're fine, but since you spelled nothing correctly I can't be sure!

Check this page for an explanation of the static keyword, also please make more of an effort to read up on C# coding conventions - this makes it much easier for people to read your code when you ask for help like this!

Expanding upon the edits above:

The 'drawback' to making code static is that it pretty much instantly makes everything it is a part of untestable. The idea behind unit testing is to have all of your code broken out into totally replaceable parts, so they can be tested separately and moved out separately (if need be). By making a bunch of code static, you are essentially welding it to any other code it might be a part of. In your own example, by making Str2Int() public static, everything that uses Str2Int() is now untestable!

Static code is, generally speaking, a vice that you should attempt to avoid as much as possible. There are places where you can't, and if you are just starting to learn really the biggest focus is just getting something working. But be ready to look back on this code and go cold at how you could've ever written something so bad in a few years, when you are more experienced and confident in your skills.

OTHER TIPS

add Form object to static class, like this :

public static class MyLoadedBtmpToolBox
{
     public static Form MainForm {get;set;}

     public static int testStr2Int = MainForm.Str2Int("100");
}

and from the Form :

public partial class MyForm1 : Form
{
     private override OnLoad(..){

        base.OnLoad(...);    
        MyLoadedBtmpToolBox.MainForm =this;

     }
}

This design is fragile by itself, as you has to gurantee that MyForm property is intialized before it's used to call ..testStr2Int = MainForm.Str2Int("100")... So, considering that testStr2Int is public, may be intialization of it you can do in Form too and not in static class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top