2 Replies - 184 Views - Last Post: 20 July 2013 - 02:44 PM Rate Topic: -----

#1 GUYGLK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-July 13

non-static field...error

Posted 20 July 2013 - 11:40 AM

hi

i ran into a problem with this code,
compile error says:
An object reference is required for the non-static field...

as the code shows, all paramseters (out) are strings, but for some reason only 'type' rises the complile error.
i want the RetreveSavedOption to be static so that no instance is required to invoke it.
it's worth mentioning that no such error was fired until i added the 'type' out parameter, and further more, when i tried adding another out param, just to see if it will give me another error - it did not!

the code (that errors):
    public class OptionSetTo
    {
        //public bool _isChecked {get; set;}
        public string OptionName {get; set;}
        public string OptionValue { get; set; }
        public string OptionType { get; set; }
        
        public OptionSetTo(string elementType, string optionName, string setValue)
        {
            OptionName = optionName;
            OptionValue = setValue;
            OptionType = elementType.GetType().ToString();
        }

        public static void RetreveSavedOption(OptionSetTo Option, out string name, out string value, out string type)
        {
            name = Option.OptionName;
            value = Option.OptionValue;
            type = OptionType;         // <------ error here
        }
    }




some where in the code i'm using this code like so (no error here):
OptionSetTo.RetreveSavedOption(option, out OptName, out OptValue, out OptType);


OptionSetTo ---------> public class
RetreveSavedOption --> public static function (within OptionSetTo)

i'm stuck.

Is This A Good Question/Topic? 0
  • +

Replies To: non-static field...error

#2 Witchking  Icon User is offline

  • D.I.C Head

Reputation: 68
  • View blog
  • Posts: 186
  • Joined: 17-February 13

Re: non-static field...error

Posted 20 July 2013 - 11:41 AM

A static method can only access other static class members. OptionType is not static, so static void RetreveSavedOption can't access it.

You can access the public OptionType property of the Option object like you did with the other properties
type = Option.OptionType;
Like the error says, you need an object reference to access instance members.


I don't see why you would use a static method in this way though, when you can just get the properties of the OptionSetTo object where you need them.
string name = Option.OptionName;
string value = Option.OptionValue;
string type = Option.OptionType;
instead of
string name;
string value;
string type;

OptionSetTo.RetreveSavedOption(Option, out name, out value, out type);

This post has been edited by Witchking: 20 July 2013 - 11:50 AM

Was This Post Helpful? 1
  • +
  • -

#3 GUYGLK  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 19-July 13

Re: non-static field...error

Posted 20 July 2013 - 02:44 PM

you're right
all i needed to to was:
type = Option.OptionType;


that solved it.
thank u.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1