C# Using and sub-items -- different from VB Using

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gbutler
    New Member
    • Mar 2008
    • 2

    C# Using and sub-items -- different from VB Using

    I can't figure out how to do something in C# that can be done in VB (2005 for both). I have C# class for all my utility functions:

    namespace CSharpUtilities
    class Utilities
    public string CSFunction1
    public bool CSFunction2

    I can have a VB application, and add CSharpUtilities as a separate project within the solution. and the Imports statement can be like "Imports CSharpUtilities .Utilities". Then I can use code like this in the VB program:

    MyString = CSFunction1("He llo World")

    or something like that. I don't have to qualify the function call to be

    MyString = Utilities.CSFun ction1("Hello World").

    How do I get the ability to not have to qualify the function call (i.e. not add "Utilities. " to the front of it) in C#?

    C# will allow a "using CSharpUtilities ;" but if I try to do "using CSharpUtilities .Utilities;" to get to where I was in VB, the compiler and IDE reject it. There is no way to be able to use the unqualified call and put

    string MyString = CSFunction1("He llo World");

    in C# that I can figure out. The only way this works is

    string MyString = Utilities.CSFun ction1("Hello World");

    Does anyone know what I need to do to be able to use the unqualified Function call?

    Thanks!
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    In VB you'd have used an Imports statement at the head of your code. C# has a similar statement - using.
    VB
    Code:
    Imports MyUtilities
    C#
    Code:
    Using MyUtilities;

    Comment

    • gbutler
      New Member
      • Mar 2008
      • 2

      #3
      Originally posted by balabaster
      In VB you'd have used an Imports statement at the head of your code. C# has a similar statement - using.
      VB
      Code:
      Imports MyUtilities
      C#
      Code:
      Using MyUtilities;
      Yes, but is there a way in C# to be able to call the functions in another project without prefacing it with "Utilities. " like you can with VB?

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by gbutler
        Yes, but is there a way in C# to be able to call the functions in another project without prefacing it with "Utilities. " like you can with VB?
        I'm unsure - I'll have to investigate and get back to you.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          In VB you used a "module" yes? An unclassified section of code.
          In C# you can have static classes, but you still must refer to the class name first.

          So if you have:
          Code:
          namespace MyNS
          {
             public static class SomeClass
             {
                public int SomeFunc(string str)
                {
                   return 1;
                }
             }
          }
          You can then use in a differnt project:
          Code:
          using MyNS;
          
          public class myotherclass
          {
             public myotherclass()
             {
                int n = SomeClass.SomeFunc("");
             }
          }
          In other words, you can "using" any depth of namespaces, but you must refer to objects directly.

          Comment

          Working...