Results 1 to 3 of 3

Thread: [RESOLVED] Cannot access function from class

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Resolved [RESOLVED] Cannot access function from class

    Hi i am trying to make my own little config reader for my programs I got most of it working but i am haveing troble trying to return a list from a class, evey time i type the class name the list function does not show up in the list eveything else works fine, I left my class code here so maybe someone can see what i am doing wrong thanks.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace TestConfig{
    
        public class MyConfig
        {
            private string _Filename;
            private static Dictionary<string, string> Lines = new Dictionary<string, string>();
    
            public MyConfig(string localname)
            {
                this._Filename = localname;
                string sLine;
                int cnt = 0;
                //Check if file is here
    
                if (!File.Exists(localname))
                {
                    throw new FileNotFoundException("File Not Found:\n" + this._Filename);
                }
                else
                {
                    using (StreamReader sr = new StreamReader(this._Filename))
                    {
                        while (!sr.EndOfStream)
                        {
                            sLine = sr.ReadLine().Trim();
                            //Check for comments
                            if (sLine.StartsWith("//"))
                            {
                                cnt++;
                                Lines.Add(cnt.ToString(), sLine);
                            }
                            else
                            {
                                //Get position of space
                                int spos = sLine.IndexOf(" ");
                                //Check for space position
                                if (spos != -1)
                                {
                                    //Extract key
                                    string sKey = sLine.Substring(0, spos).ToLower();
                                    //Extract value
                                    string sValue = sLine.Substring(spos + 1, sLine.Length - spos - 1);
                                    //Add to the dir
                                    Lines.Add(sKey, sValue);
                                }
                            }
                        }
                    }
                }
            }
    
            public static List<string> Keys()
            {
                List<string> items = new List<string>();
                items.AddRange(Lines.Keys);
                return items;
            }
    
            public string ReadValue(string keyname)
            {
                try
                {
                    return Lines[keyname];
                }
                catch
                {
                    return "";
                }
            }
        }
    }

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    Re: Cannot access function from class

    You made it "static". If you wanted it such, you can access it by typing MyConfig.Keys() instead of a specific instance. (Static methods exist on the class, not on any specific instance.)
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  3. #3

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Cannot access function from class

    k Thanks chnage it now to public and it works was not sure about that static thing like, thanks for the help. was driveing me nuts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width