Erfan
Erfan

Reputation: 71

MessageBox.show() C# problem

see this code:

ChooseGrade ChGrade = new ChooseGrade();
string GrCl = ChGrade.getGrCl().ToString(); // function getGrCl gets "public int[] grad_class=new int[2];" in ChooseGrade Class
MessageBox.Show(GrCl);

But I see in Messagebox this "System.Int32[]" instant container of grade_class Array. Can you Help me?

Upvotes: 2

Views: 706

Answers (7)

MAK
MAK

Reputation: 26586

You need to convert the array into a String before trying to print it out.

One way can be:

var GrCl=ChGrade.getGrCl();
string output=string.Join(",", GrCl.Select(x => x.ToString()).ToArray()); 
MessageBox.Show(output);

Edit: As @spender has pointed out, this won't work in this case since GrCl is an int32[] which does not have the Select method.

As other answers have shown in the meantime, a workaround is to manually iterate over the array and convert and append each element into output.

foreach (var x in GrCl){
   output+=x.ToString();
}

Upvotes: 2

Tadeusz
Tadeusz

Reputation: 6873

GrCl in Show() is converted to String calling ToString () method. As this method of int [] returns string "System.Int32" you see exactly It. You ca to do in such way:

public string IntArrToString (int [] arr)
{
  string intArrStr = "";
  foreach (int number in arr)
    intArrStr += number.ToString() + " ";
  return intArrStr;
}

But It is better to use StringBuilder for such cases :) Then you can call Show like that:

MessageBox.Show (IntArrToString (ChGrade.getGrCl()));

Upvotes: 1

mind.on.warp
mind.on.warp

Reputation: 43

What you're getting is the type name of the object returned by the getGrCl() method. If you haven't overriden the ToString() method, that is the expected behavior.

As I can see, the object returned is an integer array so you cannot alter it's ToString() method and by default it just gives you the type name. If you want to get a string containing the values from this array either traverse the returned array and use a StringBuilder to build your string, or create a separate method in your class to handle this issue.

Hope this helps you. If you still have questions just ask!

Upvotes: 2

spender
spender

Reputation: 120380

var x = ChGrade.getGrCl();
var messageString = string.Join(",", x.Select(i => i.ToString()))
MessageBox.Show(messageString);

Upvotes: 1

alex
alex

Reputation: 3720

Enumerate over the ints and add them to a string which you can then show in the MessageBox:

string message = "";
int myArray[] = ChGrade.getGrCl();

foreach(var num in myArray)
    message += String.Format("{0} ", num);

MseeageBox.Show(message);

Upvotes: 4

Dennis Traub
Dennis Traub

Reputation: 51624

The default ToString()-implementation returns the type. Since the return value of getGrCl() is an array of integer, the ToString()-method returns just this.

Upvotes: 2

eandersson
eandersson

Reputation: 26342

This is because the int you are converting to a String is an Array. You need to either loop through the array, or choose a specific index you want to print.

ChooseGrade ChGrade = new ChooseGrade();
string GrCl = ChGrade.grad_class[0].ToString();
MessageBox.Show(GrCl);

You would need to do something like this:

grad_class[0].ToString()

Upvotes: 1

Related Questions