Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Need help with global sttrings

Need help with global sttrings

Scheduled Pinned Locked Moved C#
csharphelpquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member_3130247
    wrote on last edited by
    #1

    Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)

    R L 2 Replies Last reply
    0
    • M Member_3130247

      Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      Make the strings static members of a class (eg: StringHolder) and refer to them from any class in the following manner:

      public class StringHolder
      {
      public static readonly string SomeString = "hello";
      }

      public class ReferringClass
      {
      void someMethod()
      {
      // foo gets reversed version of SomeString
      string foo = StringHolder.SomeString.Reverse();
      }

      /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • M Member_3130247

        Can anybody shed some light. I'm a bit confused. Still a noob at C#. I read that Global variables are not supported in C# yet it seems to work for. They recommend you use struct instead but I can't seem to get it to work for me. I'm having a hard time understanding to program a string that can be accessed by any function without having to pass the String contents to the function. Basiccaly I need to access some strings globally. Any suggestions? :^)

        L Offline
        L Offline
        Larantz
        wrote on last edited by
        #3

        There are global variables in C#. Just create them outside the functions. For instance:

        public class Circle
        {
        double radius = 0.0;
        double pi = 3.14;

        public Circle(double Radius)
        {
        radius = Radius;
        }

        public double Area()
        {
        //use global variable.
        return pi*Math.Pow(radius, 2);
        }
        }

        Best regards! Larantz

        for those about to code, we salute you
        http://www.tellus-software.com

        G M 2 Replies Last reply
        0
        • L Larantz

          There are global variables in C#. Just create them outside the functions. For instance:

          public class Circle
          {
          double radius = 0.0;
          double pi = 3.14;

          public Circle(double Radius)
          {
          radius = Radius;
          }

          public double Area()
          {
          //use global variable.
          return pi*Math.Pow(radius, 2);
          }
          }

          Best regards! Larantz

          for those about to code, we salute you
          http://www.tellus-software.com

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Larantz wrote:

          There are global variables in C#. Just create them outside the functions.

          They are not global. They are members of the class.

          --- single minded; short sighted; long gone;

          1 Reply Last reply
          0
          • L Larantz

            There are global variables in C#. Just create them outside the functions. For instance:

            public class Circle
            {
            double radius = 0.0;
            double pi = 3.14;

            public Circle(double Radius)
            {
            radius = Radius;
            }

            public double Area()
            {
            //use global variable.
            return pi*Math.Pow(radius, 2);
            }
            }

            Best regards! Larantz

            for those about to code, we salute you
            http://www.tellus-software.com

            M Offline
            M Offline
            Member_3130247
            wrote on last edited by
            #5

            Yes I know that works. I been programming that way all along but I just upgraded to Vista and downloaded the latest C# version. One of the note I read claimed it does not support it so I'm weary programming that way in xase it sops working in the future. See this link http://tinyurl.com/29o64p http://tinyurl.com/29o64p[^]

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups