locked
static class/method not recognized within namespace RRS feed

  • Question

  • From what I've read, C#'s static modifier should work just like Java. So, I created a utilities class which has a public static method to do some things. The utilities class is declared within the same namespace as my form class (just in a separate file), but if I attempt to access the method in the utilities class from the form class, the compiler complains that the name of the utility method or the class (if I attempt to use the FQN of the method) does not exist in the current context.

    If I attempt to add a "using MyNamespace.Utilities" statement at the top (which I fully expect to be redundant) the compiler also complains that the "Utilities" type or name does not exist in "MyNamespace".

    Any suggestions are most welcome! According to wrox, I'm doing this correctly...


    Friday, July 7, 2006 8:10 PM

Answers

  • Is MotorUtilities in a different project?  The only way I have been able to replicate the problem you are seeing is by putting the MotorUtilities class in its own project and then not referencing it from the Forms application project.

    Regards,

    Donavan

    Monday, July 10, 2006 2:57 PM

All replies

  • I can tell you what's wrong with your code, if you don't let me see it....

     

    Things to check:

     Is the class declared public?

     Is the method declared public?

     

    Friday, July 7, 2006 8:16 PM
  • I'll agree with James:  seeing the code would make this easier.

    Another hunch:

    If Utilities is a class, you must use the namespace:

    using MyNamespace;

    And scope the method call using the class name:

    Utilities.MyCoolMethod(...);

    Hope that helps.

    Saturday, July 8, 2006 12:06 AM
  • Jonathan,

    The 'static' modifier does not work like Java's static import feature. The static modifier is applied to a class, and indicates to the compiler that the class is a static holder class, that is, a class that contains only static members. However, you can't 'import' that class like a namespace, you will still need to prefix any methods in the static class with the class name itself.

    Regards

    David

    Saturday, July 8, 2006 7:22 PM
  • Many thanks to everyone for their posts.  I believe I'm following everything that's been suggested, but the compiler still complains.  I apologize for not posting code sooner but as it is somewhat lengthy and the concept (supposedly) simple I thought perhaps there might be something very basic I was missing (being new to C#).

    Below is the utility class snippet followed by code from the form class (with the utility method highlighted in bold):

    using System;
    namespace RCAWS_GUI
    {
       public static class MotorUtilities
        {
                    unsafe static public byte[] convertMotorData(MotorType type, int rawData)
                    {
                       //deletia
                    }
        }
    }
     
    The form class (which is o/w lengthy, hence the abbreviations) calling this method is declared so:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using IbisTekUtilities;
    using System.Net;
    using System.Diagnostics;
    using System.Net.Sockets;
    using System.IO;
    using System.Threading;


    namespace RCAWS_GUI
    {
        public partial class frmMain : Form
        {
                ButtonState prevDeadmanState = ButtonState.RELEASED;
       int prevLeftJoyX = 128;
       int prevLeftJoyY = 128;

            // deletia

            // ------------------------------------------------------------
            // Left Analog Joystick
            // The left joystick is mapped to Motor Control.
            // ------------------------------------------------------------
            void gamepad_leftAnalogJoyMoved(int axisX, int axisY)
            {

                byte[] data = new byte[4];

                // Only do something if the deadman switch is pressed

                if (prevDeadmanState == ButtonState.PRESSED)
                {
                    if (axisX != 128)
                    // joystick is pushed left or right of center
                    {
                        prevLeftJoyX = axisX;
                        data = MotorUtilities.convertMotorData(MotorType.EL, axisY);
                    }

                    if (axisY != 128)
                    // joystick is pushed up or down of center
                    {
                        prevLeftJoyY = axisY;
                        data = MotorUtilities.convertMotorData(MotorType.EL, axisY);
                    }
                }   // END if deadman PRESSED

            }   // end gamepad_leftAnalogJoyMoved

         // deletia

        }   // END class Form

    }   // END namespace RCAWS_GUI



    Again, many thanks to everyone for their input.  I'm certain there is something blatantly obvious to someone who's not a newbie.


    Monday, July 10, 2006 1:28 PM
  • What line is the compiler complaining about? And what is the error message?
    Monday, July 10, 2006 2:34 PM
  • The lines in bold. Error text (for both lines):  The name 'MotorUtilities' does not exist in the current context  

    The MotorUtilities class is defined in a file named MotorUtilities.cs.
    Monday, July 10, 2006 2:36 PM
  • Is MotorUtilities in a different project?  The only way I have been able to replicate the problem you are seeing is by putting the MotorUtilities class in its own project and then not referencing it from the Forms application project.

    Regards,

    Donavan

    Monday, July 10, 2006 2:57 PM
  • Jonathan -

    I realize this post is several years old - but I'm experiencing the same (mystifying issue).  I have a static utility class and after doing several things in the process of migrating my project to VS 2008 and cleaning up code analysis issues my static methods aren't being seen by client code.  The strange thing is that intellisense recognizes the methods.  I've tried everything I can think of to fix it, but without success so far.

    Thanks,

    Marcellus Tryk
    Marcellus A Tryk
    Friday, June 13, 2008 7:24 PM
  • Again, as in the first reply to the original post, please post some code. We're not psychic.  ;-)
    ~~Bonnie [C# MVP]
    Sunday, June 15, 2008 2:46 PM