newbie: How do I set the return type for this method?

D

deko

What I want to do is call the static method in the below class.

In pseudo code it might look something like this:

If (!GetImageFromDatabase.ExtractImage(strFileName,
strPathToImageFile))
{
// do something
}

In other words, if the method returns false, execute some code.

How do I set the return type for this method?
How do I call it from another class?

Thanks in advance.

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace ImageSwitcher
{
class GetImageFromDatabase
{
public static bool ExtractImage(string strFileName,
string strPathToImageFile)
{
OleDbConnection cnxOle = new OleDbConnection();
OleDbCommand cmdOle = new OleDbCommand();
MemoryStream ms = new MemoryStream();
Try
//basically, I want to turn error handling off
//is this how to do it?
{
cnxOle.ConnectionString =
("Provider=Microsoft.Jet.OLEDB.4.0; Data Source="
+ Application.StartupPath + @"\SomeImages.mdb");
string strSql = "SELECT Photo FROM Photos WHERE FileName = "
+ '\u0022' +
strFileName + '\u0022';
cmdOle = new OleDbCommand(strSql, cnxOle);
int offset = 78;
cnxOle.Open();
byte[] img = (byte[])cmdOle.ExecuteScalar();
ms.Write(img, offset, img.Length - offset);
Bitmap bmp = null;
bmp = new Bitmap(ms);
bmp.Save(strPathToImageFile, ImageFormat.Bmp);;
}
finally
{
ms.Close();
cnxOle.Close();

//=====PROBLEM================================================
//Error: Cannot assign to 'ExtractImage' because it is a
'method group'
//=============================================================

ExtractImage = File.Exists(strPathToImageFile);

// so if there's no file, something went wrong
}
} //end method ExtractImage
} //end class GetImageFromDatabase
} //end namespace ImageSwitcher
 
J

Jon Skeet [C# MVP]

deko said:
What I want to do is call the static method in the below class.

In pseudo code it might look something like this:

If (!GetImageFromDatabase.ExtractImage(strFileName,
strPathToImageFile))
{
// do something
}

In other words, if the method returns false, execute some code.

How do I set the return type for this method?

You've already set the return *type* for the method in the declaration.
I think what you're missing is the "return" statement. Just use:

return false;

or

return true;

depending on what you want to return. Note that you can't return from a
finally block.

By the way, I'd suggest the use of "using" blocks to automatically
close your database connection and stream.
 
D

deko

return false;
or

return true;

10-4 - I will use this instead of ClassName = File.Exists(...)
depending on what you want to return. Note that you can't return from a
finally block.

theses narrow scopes are driving me nuts...
By the way, I'd suggest the use of "using" blocks to automatically
close your database connection and stream.

Can you please elaborate? What's a using block? How do I use it?

Thanks for the help.
 
J

Jon Skeet [C# MVP]

deko said:
10-4 - I will use this instead of ClassName = File.Exists(...)


theses narrow scopes are driving me nuts...

I'm not sure what you mean by "narrow scopes" in this case. It's a
control flow issue, not really scoping.

The same is true in VB.NET:

<quote>
It is invalid to explicitly transfer execution into a Finally block; it
is also invalid to transfer execution out of a Finally block except
through an exception.
Can you please elaborate? What's a using block? How do I use it?

I don't have time to go into it in detail now, but basically you do:

using (Stream stream = HoweverYouGetAStream())
{
// Do stuff with the stream
}

and Dispose is automatically called at the end. It automatically adds a
try/finally block for you, effectively.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top