|
I'm trying to establish a good way of doing something before finalising my codegen.
Below is an example:
private void Form1_Load(object sender, EventArgs e)
{
Employee emp = ReallyCoolFuncSingle();
}
public Employee ReallyCoolFuncSingle()
{
Employee e = new Employee();
Person p = CoolFuncSingle();
e = (Employee)p;
e.Age = "100";
return e;
}
public Person CoolFuncSingle()
{
Person p = new Person();
p.PersonName="billy bob";
return p;
}
public Person[] CoolFuncArray()
{
Person[] p = new Person[2];
p[0] = new Person();
p[1] = new Person();
p[0].PersonName = "billy bob";
p[1].PersonName = "frank";
return p;
}
public class Person
{
private string _PersonName;
public string PersonName
{
get { return _PersonName; }
set { _PersonName = value; }
}
public static explicit operator Person(Employee e)
{
Person p = new Person();
p.PersonName = e.PersonName;
return p;
}
}
public class Employee
{
#region person stuff
private string _PersonName;
public string PersonName
{
get { return _PersonName; }
set { _PersonName = value; }
}
#endregion
private string _Age;
public string Age
{
get { return _Age; }
set { _Age = value; }
}
public static explicit operator Employee(Person p)
{
Employee e = new Employee();
e.PersonName = p.PersonName;
return e;
}
}
In essence, I'm trying to modify the Person object returned from some functions and return a super class of some sort - whether actually derived or not is not the issue.
It's a wrap and modify type of pattern.
The code above shows a few attempts.
Basically it all works fine for single objects (managed to get around the explicit operator restrictions), but won't work for arrays.
Any ideas?
Cheers,
Simon
> blog:: brokenkeyboards
> what I think of the OPTIONAL keyword in VB.NET? :: here
> CV :: PDF
> skype :: SimonMStewart
|
|
|
|
|
Although I don't have a clue what you want to accomplish with this weird code (why not just let Employee be a subclass of Person?) I'll try to help you:
It won't work for arrays . The problem is that there is no way to defined operators for arrays of a given type. But you can nevertheless transfer its elements one by one to a new array:
Person[] persons = GetMyPersonsArray();
Emplyee[] employess = new Employee[persons.Length];
for (int i = 0; i < persons.Length; i++)
employess[i] = (Employee)persons[i];
-- modified at 4:23 Sunday 14th May, 2006
|
|
|
|
|
Robert Rohde wrote: The problem is that there is no way to defined operators for arrays of a given type.
But others can reference array parameters ie
public static explicit string[] operator (Employee e){}
PS: I allways forget the syntax, could wrong :p
|
|
|
|
|
Yes, but either the return value or the parameter must exactly match the class which is defined in. Thus in your case either return type or parameter type must be Employee and so something like public static explicit Person[] operator (Employee[] e){} is not possible (neither return type nor parameter type is Employee).
|
|
|
|
|
this is the route I want to go.
More like (pseudo code):
public static explicit operator Person[] Employee(Person p)
{
Employee e = new Employee();
e.PersonName = p.PersonName;
return e;
}
So, the Employee object knows how to cast from Person[] to Employee[].
I could go the route of a static function on Employee, but was hoping for something a little more correct.
Cheers,
Simon
> blog:: brokenkeyboards
> what I think of the OPTIONAL keyword in VB.NET? :: here
> CV :: PDF
> skype :: SimonMStewart
|
|
|
|
|
Hi everyone. I just figured this out and I thought I should put it up somewhere in case someone else has spent hours looking for it like I did. Ok maybe not hours...but still. Anyways heres how you can convert a string to any .NET type that can parse a string.
((IConvertible)"1024").ToType(<br />
Type.GetType("System.Int64"),<br />
System.Globalization.CultureInfo.CurrentCulture )<br />
((IConvertible)"5/5/1985").ToType(<br />
Type.GetType("System.DateTime"),<br />
System.Globalization.CultureInfo.CurrentCulture )<br />
((IConvertible)"1024").ToType(<br />
Type.GetType("System.Double"),<br />
System.Globalization.CultureInfo.CurrentCulture )
returns:
1024
5/5/1985
1024.0
I use Type.GetType instead of typeof() because this way we could pass in a string to get type describing what values to convert to. So a potential function could look like this...
object ConvertString(string str_to_convert, string type_to_convert)<br />
{<br />
return ((IConvertible)str_to_convert).ToType(<br />
Type.GetType(type_to_convert),<br />
System.Globalization.CultureInfo.CurrentCulture )<br />
}
|
|
|
|
|
How about using Convert.ChangeType(str_to_convert, Type.GetType(type_to_convert)) ?
|
|
|
|
|
that works
|
|
|
|
|
Hello everyone,
I would like to read an 16-bit signed tif image in my program.
I used GetPixel(x, y) and the pointer method introduced in "Image processing with dummies" but in both case the program give me the message
"OutOfMemoryException".
Could every one help me to read this image or in a ESRI GRID Raster format.
Regards
Isaac
|
|
|
|
|
I would like to develop a scanner software and make it available as freeware for everyone to download and install. The software including the following functions:
1. Scan to File.
2. Scan to Fax.
3. Scan to Email.
4. Photocopy.
5. Others.
I had created a ComboBox. I want the ComboBox to list down all the scanner drivers installed in user's computer, including the TWAIN drivers. Can anybody tell me how to do that?
|
|
|
|
|
How can I search for a USB PNP ID?
For example, when you run "msinfo32.exe" and then expand "Components/USB", it lists all the USB devices as well as their "PNP Device ID".
I tried using This Code[^]; however, it doesn't list my USB device, although it does work properly by detecting the mouse and some other devices.
One thing I noticed is that all the devices that the code above finds start with "HID" in the msinfo32.exe "PNP Device ID"; however, my device starts with USB. My device id is: USB\VID_0403&PID_6001.
-- modified at 20:26 Saturday 13th May, 2006
|
|
|
|
|
I figured it out by using ManagementObjectSearcher class. Wow! This class is really powerful if you ever need to figure out anything from your hardware.
Here is the code:
<br />
string device = "Win32_USBHub";<br />
ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from " + device);<br />
foreach(ManagementObject mo in searcher.Get())<br />
{<br />
Trace.WriteLine(mo.GetPropertyValue("Name")+" "+ mo.GetPropertyValue("PNPDeviceID"));<br />
}<br />
Note that there are a ton of hardware classes available from here[^].
Each class has several variables that can be accessed using the GetPropertyValue method.
|
|
|
|
|
So I have this class, AdminPaper and it implements, IComparable to sort an array of AdminPapers by a property of AdminPaper.
The implementation looks like this:
public int CompareTo(object obj)
{
AdminPaper paper = (AdminPaper)obj;
if (this.scoreDouble > paper.scoreDouble)
{
return -1;
}
else if (this.scoreDouble < paper.scoreDouble)
{
return +1;
}
else
return 0;
}
So, this code will sort my array list based upon the scoreDouble field. And this works great, I just call it like this, Array.Sort(papers); , where papers is an un-sorted array of AdminPapers.
However, what if I want to sort papers a different way? Right now I only can sort the way I implemented Icomparable in AdminPapers . Is there a way to have two implementations? How do I do this? Right now the only sorting method I have is the CompareTo method above, is there a way to implement another?
/\ |_ E X E GG
-- modified at 20:48 Saturday 13th May, 2006
|
|
|
|
|
see IComparer. You can create several of them or have one that provides some mechanism to control what to sort on.
good luck
"What classes are you using ? You shouldn't call stuff if you have no idea what it does" Christian Graus in the C# forum
led mike
|
|
|
|
|
Here are two options:
1) Implement IComparable in the AdminPaper class and create a static property which determines the comparison mode to use. Inside of your IComparable implementation perform the appropriate comparison based on the property value.
2) Create multiple implementations of IComparer in separate classes (one class per implementation). Have one static property for every IComparer implementation in the AdminPaper class (returning a singleton instance from each static property). Pass the appropriate IComparer implementation as the second argument to the Sort method of Array .
Josh
-- modified at 23:48 Saturday 13th May, 2006
BTW - If you go with option (2) you'd probably want to make the classes that implement IComparer private nested classes within the AdminPaper class, just to avoid namespace pollution.
|
|
|
|
|
i want to insert a new row inside my microsoft access database, i do it, but i need it immediately. how?
|
|
|
|
|
Although this is a question to database forum (if there is), I know a little about it so I'll help...
I personally work with database that doesn't stop to work (there are always a few people that can use the database), you should always take that risk! because if some-one inserts a row while you insert a row, you can both edit the same row in the end.
I'll show you the command line for the last inserted row, and then I'm going to show you the better way.
So this is command to get the last row:
Select Top 1 * From tblTable Order By <id-field> DESC
(ofcourse no case-sensetive in here) tblTable = the table
ID-Field = the ID or some other field that increases by time (eg. Date\Time)
Now this is not good because if two people add a row, at the same time, they will, in the end see the same row, the last row inserted, and not their own.
The better way which I prefer is to save the variables from the insert command, and just do a "Where" statement in the select command.
eg: Select * from tblTable Where <field1> = '{0}' And <field2> = '{1}' etc. etc...
Hope I helped... NaNg
|
|
|
|
|
Hi!
Don't know if I understand what you want, but if you need the value of an AutoValue that Access has assigned to a field in your newly-inserted row, you can look for "@@Identity".
Another way could be to create your IDs on your own - use a GUID and you can be sure that it's unique and you have its value before you even insert the row into the database.
Regards,
mav
--
Black holes are the places where god divided by 0...
|
|
|
|
|
Hey all,
I'm getting into WPF development and need advice about what the best tools are for developing in that space. I've tried VS2005 + "Orcas" extensions (namely "Cider") but it's constantly crashing and my simple little test apps crash for unknown reasons. It seems like every time I modify the XAML, the app crashes until I close VS. So, Orcas is on my blacklist.
XamlPad is really cool, but AFAIK it does not support an associated code-behind for the event handling methods. I want to define behavior as well as layout, but XamlPad doesn't offer that. Please let me know if I'm wrong about this.
So, for those of you who have been developing/playing in WPF (a.k.a. Avalon), what are the best tools that you've found? Please don't tell me that Cider is as good as it currently gets!!
Thanks,
Josh
|
|
|
|
|
|
Thanks a lot! I'll definitely check that out.
Josh
|
|
|
|
|
Hi
Could someone plz help me?
Im a beginner programmer and I hav gone through the article called "Using Events and Delegates in C#" posted by Graham Harrison on the 21 Oct 2005.
I am havin proplems with coding the Main form or GUI.
I dropped a comboBox on my form and used a string array called invoiceIds with the values{"1","2","3","4"} and populated the comboBox in Form_Load() with these array values.
This is my problem : Now I want a MsgBox to pop up when a different invoiceId is selected from the comboBox. The MsgBox must say what the new invoiceId is and also what the previous invoiceId was.
Its pretty much the same as the article, except the article doesnt explain or show much of the MainForm code.
|
|
|
|
|
You don't have to know much about events to use them.
Select the combobox in the form designer and switch the properties window to show events. Doubleclick in the value field for the event you want to use (probably SelectedIndexChanged in this case), and the form designer will create a method stub for you, and hook it up to the event.
---
b { font-weight: normal; }
|
|
|
|
|
Got it right!
thanks alot for the help
|
|
|
|
|
Hi!
I need HELP!
I tired!
I don't know how to delete folders with
all subfolders(!!!) and containing files(!!!) on FTP in C#.
I use:
public void Delete()
{
DelFiles(FileList.SelectedItems[0].Tag as FTPfileInfo);
MessageBox.Show("DONE!");
}
void DelFiles(FTPfileInfo directory)
{
ftplib.ChangeDir(directory.FullName);
FTPdirectory dir = ftplib.ListDirectoryDetail();
foreach (FTPfileInfo fi in dir)
{
ftplib.ChangeDir(fi.Path);
if (fi.FileType == FTPfileInfo.DirectoryEntryTypes.File)
{
rm(fi.Filename);
}
else
{
DelFiles(fi);
}
}
}
It's only delete of files.
I need delete all.
Help me please!!!
Hello from Ukraine!
|
|
|
|