tags:

views:

103

answers:

3

I'm new to C#, so this may be a basic question. What I need to do is put an array such as the one below in a class library and then make a call to it. So I'd want the appropriate picture to appear via the class and this array. I know there's a much simpler way to make certain pictures appear, but this is a requirement for the project. It's an asp.NET website in C#.

string[] PictureArray;

PictureArray = new string[3];
PictureArray[0] = "~/pics/grl.jpg";
PictureArray[1] = "~/pics/pop.jpg";
PictureArray[2] = "~/pics/str.jpg";
PictureArray[3] = "~/pics/unk.jpg";

EDIT (also in comment):
I'm trying to get the picture from the array to show up in an image box upon a button click like this:

protected void Button1_Click(object sender, EventArgs e) { 
   Class1 name = new Class1(); 
   this.Image1.ImageUrl = name.GetPic(); 
}

Obviously just the name.GetPic() isn't going to do anything, it's throwing an error actually.

EDIT2: Sorry for the confusion, I just realized I can edit and comment. I've got the array in the class properly setup, now I need to access it (read above edit).

Answered

string[] pictures = work.GetPictures();
Image1.ImageUrl = pictures[0];

is all that I needed to see, thanks Zyphrax

A: 

I'm new to SO and it seems I can't comment your question so I'll post it as an answer.

Please be explicit when asking a question on a forum. State your problem, what you have tried and what you don't understand, etc. Here you just tell us that you need to show pictures, ok but what is your problem ? Asking good question will bring you answers but this is a professionnal forum and people usually dont bother when you're not explicit when asking questions.

If this is a homework, don't ask how you do the homework, tell us what you have tried and what detail you have problems with. Never ask answer to you're homework, this doesn't prove any professionalism.

Thanks and welcome to SO !

A: 

I can only add to this via an answer.

public class Class1
{
        public string ArrayMethod (string ArrayMethod)
        {

            string[] PictureArray;
            PictureArray = new string[3];
            PictureArray[0] = "~/pics/grl.jpg";
            PictureArray[1] = "~/pics/pop.jpg";
            PictureArray[2] = "~/pics/str.jpg";
            PictureArray[3] = "~/pics/unk.jpg";
        }
}

This is how I've tried putting the array in a class. When it builds I get the error "not all code paths return a value". For a start you could help me get that working. And yes this is for school, and as far as I can tell this is how you learn programming if it's something the teacher didn't cover. I'm not asking you to write my code, I'm just stuck with it.

eddy
You should be able to edit this into your question instead of posting it as an answer. Many people will just look at the question to see if they can answer it, and they might miss this additional information given here in the answer.
sth
Your method ArrayMethod above states that it returns a string. Your method contains no return statement. It also takes a string parameter. I'm not sure what you're using that for. Your array is defined to have a length of 3, yet you are trying to add 4 items to it. You will get an index out of bounds exception.
steve_c
+1  A: 

ok, it seems that you've put some effort into this.
I think you're looking for something like the sample below.

I've added comments to help you understand what's going on.

One of the classes in your class library:

// Define a class called HomeWork, make it accessible to other
// classes in other namespaces
public class HomeWork {

  // Define a method called GetPictures, no incoming arguments,
  // returns a string[]
  public string[] GetPictures() {

    // Create a new instance of a string array
    string[] pictures = new string[4];

    // Fill the string array with a couple of strings
    pictures[0] = "~/pics/grl.jpg";
    pictures[1] = "~/pics/pop.jpg";
    pictures[2] = "~/pics/str.jpg";
    pictures[3] = "~/pics/unk.jpg";

    // Return the string array
    return pictures;
  }
}

How to call the method on that class:

// Create a new instance of the HomeWork class
HomeWork work = new HomeWork();

// Call the GetPictures method
work.GetPictures();

Please ask your teacher to explain it into more detail.
We can't teach you years of programming experience in one SO question :)

EDIT: In response to your second question

Option 1: use the array that was returned by GetPictures:

HomeWork work = new HomeWork();
string[] pictures = work.GetPictures();

Image1.ImageUrl = pictures[0];
Image2.ImageUrl = pictures[1];
Image3.ImageUrl = pictures[2];
// etc..

Option 2: create an overload for GetPictures that accepts an index

public string[] GetPictures() {
    // This method remains unchanged
}

public string[] GetPictures(int index) {
   // Get the string array from GetPictures
   string[] pictures = GetPictures();

   // Return a specific index
   return pictures[index];

   // As you can see, this method might be
   // dangerous to use, because someone could
   // ask for an invalid index causing an
   // IndexOutOfRangeException
}

HomeWork work = new HomeWork();
Image1.ImageUrl = work.GetPictures(0);
Image2.ImageUrl = work.GetPictures(1);
Image3.ImageUrl = work.GetPictures(2);

The samples above are to illustrate how C# works.
It would be inefficient to use it this way in a business application.

Zyphrax
I really appreciate the help, just one more thing. Once I call to the class, how do I specify which picture(string) on the array (0, 1, 2 or 3) will be used? I'd imagine it goes in work.GetPictures(); between the parenthesis, but I'm unsure of the syntax.
eddy
I'm trying to get the picture from the array to show up in an image box upon a button click like this:protected void Button1_Click(object sender, EventArgs e) { Class1 name = new Class1(); this.Image1.ImageUrl = name.GetPic(); }Obviously just the name.GetPic() isn't going to do anything, it's throwing an error actually.
eddy
I'll copy this to my post, it doesn't space the code out.
eddy
The compiler won't allow you to assign an string[] to the ImageUrl property (which is a single string). Imagine, how would your program know which image to show? I've updated my answer to provide a bit more info.
Zyphrax