943,871 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1044
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 17th, 2009
0

help with c# classes

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
valter is offline Offline
57 posts
since Aug 2007
Sep 17th, 2009
0

Re: help with c# classes

You just need to close the code tag by [/code] not [code].
You should have another property holding image file path

C# Syntax (Toggle Plain Text)
  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...
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Sep 17th, 2009
0

Re: help with c# classes

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
Reputation Points: 341
Solved Threads: 233
Posting Shark
DdoubleD is offline Offline
984 posts
since Jul 2009
Sep 18th, 2009
0

Re: help with c# classes

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
valter is offline Offline
57 posts
since Aug 2007
Sep 18th, 2009
0

Re: help with c# classes

1. Here is a mock photo class:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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:
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 18th, 2009
0

Re: help with c# classes

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
valter is offline Offline
57 posts
since Aug 2007
Sep 18th, 2009
0

Re: help with c# classes

In the class I exposed the array list and accessed it by index:
C# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Sep 18th, 2009
1

Re: help with c# classes

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..
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Sep 19th, 2009
0

Re: help with c# classes

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
valter is offline Offline
57 posts
since Aug 2007
Sep 19th, 2009
0

Re: help with c# classes

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

c# Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
valter is offline Offline
57 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: NavigationUI does not get Enabled(WPF Frame Control).
Next Thread in C# Forum Timeline: About infix to postfist and postfix evaluation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC