random method in setter method in C#

c# getter/setter best practices
c# private setter
c# setter
c# getter setter shorthand
c# class setter method
c# property
why we use getter and setter in c#
c# custom setter

I am new to C#. I am trying to figure how to use the random method inside a setter. This is how my code looks right now:

public class star_wars_figures
{
    private string charactor;
    private int bounty;

    public star_wars_figures(string charactor)
    {
        Charactor = charactor;            }

    public string Charactor
    {
        get
        {
            return charactor.ToUpper();
        }
        set
        {
            if (value == "Han Solo" || value == "Leia")
            {
                charactor = value;
            }
            else charactor = "INCORRECT CHARACTOR!!!!";
        }
    }

    public int Bounty
    {
        get
        {
            return bounty;
        }
        set
        {
            Random rnd = new Random();
            bounty = rnd.Next(1, 10);
            bounty = value;
        }
    }
}

In my main I insatiate with the following:

star_wars_figures sw1 = new star_wars_figures("Han Solo");
Console.WriteLine($"Character is: {sw1.Charactor}");
Console.WriteLine($"Money: {sw1.Bounty}"); 

I am trying to generate a random int number in the setter method of the bounty attribute. Its just returning 0.

What have I missed out here?

-thanks


Because you don't set the bounty in object constructor. You use only getter for bounty property. Try to use:

public star_wars_figures(string charactor)
{
    Charactor = charactor;
    Random rnd = new Random();
    bounty = rnd.Next(1, 10);
}

C# Property Examples, A property is a method that gets or sets a value. C# program that uses private setter in property using System; class Example { public Id = new Random(). C# | Random.Next () Method The Next () Method of System.Random class in C# is used to get a random integer number. This method can be overloaded by passing different parameters to it as follows:


"value" (your last assignment) is the value received while calling the setter.

Currently "bounty" is set twice : firstly but your random generator result, and immediately after with "value". Therefore the first value set is lost and not usable anywhere.

You could just remove "bounty = value;" to have it working. Nevertheless, a property setter is not the right place to do what you seem to want, I think you have fundamental misunderstanding of how to work with properties and backing fields.

Beginning Object-Oriented Programming with C#, passCount property method has a set statement block. (In some states, if you had a setter for the odometer property, you might find yourself a 2. Generate a random integer number that falls within the range of 1 to DECKSIZE, inclusively. The NextDouble() Method of System.Random class in C# is used to return a random floating-point number which is greater than or equal to 0.0, and less than 1.0. Syntax: public virtual double NextDouble(); Return Value: This method returns a double-precision floating point number which is greater than or equal to 0.0, and less than 1.0.


The random number generator needs to be initialized once only, as it used the CPU ticks at the time of initialization for the random number seed. Best practice is to include it into a private static field.

Change your code to

public class StarWarsFigures
{
    // Define one instance of the random number generator
    static readonly Random rng = new Random();

    ...
}

Now as far as using it inside a setter, it is bad design, as the purpose of the setter is to set a property to a specified value. If you want to set the property to a random number initially, then do it at the constructor.

    public StarWarsFigures(string character)
    {
        this.Character = character;
        this.Bounty = rng.Next(1, 10);
    }

    public int Bounty
    {
        get { return bounty; }
        set { bounty = value; }
    }

Finally, there are some issues with the character string. I assume you want to store it as upper case only, since the getter converts it as such. It is better to do the conversion once at the setter instead. Additionally, when comparing strings, always use .Equals() instead of == as they mean different things for strings. Finally, you need to convert the set value to upper case also before comparing, or use the StringComparison.OrdinalIgnoreCase option.

    public string Character
    {
        get { return character; }
        set
        {
            if (value.Equals("Han Solo", StringComparison.OrdinalIgnoreCase) 
                || value.Equals("Leia", StringComparison.OrdinalIgnoreCase))
            {
                character = value.ToUpper();
            }
            else
            {
                character = "INCORRECT CHARACTER!!!!";
            }
        }
    }

PS. You might want to fix the typos. Character is the correct spelling, not Charactor.

Lesson 15.1 – Getting random numbers for the game – ScottLilly.com, This game, like many games, needs to get random numbers. Step 3: Decide which method you want to use, and paste the code below into the RandomNumberGenerator class. All lessons: Learn C# by Building a Simple RPG Index It doesn't have a getter and/or setter, which is what would make it a property. Replace getter and setter methods with corresponding properties. The property's name should be the same as its originating methods, but without "Get" and "Set". For example, if the method had a name int GetSize(), then the property should have type int and name Size. Leave all access modifiers the same: private setters should stay private


Java: Help with getters and setters in random number program , Java: Help with getters and setters in random number program This is why I chose to set only the max for the random number generator with the I always were learning using the traditional school method, going to an Exam, studying alot  When new to C#, it may be tempting to just give all properties both getters and setters, which unless they explicitly need to be mutable, is bad practice. “Tell an object what to do, don’t ask it for information and manipulate it yourself”.


property() function in Python, The property() function in Python is used to define properties in the class. Module · Math Module · Statistics Module · Collections Module · Random Module script which defines the person class as having the getter and setter methods. It encapsulates instance attributes and provides a property, same as Java and C#. Returns a random floating-point number between 0.0 and 1.0. using namespace System; // This derived class converts the uniformly distributed random // numbers generated by base.Sample( ) to another distribution. public ref class RandomProportional : Random { // The Sample method generates a


GenFu, using an internal database of values or randomly created data. Use GenFu's static methods to new up new objects for testing, design-time data or If your project uses one-parameter setter methods, you can use GenFu too! Phone Windws Live Writer Winnipeg bower c# compiling jQuery jQuery UI node npm open  To generate random numbers, use Random class. Create an object − Random r = new Random(); Now, use the Next() method to get random numbers in between a range − r.Next(10,50); The following is the complete code − Example. Live Demo