help with c# classes

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Aug 2007
Posts: 13
Reputation: valter is an unknown quantity at this point 
Solved Threads: 0
valter valter is offline Offline
Newbie Poster

help with c# classes

 
0
  #1
Sep 17th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: help with c# classes

 
0
  #2
Sep 17th, 2009
You just need to close the code tag by [/code] not [code].
You should have another property holding image file path

  1. public string ImageFilePath
  2. {
  3. get...
  4. set....
  5. }
  6. public Image Imge
  7. {
  8. get
  9. {
  10. try
  11. {
  12. return _Imge;
  13. }
  14. catch
  15. {
  16. Bitmap bm = new Bitmap(100, 100);
  17. Graphics g = Graphics.FromImage(bm);
  18. g.Clear(Color.WhiteSmoke);
  19. Pen p = new Pen(Color.Red, 5);
  20. g.DrawLine(p, 0, 0, 100, 100);
  21. g.DrawLine(p, 100, 0, 0, 100);
  22. return bm;
  23. }
  24. }
  25. set {
  26. using (Image _Imge = Image.FromFile(ImageFilePath))
  27. Imge = value;
  28. }
  29. }
Set the ImageFilePath first...
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 889
Reputation: DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough DdoubleD is a jewel in the rough 
Solved Threads: 140
DdoubleD DdoubleD is offline Offline
Practically a Posting Shark

Re: help with c# classes

 
0
  #3
Sep 17th, 2009
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).
Last edited by DdoubleD; Sep 17th, 2009 at 11:23 am. Reason: added end to last sentence
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 13
Reputation: valter is an unknown quantity at this point 
Solved Threads: 0
valter valter is offline Offline
Newbie Poster

Re: help with c# classes

 
0
  #4
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: help with c# classes

 
0
  #5
Sep 18th, 2009
1. Here is a mock photo class:
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace daniweb
  5. {
  6. public class PhotoStuff
  7. {
  8. private string _fileName;
  9. private string _title;
  10. private DateTime _date;
  11. private ArrayList _photo;
  12.  
  13. public string FileName
  14. {
  15. get { return _fileName; }
  16. set { _fileName = value; }
  17. }
  18. public string Title
  19. {
  20. get { return _title; }
  21. set { _title = value; }
  22. }
  23. public DateTime Date
  24. {
  25. get { return _date; }
  26. set { _date = value; }
  27. }
  28. public ArrayList Photo
  29. {
  30. get { return _photo; }
  31. set { _photo = value; }
  32. }
  33.  
  34. public PhotoStuff()
  35. {
  36. _photo = new ArrayList();
  37. }
  38. }
  39. }

Inhering from the class and adding a "Photographer" string property to indicate who took the picture:
  1. public class PersonalPhoto : PhotoStuff
  2. {
  3. private string _photographer;
  4. public string Photographer
  5. {
  6. get { return _photographer; }
  7. set { _photographer = value; }
  8. }
  9.  
  10. public PersonalPhoto()
  11. : base()
  12. {
  13. }
  14. }

So altogether we have a single file with this:
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace daniweb
  5. {
  6. public class PhotoStuff
  7. {
  8. private string _fileName;
  9. private string _title;
  10. private DateTime _date;
  11. private ArrayList _photo;
  12.  
  13. public string FileName
  14. {
  15. get { return _fileName; }
  16. set { _fileName = value; }
  17. }
  18. public string Title
  19. {
  20. get { return _title; }
  21. set { _title = value; }
  22. }
  23. public DateTime Date
  24. {
  25. get { return _date; }
  26. set { _date = value; }
  27. }
  28. public ArrayList Photo
  29. {
  30. get { return _photo; }
  31. set { _photo = value; }
  32. }
  33.  
  34. public PhotoStuff()
  35. {
  36. _photo = new ArrayList();
  37. }
  38. }
  39. public class PersonalPhoto : PhotoStuff
  40. {
  41. private string _photographer;
  42. public string Photographer
  43. {
  44. get { return _photographer; }
  45. set { _photographer = value; }
  46. }
  47.  
  48. public PersonalPhoto()
  49. : base()
  50. {
  51. }
  52. }
  53. }

And calling it:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace daniweb
  11. {
  12. public partial class PhotoStuffCaller : Form
  13. {
  14. public PhotoStuffCaller()
  15. {
  16. InitializeComponent();
  17. }
  18.  
  19. private void button1_Click(object sender, EventArgs e)
  20. {
  21. PersonalPhoto foto = new PersonalPhoto();
  22. foto.Date = DateTime.Now;
  23. foto.FileName = @"C:\something.jpg";
  24. foto.Photographer = "Me!!!";
  25. if (foto.Photo.Count > 0)
  26. this.BackgroundImage = (foto.Photo[0] as Image);
  27. }
  28. }
  29. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 13
Reputation: valter is an unknown quantity at this point 
Solved Threads: 0
valter valter is offline Offline
Newbie Poster

Re: help with c# classes

 
0
  #6
Sep 18th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,187
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 571
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: help with c# classes

 
0
  #7
Sep 18th, 2009
In the class I exposed the array list and accessed it by index:
  1. if (foto.Photo.Count > 0)
  2. this.BackgroundImage = (foto.Photo[0] as Image);

Is that not what you wanted?
Last edited by sknake; Sep 18th, 2009 at 4:23 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: help with c# classes

 
1
  #8
Sep 18th, 2009
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..
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 13
Reputation: valter is an unknown quantity at this point 
Solved Threads: 0
valter valter is offline Offline
Newbie Poster

Re: help with c# classes

 
0
  #9
Sep 19th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 13
Reputation: valter is an unknown quantity at this point 
Solved Threads: 0
valter valter is offline Offline
Newbie Poster

Re: help with c# classes

 
0
  #10
Sep 19th, 2009
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

  1. public PersonalPhoto()
  2. : 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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC