I am having a bit of trouble with a property.
I am developing a class "Photo" which has properties etc for a single photo
[code=c#]
using System.Drawing;
public Image Imge
{
get
{
try
{
return _Imge;
}
catch
{
Bitmap bm = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bm);
g.Clear(Color.WhiteSmoke);
Pen p = new Pen(Color.Red, 5);
g.DrawLine(p, 0, 0, 100, 100);
g.DrawLine(p, 100, 0, 0, 100);
return bm;
}
}
set {
using (Image _Imge = Image.FromFile(_FileName))
Imge = value;
}
}
[code]
I am trying to write a property that converts a filename to an image.
The problem is the last set statement gives a Cannot implicitly convert type error.
Thanks for any help
You just need to close the code tag by [TEX][/code][/TEX] not [TEX][code][/TEX].
You should have another property holding image file path
public string ImageFilePath
{
get...
set....
}
public Image Imge
{
get
{
try
{
return _Imge;
}
catch
{
Bitmap bm = new Bitmap(100, 100);
Graphics g = Graphics.FromImage(bm);
g.Clear(Color.WhiteSmoke);
Pen p = new Pen(Color.Red, 5);
g.DrawLine(p, 0, 0, 100, 100);
g.DrawLine(p, 100, 0, 0, 100);
return bm;
}
}
set {
using (Image _Imge = Image.FromFile(ImageFilePath))
Imge = value;
}
} Set the ImageFilePath first...
In your set statement, you create a new var of name "_Imge"--was that intended? And, you also call the property setter again with Imge = value --was that intended?
Maybe I've missed something, but I don't understand why you would return _Imge in your getter, but then call the setter again inside the set property, or why you would create, or attempt, what appears to be another instance for _Imge in the setter (and then not even use it).
Thanks for assistance with previous question.
I have two further short class questions.
1.
I have a class called photo which has 3 properties, filename, title, date, and a subclass called pack, which is an ArrayList of the photo objects, with it's own properties and methods.
My question is how do I access the photo properties once the object is in the ArrayList pack?
2. I wish to use other classes with the same project but I am confused with the inheritance issues.
I am not sure how many classes I can create.
I have read a great mass of information, including several books on c#, I know it is my deficiency, but does anyone have a site that might simplify it for me.
Thanks for any replies.
1. Here is a mock photo class:
using System;
using System.Collections;
namespace daniweb
{
public class PhotoStuff
{
private string _fileName;
private string _title;
private DateTime _date;
private ArrayList _photo;
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
public ArrayList Photo
{
get { return _photo; }
set { _photo = value; }
}
public PhotoStuff()
{
_photo = new ArrayList();
}
}
}
Inhering from the class and adding a "Photographer" string property to indicate who took the picture:
public class PersonalPhoto : PhotoStuff
{
private string _photographer;
public string Photographer
{
get { return _photographer; }
set { _photographer = value; }
}
public PersonalPhoto()
: base()
{
}
}
So altogether we have a single file with this:
using System;
using System.Collections;
namespace daniweb
{
public class PhotoStuff
{
private string _fileName;
private string _title;
private DateTime _date;
private ArrayList _photo;
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
public ArrayList Photo
{
get { return _photo; }
set { _photo = value; }
}
public PhotoStuff()
{
_photo = new ArrayList();
}
}
public class PersonalPhoto : PhotoStuff
{
private string _photographer;
public string Photographer
{
get { return _photographer; }
set { _photographer = value; }
}
public PersonalPhoto()
: base()
{
}
}
}
And calling it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace daniweb
{
public partial class PhotoStuffCaller : Form
{
public PhotoStuffCaller()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PersonalPhoto foto = new PersonalPhoto();
foto.Date = DateTime.Now;
foto.FileName = @"C:\something.jpg";
foto.Photographer = "Me!!!";
if (foto.Photo.Count > 0)
this.BackgroundImage = (foto.Photo[0] as Image);
}
}
}Thanks sknake,
I can follow that, what I don't quite get is how do I add a photo with its properties to the arraylist, to store say some 10 photos, and how I access a particular index.
In the class I exposed the array list and accessed it by index:
if (foto.Photo.Count > 0)
this.BackgroundImage = (foto.Photo[0] as Image);
Is that not what you wanted?
Why ArrayList?! Are you fond of casting??
Use generics List<Photo> photos = new List<Photo>(); and in Photo class you may have list of photos and indexer to get them by index or name anything is unique..
Thanks to all who responded I have been studying your answers.
sknake I cannot understand the code.
[c#] #
public PersonalPhoto()
#
: base()
[/code]
What is this base?
Ramy,
I am trying to use ArrayList because I want to use a collection and this one is the only one I ( thought I understood).
I am trying to populate the array within the class, I notice you do this outside it. Also how would you call a method to set a property.
Please disregard previous message, I messed up with the code delimiters, and I only sent a partial message.
I will try again.
sknake I cannot understand part of your code in the PersonalPhoto class
public PersonalPhoto()
: base()
Also I note that you populate your ArrayList outside the class, I have been trying to do it inside the class after loading a file, is this not appropriate?
How do you call a method in your calling routine?
I use an ArrayList because I wanted to use a collection, and
an ArrayList is the most familiar to me.
It was using inheritince, and base() calls the base constructor.
public class PersonalPhoto : PhotoStuff
{
public PersonalPhoto(): base()
In this case base() calls the ctor for PhotoStuff()
I am still wrestling with this class code. Using what sknake sent this is an excerpt from a method in the class.
Now this works fine eg ps[1]= a file name etc
if (Parts.Length > 0)
Title = GetTitle(parentName);
ArrayList ps = new ArrayList();
for (int counter = 0; counter < Parts.Length - 2; counter++)
{
PersonalPhoto P = new PersonalPhoto();
// PhotoStuff P = new PhotoStuff();
P.FileName = Parts[counter];
ps.Add(P.FileName);
P.DateTaken = GetDatePictureTaken(temp);
ps.Add(P.DateTaken);
P.Title = Title[counter];
ps.Add(P.Title);
}
Here's the thing I am trying to load an arraylist of objects with each object having several text properties. Now if I try to do this with this modified code below, I can see each element of the arraylist but not the properties belonging to each object P. I have read many books over the past few weeks and rewritten the code a lot, but I just can't make this work.
Any takers please.
if (Parts.Length > 0)
Title = GetTitle(parentName);
ArrayList ps = new ArrayList();
for (int counter = 0; counter < Parts.Length - 2; counter++)
{
PersonalPhoto P = new PersonalPhoto();
// PhotoStuff P = new PhotoStuff();
P.FileName = Parts[counter];
P.DateTaken = GetDatePictureTaken(temp);
P.Title = Title[counter];
ps.Add(P);
}Here's the thing I am trying to load an arraylist of objects with each object having several text properties. Now if I try to do this with this modified code below, I can see each element of the arraylist but not the properties belonging to each object P. I have read many books over the past few weeks and rewritten the code a lot, but I just can't make this work.
Any takers please.
if (Parts.Length > 0) Title = GetTitle(parentName); ArrayList ps = new ArrayList(); for (int counter = 0; counter < Parts.Length - 2; counter++) { PersonalPhoto P = new PersonalPhoto(); // PhotoStuff P = new PhotoStuff(); P.FileName = Parts[counter]; P.DateTaken = GetDatePictureTaken(temp); P.Title = Title[counter]; ps.Add(P); }
To access the items in your ArrayList ps, you need to cast the item back to type PersonalPhoto:
for (int i=0; i<ps.Count; i++)
{
PersonalPhoto p = (PersonalPhoto)ps[i];
// Now you can access the PersonalPhoto item's properties (eg. p.FileName)
Console.WriteLine(p.FileName);
}
As an alternative, you could use a strong-typed List instead of an ArrayList:
List<PersonalPhoto> ps = new List<PersonalPhoto>();
for (int counter = 0; counter < Parts.Length - 2; counter++)
{
PersonalPhoto P = new PersonalPhoto();
// PhotoStuff P = new PhotoStuff();
P.FileName = Parts[counter];
P.DateTaken = GetDatePictureTaken(temp);
P.Title = Title[counter];
ps.Add(P);
}
// access each List/array element as PersonalPhoto...
foreach (PersonalPhoto p in ps)
{
Console.WriteLine(p.FileName);
Console.WriteLine(p.Title);
// .... etc.
}
// or using index like:
for (int i = 0; i < ps.Count; i++)
{
Console.WriteLine(ps[i].FileName);
Console.WriteLine(ps[i].Title);
}Here is multiple examples of loading collections accessible by index.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
namespace daniweb
{
public class PhotoStuff
{
protected string _fileName;
protected string _title;
protected DateTime _date;
public string FileName
{
get { return _fileName; }
set { _fileName = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
public DateTime Date
{
get { return _date; }
set { _date = value; }
}
public virtual Image GetImage()
{
return Image.FromFile(this.FileName);
}
public PhotoStuff()
{
}
public PhotoStuff(string FileName, string Title, DateTime Date)
: this()
{
this.FileName = FileName;
this.Title = Title;
this.Date = Date;
}
public static PhotoStuff[] LoadFromSomething()
{
List<PhotoStuff> lst = new List<PhotoStuff>();
lst.Add(new PhotoStuff(@"C:\img\iconError.png", "Error Icon", DateTime.Today));
lst.Add(new PhotoStuff(@"C:\img\refresh_small.png", "Small Refresh Icon", DateTime.Today.AddDays(-1)));
return lst.ToArray();
}
}
public class PersonalPhoto : PhotoStuff
{
private string _photographer;
public string Photographer
{
get { return _photographer; }
set { _photographer = value; }
}
public PersonalPhoto()
: base()
{
}
public PersonalPhoto(string FileName, string Title, DateTime Date, string Photographer)
: base(FileName, Title, Date)
{
this.Photographer = Photographer;
}
public static PersonalPhoto[] LoadFromSomething()
{
List<PersonalPhoto> lst = new List<PersonalPhoto>();
lst.Add(new PersonalPhoto(@"C:\img\iconError.png", "Error Icon", DateTime.Today, "The computer"));
lst.Add(new PersonalPhoto(@"C:\img\refresh_small.png", "Small Refresh Icon", DateTime.Today.AddDays(-1), "A camera"));
return lst.ToArray();
}
}
}
Calling it:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace daniweb
{
public partial class PhotoStuffCaller : Form
{
public PhotoStuffCaller()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
//Single instance
PersonalPhoto foto = new PersonalPhoto();
foto.Date = DateTime.Now;
foto.FileName = @"C:\something.jpg";
foto.Photographer = "Me!!!";
}
{
//Single instace
PhotoStuff foto = new PhotoStuff();
foto.Date = DateTime.Now;
foto.FileName = @"C:\something.jpg";
}
{
//Array and list, accessible by index
PersonalPhoto[] personalPhotoArray = PersonalPhoto.LoadFromSomething();
List<PersonalPhoto> personalPhotoList = PersonalPhoto.LoadFromSomething().ToList();
Console.WriteLine(personalPhotoArray[0].FileName);
Console.WriteLine(personalPhotoList[0].Title);
}
{
//Array and list, accessible by index
PhotoStuff[] personalPhotoArray = PhotoStuff.LoadFromSomething();
List<PhotoStuff> personalPhotoList = PhotoStuff.LoadFromSomething().ToList();
Console.WriteLine(personalPhotoArray[0].FileName);
Console.WriteLine(personalPhotoList[0].Title);
}
{
//Loading outside the class
List<PhotoStuff> lst = new List<PhotoStuff>();
for (int i1 = 0; i1 < 10; i1++)
{
string fName = string.Format(@"C:\test{0:F0}.bmp", i1);
string title = Guid.NewGuid().ToString(); //test data
DateTime photoDate = DateTime.Today.AddDays(-1 * i1);
lst.Add(new PhotoStuff(fName, title, photoDate));
}
}
}
}
}