user5602591
user5602591

Reputation:

Can we call a method in the same class C#

I have the below scenario. I need to call a method which is implemented in the same class. But it is throwing errors. How can we rewrite this?

public class A
{
    Random random = new Random(2);
    string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string randomString = GenerateString(5);

    public string GenerateString(int size)
    {
        char[] chars = new char[size];
        for (int i = 0; i < size; i++)
        {
            chars[i] = Alphabet[random.Next(Alphabet.Length)];
        }
        return new string(chars);
    }
}

Will this scenario works?

Upvotes: 1

Views: 1058

Answers (4)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186833

Another possibility is to turn randomString field into read-only property:

string randomString => GenerateString(5);

all you have to do is to add > after =; further refactoring:

public class A
{
    //TODO: Random(2) for debug only; Random() for real life
    readonly Random random = new Random(2);
    // Alphabet is a constant isn't it? 
    const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    // read-only property; initial value is GenerateString(5)
    //TODO: You may want to capitalize the property - RandomString
    string randomString => GenerateString(5);

    public string GenerateString(int size)
    {
        // Validation
        if (size < 0)
          throw new ArgumentOutOfRangeException(nameof(size), "size must be non-negative");

        // Linq is often compact and readable
        return string.Concat(Enumerable
          .Range(0, size)
          .Select(i => Alphabet[random.Next(Alphabet.Length)]));
    }
}

Edit: As Progman noticed in comments property (and thus GenerateString(5)) will be executed each time when accessed, e.g.

  A a = new A();

  // call randomString 3 times
  string test = string.Join(Environment.NewLine, Enumerable
    .Range(0, 3).Select(i => a.randomString));

  Console.Write(test);

will print out three different values

RE5Z3
BSG80
R10ID

Upvotes: 3

GoldenAge
GoldenAge

Reputation: 3078

You cannot access non-static method GenerateString in static context. You can write your code just like this:

public class A
{
    static Random _random = new Random(2);
    const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string _randomString = GenerateString(5);

    public static string GenerateString(int size)
    {
        char[] chars = new char[size];
        for (int i = 0; i < size; i++)
        {
            chars[i] = Alphabet[random.Next(Alphabet.Length)];
        }
        return new string(chars);
    }
}

Upvotes: 5

vividpk21
vividpk21

Reputation: 384

Change what you have to include a default constructor that you call that from.

public class A
{
    Random random;
    string Alphabet;
    string randomString;

    public A()
    {
        Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        random = new Random(2);
        randomString = GenerateString(5);

    }

    public string GenerateString(int size)
    {
        char[] chars = new char[size];
        for (int i = 0; i < size; i++)
        {
            chars[i] = Alphabet[random.Next(Alphabet.Length)];
        }
        return new string(chars);
    }
}

It's generally considered best practice to initialize all of your member variables inside the constructor rather than when you declare them, it helps with readability and maintainability.

Upvotes: 2

Baltasarq
Baltasarq

Reputation: 12232

You need to learn about constructors. Specifically, in your code you need a constructor initializing randomString. Also, Alphabet is never going to change during execution, so it is a great candidate to become const.

public class A {
    const string Alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public A()
    {
        random = new Random(2);
        randomString = GenerateStrig(5);
    }

    public string GenerateString(int size)
    {
        char[] chars = new char[size];
        for (int i = 0; i < size; i++)
        {
            chars[i] = Alphabet[random.Next(Alphabet.Length)];
        }

        return new string(chars);
    }

    string randomString;
    Random random;
}

Hope this helps.

Upvotes: 6

Related Questions