Landen Brown
Landen Brown

Reputation: 11

Returning object attributes from C# function to Blazor web elements

I am trying to initialize a new object in C#, and then print the attributes of the newly created object into a <p> tag. I am stuck on how I would go about taking the generated object from:

<button type="button" @onclick="@(() => createSword("steel"))">
And being able to use it in my web app. 


@page "/authors"

<h3>Authors</h3>


<button type="button" @onclick="@(() => createSword("steel"))">Click me to generate a new sword</button>

<p> Current Sword: </p> <p id="generatedSword">My Sword Details here</p>

@{ <p> Hello </p>
        }

@code {

    public Object createSword(string x)
    {
        Sword testSword = new Sword(x);
        return testSword;
        
    }
    public class Sword
    {
        //Defining attributes of sword
        public int damage;
        public String statType;
        public int stat;
        public String name;

        //Defining private variables of sword

        public Sword(string typeOfSword)
        {
            if (typeOfSword == "Steel")
            {
                damage = 5;
                name = "Steel Sword";
                statType = "Strength";
                stat = 2;

            }
            if (typeOfSword == "Bone")
            {
                damage = 10;
                name = "Bone Sword";
                statType = "Strength";
                stat = 5;

            }
            if (typeOfSword == "Glowing")
            {
                damage = 20;
                name = "Glowing Sword";
                statType = "Strength";
                stat = 10;

            }

            if (typeOfSword == null)
            {
                damage = 1;
                name = "None";
                statType = "None";
                stat = 0;
            }
        }

    }

}

Would I have to assign it to a global object before doing so? I believe this could be circumvented when I create the player class and I could just print the player data in these fields since the player object would be a unique persistent object. Thoughts?

Upvotes: 1

Views: 513

Answers (1)

MrC aka Shaun Curtis
MrC aka Shaun Curtis

Reputation: 30001

Does this answer your question (it seems too simple)?

I've changed your type of sword to an emun and added the object creator as a static method to the Sword class.

The object created is in the private field generatedSword.

@page "/"
<h3>Authors</h3>


<button type="button" @onclick="() => CreateSword(Sword.SwordType.Steel)">Click me to generate a new sword</button>

@if (generatedSword != null)
{
    <p> Current Sword: @generatedSword.name</p>
}
@{
    <p> Hello </p>
}

@code {
    private Sword generatedSword;

    private void CreateSword(Sword.SwordType typeOfSword)
    => generatedSword = Sword.NewSword(typeOfSword);

    public class Sword
    {
        //Defining attributes of sword
        public int damage;
        public String statType;
        public int stat;
        public String name;

        //Defining private variables of sword

        public Sword(SwordType typeOfSword)
        {
            if (typeOfSword == SwordType.Steel)
            {
                damage = 5;
                name = "Steel Sword";
                statType = "Strength";
                stat = 2;

            }
            if (typeOfSword == SwordType.Bone)
            {
                damage = 10;
                name = "Bone Sword";
                statType = "Strength";
                stat = 5;

            }
            if (typeOfSword == SwordType.Glowing)
            {
                damage = 20;
                name = "Glowing Sword";
                statType = "Strength";
                stat = 10;

            }

            if (typeOfSword == SwordType.None)
            {
                damage = 1;
                name = "None";
                statType = "None";
                stat = 0;
            }
        }

        public enum SwordType {None, Steel, Bone, Glowing};

        public static Sword NewSword(SwordType typeOfSword)
            => new Sword(typeOfSword);

    }

}

Upvotes: 1

Related Questions