Jacce
Jacce

Reputation: 172

Store propertyvalues so that it can be used in other classes

In a windows form project I have two classes: createTextDef and CreateTextFile

In the CreateTextFile class I have a constructor that looks like this:

public string Price { get; private set; }
public string Unlock { get; private set; }
public string Stock { get; private set; }

public CreateTextFile(string Price,string Unlock, bool stock)
{
    this.Price = Price;
    this.Unlock = Unlock;
    if (stock)
        Stock = "true";
    else
        Stock = "false";
}

In my createTextDef class I must use the properties that is being created in the CreateTextFile class constructor like this:

public void CreateSingle()
{
string path = Common.PathToTextFile + CreateTextFile.TextName;
string text = File.ReadAllText(Common.templatePath + "Text files\\"+ "File.txt")
    .Replace("10",    CreateTextFile.Unlock)
    .Replace("100", CreateTextFile.Price)
    .Replace("true1", CreateTextFile.Stock)

   File.WriteAlltext(path, text)
}

In the Form1 class I am calling the CreateTextFile:

private void GenerateFile_Text_Click_1(object sender, EventArgs e)
{
    if(single)
        createText = new CreateTextFile(price, unlockLvl, true);
}  

However when I try to access these property values in the createTextDef class I only get empty values. When I call the CreateTextFile constructor in the Form1 class the values is gone when I try to reach it from the createTextDef class. I do not know how to proceed so does anyone know how I can make property values not disappear when I try to call them from another class?

To be more clear: I have 3 values that I should be replacing with other string values in the CreateSingle method but they all is just replaced with empty strings and therefore the textfile im creating is empty. When I call the CreateTextFile constructor I do not put in empty strings.

I would preferably not want to use static members.

Upvotes: 0

Views: 42

Answers (1)

Rufus L
Rufus L

Reputation: 37020

The most likely reason for the empty property values is that you are setting properties of an instance called createText in your GenerateFile_Text_Click_1, but in the CreateSingle method you are reading properties from a different instance called CreateTextFile.

The most likely resolution is to use the createText instance whose properties you assigned values to for your source when reading the information.

Note that I also reversed the order of your replacements since calling .Replace(10) will also replace the first two characters of the string 100, which you were also using as a placeholder.

public void CreateSingle()
{
    string source = Path.Combine(Common.templatePath, "Text files", "File.txt");
    string dest = Path.Combine(Common.PathToTextFile, CreateTextFile.TextName);

    string text = File.ReadAllText(source)
        .Replace("100", createText.Price)
        .Replace("10", createText.Unlock)
        .Replace("true1", createText.Stock)

   File.WriteAlltext(dest, text)
}

Ideally, you might consider using more unique strings to represent your placeholders in the template, like "[Unlock]", "[Price]", and "[Stock]" to avoid the ambiguity in the Replace code. Then that line would look like:

string text = File.ReadAllText(source)
    .Replace("[Unlock]", createText.Unlock)
    .Replace("[Price]", createText.Price)
    .Replace("[Stock]", createText.Stock)

Upvotes: 1

Related Questions