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. Global dictionary & partial class scheme - error: cannot be accessed with an instance reference

Global dictionary & partial class scheme - error: cannot be accessed with an instance reference

Scheduled Pinned Locked Moved C#
helpquestion
2 Posts 2 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.
  • R Offline
    R Offline
    RNEELY
    wrote on last edited by
    #1

    I'd like to have a globals class in my app with a global dictionary, but am having trouble getting past the error below. Any ideas or suggestions? using System; using System.Collections.Generic; namespace GlobalDict { public partial class Globals { public static Dictionary<string, int> GlobalInts = new Dictionary<string, int>() { {"one", 1}, {"two", 2}, }; } class Program { public static Globals g = new Globals(); static void Main(string[] args) { int i = g.GlobalInts["two"]; /* Error: Member 'GlobalDict.Globals.GlobalInts' * cannot be accessed with an instance reference; * qualify it with a type name instead */ } } }

    Sincerely, -Ron

    C 1 Reply Last reply
    0
    • R RNEELY

      I'd like to have a globals class in my app with a global dictionary, but am having trouble getting past the error below. Any ideas or suggestions? using System; using System.Collections.Generic; namespace GlobalDict { public partial class Globals { public static Dictionary<string, int> GlobalInts = new Dictionary<string, int>() { {"one", 1}, {"two", 2}, }; } class Program { public static Globals g = new Globals(); static void Main(string[] args) { int i = g.GlobalInts["two"]; /* Error: Member 'GlobalDict.Globals.GlobalInts' * cannot be accessed with an instance reference; * qualify it with a type name instead */ } } }

      Sincerely, -Ron

      C Offline
      C Offline
      carbon_golem
      wrote on last edited by
      #2

      Aside from warning you about using globals AND public members, you can't use an instance. Try this syntax: Globals.GlobalInts["two"]; If you remove the static modifier, you'll be able to use the instance reference. Scott P

      “It is practically impossible to teach good programming to students that have had a prior exposure to BASIC: as potential programmers they are mentally mutilated beyond hope of regeneration.” -Edsger Dijkstra

      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