| | |
help with c# classes
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Aug 2007
Posts: 13
Reputation:
Solved Threads: 0
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
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
not
.
You should have another property holding image file path
Set the ImageFilePath first...
You should have another property holding image file path
C# Syntax (Toggle Plain Text)
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; } }
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Jul 2009
Posts: 889
Reputation:
Solved Threads: 140
In your set statement, you create a new var of name "_Imge"--was that intended? And, you also call the property setter again with
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).
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
•
•
Join Date: Aug 2007
Posts: 13
Reputation:
Solved Threads: 0
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.
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:
Inhering from the class and adding a "Photographer" string property to indicate who took the picture:
So altogether we have a single file with this:
And calling it:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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:
C# Syntax (Toggle Plain Text)
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); } } }
In the class I exposed the array list and accessed it by index:
Is that not what you wanted?
C# Syntax (Toggle Plain Text)
if (foto.Photo.Count > 0) this.BackgroundImage = (foto.Photo[0] as Image);
Is that not what you wanted?
Last edited by sknake; Sep 18th, 2009 at 4:23 am.
Why ArrayList?! Are you fond of casting??
Use generics
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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
Join Date: Aug 2007
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2007
Posts: 13
Reputation:
Solved Threads: 0
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
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.
I will try again.
sknake I cannot understand part of your code in the PersonalPhoto class
c# Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- need idea for project using classes and inheritance (C++)
- How do i do chat program using MFC (Microsoft Foundation Classes) and Visual Basic? (Visual Basic 4 / 5 / 6)
- Loading classes from a .jar file (Java)
- Help with Classes (C++)
- Understanding classes (C++)
- Working with objects of different Classes (Java)
- Classes (C++)
- Summer Classes (Geeks' Lounge)
Other Threads in the C# Forum
- Previous Thread: NavigationUI does not get Enabled(WPF Frame Control).
- Next Thread: About infix to postfist and postfix evaluation
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml






