kashif
kashif

Reputation: 3834

how to create a method that returns conditional type

I have a Method as follows in one of my projects that retuns byte[] if my pictureboxstupic contains an image. while I want to convert it in such a way that if there is no picture in pictureboxstupic it should return string = "NULL" else it should return bute[]. how can I do that??

private byte[] GetPic()
{
    using (var stream = new MemoryStream())
    {
        var bmp = new Bitmap(pictureboxstupic.Image);
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        stream.Position = 0;
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        return data;
    }
}

Note: The column in which I am inserting image is as follows

CREATE TABLE [dbo].[Students] 
([Photo] [image] NULL)

and in my current condition I'm inserting the image in my above mentioned columns i.e Students.Photo like this

if (pictureboxstupic.Image == null)
{
    cmd.Parameters.Add("@Photo", SqlDbType.VarChar).Value = "NULL";
}
else
{
cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = GetPic();
}

Upvotes: 0

Views: 316

Answers (6)

kashif
kashif

Reputation: 3834

I solved my problem like this Method:

private object GetPic()
{
    if(pictureboxstupic.Image == null)
    {
        return null;
    }
    using (var stream = new MemoryStream())
    {
        var bmp = new Bitmap(pictureboxstupic.Image);
        bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
        stream.Position = 0;
        var data = new byte[stream.Length];
        stream.Read(data, 0, data.Length);
        return data;

    }
}

usage:

cmd.Parameters.Add("@Photo", SqlDbType.Image).Value = GetPic() ?? DBNull.Value;

Upvotes: -1

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

@empi's answer is the right one, and I'd like to expand on it: Methods and functions should have a tightly defined set of responsibilities. In the case of your GetPic method, it's to get an image's byte stream. That should be the extent of the logic encapsulated in that method.

Displaying the word "NULL" in some error-box is not GetPic's responsibility. It's the responsibility of whomever calls it and is in charge of displaying the image or the error message. That's why I think GetPic should just return null if it's a reasonable condition to receive no image, or perhaps even throw an exception if it's a real error condition that breaks the program flow.

What you shouldn't do in any case is return an object that can be either byte[] or string, since that defeats the whole point of a strongly typed environment, and is a great way to introduce bugs later on. If you must return either a byte[] or a string, you can either use the ref/out params as suggested by others, or create a new class called ImageData, or whatever, that contains both the bytes and the error message, which you can use to retrieve either one or the other.

-- EDIT

After your clarification, I'll still stick by my answer. You shouldn't be doing that. Databases support a NULL value that is not the same as the string "NULL". Rather than inserting this string, you should make sure your column is nullable, and insert the null value - DBNull.Value. Your current code will crash, because the Photo column cannot handle VarChar values.

Upvotes: 3

user35443
user35443

Reputation: 6403

You can make it by three solutions:

1.) Use object as return type

2.) Use 'ref' and 'out' parameter specifiers to return array and make method return type string for returning status message.

3.) Use 'unsafe' code for pointers...

Example:

    public string GetPic(out byte[] ret)
    {
        if (Image == null)
            return "NULL";
        //set ret's value as image bytes
        return "ok";
    }

Upvotes: 1

Mitja Bonca
Mitja Bonca

Reputation: 4546

You can do it like:

        //call the method:
        string str = ""; //variable that will be changed accordingly by returned value
        byte[] image = GetPic();
        if (image.Length == 0)
        {
            //do what ever you want, like string = "NULL":
            str = "NULL";
        }
    //
    //

    private byte[] GetPic()
    {
        PictureBox pictureboxstupic = new PictureBox();
        using (var stream = new MemoryStream())
        {
            var bmp = new Bitmap(pictureboxstupic.Image);
            bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;
            var data = new byte[stream.Length];
            stream.Read(data, 0, data.Length);
            return data;
        }
    }

Upvotes: 0

Baz1nga
Baz1nga

Reputation: 15579

return object instead? Then cast based by first checking the type using the is keyword.

if(returnedType is string) //or use as and then check for NULL
{

}
else   //type is byte[]
{
}

Not sure about your use case and whether it is valid or not, but this can be a solution

Upvotes: 1

empi
empi

Reputation: 15881

You may always return object and cast it to type you expect - but it is not a good idea. It would be better if you just check if pictureboxstupic is empty before calling your method and then execute proper logic (e.g. assigning "NULL").

You may also apply similar semantic to TryParse - that is you may return bool that would answer if pictureboxstupic wasn't empty and return byte array as an out parameter.

Upvotes: 4

Related Questions