0

Is it possible to reference a class within the same class... I mean in the following code, I have to get the attribute value from a saved file, that's what I want to return.

public class MyClass
{
    private string attr1 = "atrribute 1";   
    public string Attr1
    {
        get
        {
            //In the line below, I have to reference the type of the class "MyClass"

            if (MyFileStorage.ReadSharedData<MyClass>("filename.xml").prop1.Equals(String.Empty))
               return attr1;
            else
               return MyFileStorage.ReadSharedData<MyClass>("filename.xml").prop1;
        }
        set
        {
            MyFileStorage.WriteSharedData("filename.xml", value);
        }
    }

    private string attr2 = "atrribute 2";
    public string Attr2
    {
        get
        {
            return attr2
        }
        set
        {
            attr2 = value;
        }
    }
}

USAGE

public static MyClass myClass;
Debug.WriteLine(myClass.Attr1); //output: attribute 1
myClass.Attr1 = "xyz";
Debug.WriteLine(myClass.Attr1); //output: xyz (should be able to read this value from the Storage file)

However, there's an Unknown error. It is not possible and the app is stuck!

Could anyone tell me how is it possible to read a property value from a storage file (which has MUTEX implemented) and it's a shared file among two projects and return.

ReadSharedData implementation: stackoverflow.com/questions/21596564/

wafers
  • 1,149
  • 4
  • 14
  • 34
  • 2
    do you mean "this"? [more Info](http://msdn.microsoft.com/en-us/library/dk1507sz.aspx) – Daniel Budick Nov 19 '14 at 14:46
  • What do you mean by unknown error exactly? – wonko realtime Nov 19 '14 at 14:46
  • Can you show the relevant parts of MyFileStorage.ReadSharedData ? – wonko realtime Nov 19 '14 at 14:47
  • The more common problem is to reference the outer class from an inner class.. – TaW Nov 19 '14 at 14:47
  • As a side note using getters and setters to read/write to files is probably not a good design. A better design would be to load the entire class to a file and save it at some point. – D Stanley Nov 19 '14 at 14:47
  • @wonkorealtime Unknown eroor: `A first chance exception of type 'System.IO.IsolatedStorage.IsolatedStorageException' occurred in mscorlib.ni.dll` – wafers Nov 19 '14 at 14:48
  • @DanielBudick sing "this" give this error : `Operator '<' cannot be applied to operands of type 'method group'` – wafers Nov 19 '14 at 14:50
  • @DStanley yes, that's what I can do, however, in that case I need to make another wrapper over to "MyClass". If that's better practice, I can do it! – wafers Nov 19 '14 at 14:52
  • @wafers Not necessarily - you can still do it within the `MyClass` class, but do it in other methods (not within the property accessors). – D Stanley Nov 19 '14 at 14:55
  • How is ReadSharedData implemented? Is it something like http://stackoverflow.com/questions/21596564/deserializing-json-c-sharp-first-chance-exception-of-type-newtonsoft ? – wonko realtime Nov 19 '14 at 14:57
  • @wonkorealtime ReadSharedData is implemeted like: `public static T ReadSharedData(string fileName) where T : class, new(){}`... yes exactly, as you have shared the link. – wafers Nov 19 '14 at 14:59
  • Did you check the setter? Does it work when you first clear the file filename.xml? Can you step into ReadSharedData? Is there additional info in the exception object? Did you check file system permissions? How does the body of the func look like? – wonko realtime Nov 19 '14 at 15:01
  • @wonkorealtime The problem is in the ReadSharedData method... on this line `var fileStream = storage.OpenFile(fileName, FileMode.Open, FileAccess.ReadWrite);` and the error is: `Operation not permitted on IsolatedStorageFileStream` something like that. Don't know what to do whith that method. – wafers Nov 19 '14 at 16:43
  • Maybe it's a locking problem as indicated here : http://stackoverflow.com/questions/8415979/operation-not-permitted-on-isolatedstoragefilestream-error – wonko realtime Nov 19 '14 at 16:44
  • @wonkorealtime I tried out all these things, the exception disappears partially, but the data is not being fetched from the storage file. – wafers Nov 19 '14 at 17:03

1 Answers1

0

Yes, it's physically possible - although I would not recommend what you are trying to do.
Either make attr1 static, so that they share the same variable:

private static string attr1 = "atrribute 1";
or always return the file, regardless of whether attr1 has been set or not:
get  {  return MyFileStorage.ReadSharedData<MyClass>("filename.xml").prop1; }
Bear in mind, none of the above is good practise.
z0mbi3
  • 336
  • 4
  • 14
  • OP does not need to make `attr1` static - he is calling `myClass.Attr1` - ie, he is accessing an instance property. I believe OP - despite wording of his question - is asking about the `this` operator. – Dan Nov 22 '14 at 19:06
  • @DP: It seems that wafers contradicted himself in the opening paragraph, so I guessed the self-reference is not really what he's after. He seems to inquire (although not explicitly) about a static getter that's shared across all instances of this class. But I might be wrong. – z0mbi3 Nov 22 '14 at 19:15
  • his example explicitly states otherwise – Dan Nov 22 '14 at 19:19
  • You're right. He explicitly asks for the "this" reference. Then he goes on and elaborate on what he means by his question, which is open for different interpretations. – z0mbi3 Nov 23 '14 at 12:54
  • @z0mbi3 thanks for the answer. The solution partially worked. There's a problem, that the property is calling itself recursively when I was trying to get it's value. So I did set another static instance of the class where I'm writing and reading. I will put my solution here as well – wafers Nov 25 '14 at 09:15