0
internal double[] fillingArray()
{
    a = 1.5;
    b = .5;
    
    for (i = 0; i<10; i++)
    {
        c = a * b;
        a = a + 1;
        b = b + 1;
        arrayInt[i] = c;  
    }
    
    return arrayInt;
}

I wanted to call a method fillingArray() which stores array variables and display those variables via displayAll() main method. When arrayInt is returned, console displays system.Double but not the values inside the array.

6
  • Can you share the code of console display? Commented Jul 3, 2020 at 17:18
  • There doesn't seem to be any point returning arrayInt (it's not full of ints either!) becaus eit is clearly declared at class level. Perhaps you should pass it into this method too Commented Jul 3, 2020 at 17:20
  • as Caius mentioned below, the writeLine method converts and displays to string. However, here this being an array, it is returning the type of array. Hence I would loop the writeLine in another method for each return from fillingArray. Commented Jul 3, 2020 at 17:55
  • I would break the code and paste due to limitation in the comments length Commented Jul 3, 2020 at 17:56
  • class accessingMethods { static void Main(String[] args) { //accessing methods from methodsRevision class Console.WriteLine("\nInternal fillingArray Method's value in accessingMethods class: {0}", mr.fillingArray()); Console.WriteLine("\nInternal Protected intToStringConversion method's value in accessingMethods class: {0}", mr.intToStringConversionIpsMethod()); Console.WriteLine("\nUnable to access Private, Private Protected and Protected Methods"); //END } } } Commented Jul 3, 2020 at 17:58

3 Answers 3

2

You need to parse each value like with the filling function:

static void Test()
{
  Display(CreateFilledArray());
}

static internal void Display(double[] array)
{
  foreach ( var value in array )
    Console.WriteLine(value);
}

static internal double[] CreateFilledArray()
{
  var result = new double[10];
  double a = 1.5;
  double b = .5;
  for ( int index = 0; index < 10; index++ )
  {
    result[index] = a * b;
    a = a + 1;
    b = b + 1;
  }
  return result;
}

Output

0,75
3,75
8,75
15,75
24,75
35,75
48,75
63,75
80,75
99,75
Sign up to request clarification or add additional context in comments.

Comments

1

I get the feeling that you're doing something like this:

Console.WriteLine(arrayInt);

And expecting it to show a representation of all the numbers in the array. C# doesn't do that; it will just print out the type of the array, which is System.Double[]

what is actually happening here is that WriteLine can only write "sensible looking things" it if it's something it can specifically handle. WriteLine can handle lots of different things:

Console.WriteLine(1); //handles numbers
Console.WriteLine('1'); //handles characters
Console.WriteLine("1"); //handles strings

WriteLine cannot handle arrays; they get passed to the version of WriteLine that handles Objects, and the only thing that that version of WriteLine does is call ToString() on the object that is passed in and then print the string result that ToString returns. When we call ToString on an array, it just returns the kind of array it is, not the values in the array

This is why you see "System.Double[]" appear in the console

Youll have to loop over the array printing the values out one by one (there are other tricks you can do, but they're more advanced c# than you know currently and it would not be wise to recommend them if this is an academic assignment)

I can see you know how to loop, so I'll leave the implementing of it up to you

1 Comment

thank you, that makes sense. I was assuming that when I called a method, the calling method would also iterate along called method.
0

I modified the method to string and returned a string value concatenating values of array -- this worked

internal string fillingArray()
        {
            a = 1.5;
            b = .5;
            for (i = 0; i<10; i++)
            {
                c = a * b;
                a = a + 1;
                b = b + 1;
                arrayInt[i] = c;
                //astr array string
                astr = astr + arrayInt[i].ToString() + " ";
            }
            return astr;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.