Question Can't access class resides in App_Code

dzung

New member
Joined
Aug 4, 2019
Messages
2
Programming Experience
1-3
Hi there
I have created a website in ASP.NET and have created a class and put it inside of the App_Code folder. When I run the website locally (http://localhost:4300/Default.aspx), it works fine. However, when I upload it on to the server and run the browser, it produces a "Compiler Error Message: CS0103: The name 'StaticHelpers' does not exist in the current context".

Source Error:

Line 44: if (int.TryParse(redRange.Text, out red))
Line 45: {
Line 46: color = StaticHelpers.getColor((byte)red, (byte)blue, (byte)green, bGray);
Line 47: }
Line 48:

C#:
        // The following codes are in Default.aspx.cs

        public void showColorAndStatus()
        {
            green = int.Parse(rbl.SelectedValue);
            blue = int.Parse(ddl.SelectedValue);
            bGray = cbxGrayscale.Checked;

            if (int.TryParse(redRange.Text, out red))
            {
                color = StaticHelpers.getColor((byte)red, (byte)blue, (byte)green, bGray);
            }

            _show.ForeColor = color;
            _show.BackColor = color;
            _status.Text = "";
        }

        // The following codes are in StaticHelpers.cs which resides in the App_Code folder.

        public class StaticHelpers
        {
            public static Color getColor(byte red, byte blue, byte green, bool bGray)
            {
                Color color;

                if (!bGray)
                    color = Color.FromArgb(255, red, green, blue);
                else
                {
                    int greyScale = (red + green + blue) / 3;
                    color = Color.FromArgb(255, greyScale, greyScale, greyScale);
                }
                return color;
            }
        }

Any suggestions will be greatly appreciated.
Thank you in advance.
 
Last edited by a moderator:
Did you try an iisreset, or dropping in and then deleting an app_offline.htm to try to get the app pool to reload (and therefore re-compile all found code)?
 
Just like lines 5 and 6 were just setting blue and green which are presumably class variables, I assume that red was also a class variable and so should not require a type.
 
What namespace is StaticHelpers declared in? Are you including that namespace?

What exactly is your deployment process? Do you cherry pick files out of your local machine, or do you use the Publish to folder option in Visual Studio, and tree copy that folder over into your IIS server.
 
But neither was blue, green, nor bGray. But somehow the compiler didn't complain about lines 5, 6, 7, or 9. It was complaining about the StaticHelpers class not existing in the current context. ... unless the OP is hiding other error messages that he is encountering.

And even if the OP wrote line 9 as:
C#:
if (int.TryParse(redRange.Text, out int red))
and further assuming that the OP is using a version of ASP.NET that support the C# 7.0 syntax, it still shouldn't affect the error regarding StaticHelpers not being declared within the context.
 
Last edited:
Source Error:
Line 44: if (int.TryParse(redRange.Text, out red))
Just pointing out the obvious despite what version is being run. Looking at where the error is reported, I'd say this has something to do with it.
Compiler Error Message: CS0103
That compiler issue reports a general scoping issue or an unreachable declaration, one of which would generally imply it may be outside the scope of the operating code. See here : Compiler Error CS0103

As for this : public class StaticHelpers - while you may have static members in an instance based class which are still reachable without creating instance of them, it really does defeat the ideals behind having a static class that isn't actually static itself.

Next; since you are trying to parse as int : green = int.Parse(rbl.SelectedValue); - I assume green is meant to be int also. So you shouldn't be getting any error there when casting, which brings me back to the above compiler message error, and conclude it must be some kind of declarative issue. Given our OP only has 1-3 years experience, it is best that they post the rest of their code so that it can be assessed fully, instead of arguing semantics and what might and might not be.
 
That compiler issue reports a general scoping issue or an unreachable declaration, one of which would generally imply it may be outside the scope of the operating code. See here : Compiler Error CS0103
Yes, but the C# compiler is also smart enough to report which symbol it can't find. In this case, the OP reports that the symbol that cannot be found is StaticHelpers, not red. If this were a C++ compiler complaining about a symbol not being in the current context, I too would be suspicious about red even though the error was about StaticHelpers if there was a complaint about line 11.

Furthermore, if red were not declared, there should have been an error about red not being in the current scope on line 9... before we even get to line 11. The nature of the ASP.NET C# compiler for App_Code is that is stops on the first failure and won't bother reporting succeeding errors, unlike Visual Studio or csc.exe. So if there was an error on line 9, then the error on line 11 would not be reported. But our OP is telling us the error was on line 11, so that means line 9 was good and red was declared somewhere on scope.
 
Back
Top Bottom