|
Vodstok wrote: I have not ever looked at desing patterns.
You might want to look out for a book on the subject. There are books on design patterns in C#, or there are generic books that apply to any language. It would seem to me that you have figured out, or almost figured out, some on your own. Reading up on the subject will help get you a better understanding, and you can short circuit some of the trial and error of getting the thing right in the first place. It will also give you some good ideas of how to proceed in areas where perhaps you are still figuring things our, or have a solution that just does not feel right.
Once you have studied the basic patterns you can migrate on to some of the more interesting (in my opinion) patterns as given in Martin Fowler's book Patterns of Enterprise Application Architecture (IIRC). I wouldn't jump in to that book straight away though, it's a reference book for the most part, you'd need to understand the basics to get the best out of it.
Good luck.
|
|
|
|
|
Thank you again, it looks like i have some reading to do
______________________
Oh Hamburgers!
|
|
|
|
|
This is 50% curiousity, 50% because I might want to do this (so if this is a 'DON'T DO THIS EVER' type thing, by all means tell me) but I don't even know if its possible.
Say you had something like this:
class Book;
class CheckedOutBook : Book;
class Library
..void CheckOut(ref Book b, Person p);
Is it possible for CheckOut() to transform the book input into it so it is now a CheckedOutBook?
Obviously, you can return a new instance of CheckedOutBook, but I'm specifically talking about transforming the reference - my memories of C++ are that with pointers it should certainly be possible, but is it in C#?
To be clear I'm thinking of something like this:
Book b = new Book("Design Patterns");
(new Library()).CheckOut(b, new Person("Frank"));
Diagnostics.Assert(b is Book);
Diagnostics.Assert(b is CheckedOutBook);
|
|
|
|
|
Togakangaroo wrote: This is 50% curiousity, 50% because I might want to do this (so if this is a 'DON'T DO THIS EVER' type thing, by all means tell me) but I don't even know if its possible.
Code in the base class can cast itself to a derived class IF (AND ONLY IF) the object is actually an instance of the derived class in the first place.
HOWEVER, (and you're way ahead of me), this is a "DON'T DO THIS EVER" type of thing. The base class should have no knowledge of its derived classes. It is the responsibility of the derived class to know about its base.
If a base class uses information about derived classes then you lock the base class effectively. The base is no longer open to extension by other new derived classes at a later date. Any new derived class would potentially require changes in the base. This is bad.
Togakangaroo wrote: Is it possible for CheckOut() to transform the book input into it so it is now a CheckedOutBook?
This is a different question. CheckOut is a method on Library which is not in the same class heirarchy as Book and CheckedOutBook .
You have a ref Book b in the method signature, so I suppose you could REPLACE b with a CheckedOutBook but not transform the existing one. Also I consider using ref and out to be poor practice. In an OO system there are many better ways to accomplish the same thing. I see that there is no return value for the method so there really is no reason at all to be using ref as you can do that through the return value.
Also, the implication of CheckedOutBook is that you are assigning state (whether the book is checked out or not) to the class. State should be a property of the class, not the reason d'etre of the class.
I hope this helps.
|
|
|
|
|
Good point about not having a base class cast itself - As you pointed out, I guess in what I was saying CheckOut() would be what is doing the casting and really the only reason I would want to do this would be for fear of someone referring to the old variable later on in the scope. Obviously there are much better ways of preventing this. So let's move this entirely into the "I'm just curious" column.
|
|
|
|
|
I agree with Colin for the most part. It seems as though you are looking to use the class itself as a way to represent some information about the instance. Although as you mentioned this can work in C++, C# works very differently, and the type of an object really represents two things: first of all, the actual type of the instance, and secondly (in the case of base classes) a common interface (since subclassing is really just a way of implementing the base class's interface, from the perspective of someone using the object).
Anyhow, if you were to cast the instance of ref Book b to a CheckedOutBook inside CheckOut(), this would actually only apply to the local representation of b, and even though it's marked as ref, would not affect the object passed into it, namely because that object in the calling method already has an interpretation (type), and ref parameters allow an object to be changed, not how it is interpreted (i.e. its local apparent type).
As far as it being a "DON'T DO THIS EVER" thing, I think you'll quickly realize that it won't work, so it's not so much a warning as there's just no point in doing it.
Now that said, you might actually be able to do something like what you want by creating a new object of type CheckedOutBook, setting it as somehow equivalent to the passed-in Book, and then setting the ref b parameter to equal the new instance (since it is still a Book). The key is that you will have to create a new object, and the old one will still be floating around so long as it is used (elsewhere). However you appear to be aware of this scenario.
The short answer is no, you cannot "transform" a reference to be some type other than how it was created. Not in C#.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Hi there.
I want to add a button next to the Window Minimize button. (sth like this[^])
How can I do it ?
Thanks in advance .
|
|
|
|
|
Have a look here[^] - Vista compatible too!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Here is one article that I wrote on the subject: Add Transparent Menus and XP Titlebar Buttons to your application[^]
Regards,
Thomas Stockwell
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Visit my Blog
|
|
|
|
|
Is it possible to do this?
I can do it with a List<> but with an array it won't compile - 'Indentifier expected' where the open square bracket is.
Doesn't work:
public static implicit operator Range(DateTime[] dateTimeArray)
{
if (dateTimeArray.Length == 2)
return new Range(dateTimeArray[0], dateTimeArray[1]);
throw new ArgumentException("Invalid number of arguments");
}
Does work:
public static implicit operator Range (List<DateTime> dateTimeList)
{
if (dateTimeList.Count == 2)
return new Range(dateTimeList[0], dateTimeList[1]);
throw new ArgumentException("Invalid number of arguments");
}
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Wierd - the damn thing started compiling and working just after posting - bizzare! The CP touch
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Hmm, cool. Implicit operators are neat/weird.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
The docs for Enum says (paraphrasing) "Enumerations have an underlying type used for storage, which by default is int"
So I have:
using System;
namespace Employee_Example1
{
public enum EmployeeType
{
FullTime = 0,
PartTime = 1,
Contract = 2,
Other = 3
}
public class Employee_Base
{
public int iEmpId = 0;
public int iEmployeeType = EmployeeType.FullTime;
public double dSalary = 0.00;
public string sLastName = "";
public string sFirstName = "";
public Employee_Base()
{
}
}
}
So why am I getting the error "Cannot implicitly convert type 'Employee_Example1.EmployeeType' to 'int'"
I have also tried "public enum EmployeeType : int", and it still won't compile. However, this works:
public int iEmployeeType = (int)EmployeeType.FullTime;
The question is, if the enum's underlying type is int, why do I need to cast?
Everything makes sense in someone's mind
|
|
|
|
|
There's unfortunately no implicit conversion from enum to int, although it should be possible without problems. I guess it's for the sake of type safety, C# is a bit restrictive here.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
As Greeeg said above, there is no implicit conversion for a enum into an int. All conversions need to be done explicitly.
Just a question/though about your base class: why are you storing the enum as an int? I think for program clarity and maintainability, storing EmployeeType as the enum type would be the best way to go (and it will be more clear if your going to have an object out of the base).
|
|
|
|
|
From the enum docs[^]:
"The underlying type specifies how much storage is
allocated for each enumerator. However, an explicit
cast is needed to convert from enum type to an integral
type."
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
KMAROIS wrote: which by default is int
It can be other types including strings so explicit casting is the only logical way - implicit would create a whole bunch of problems!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Do you know how to create enums based on strings? That would be very useful to me.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
Do you mean putting strings rather than int ? AFAIK, you can use attributes for the enum and specify the string value there, use reflection to read the attribute and it's value. I have seen some good articles here written by PiebelConsult and Scott dorman.
|
|
|
|
|
Navaneeth is correct in his response - I was wrong, you can't use strings directly . I knew I'd used them before but forgot that I'd used attributes to do it. There's many examples around of different methods of achieving a string enum. An interesting one is here[^].
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Expect everything to be hard and then enjoy the things that come easy. (code-frog)
|
|
|
|
|
Lol no worries. The attribute trick might be worth it, although with reflection you can get the name of an enum constant (of course these are subject to C# naming rules). That library looks interesting. Thanks to both of you for the responses.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
I want to drag and drop custom control on the form this control had been inherited from using System.Windows.Forms.UserControl
hint:
1) an object of this control will be created at runtime by reflection then will be added to the form collection(System.Windows.Forms.Form.Controls).
2) the form_MouseClick EventHandler is used to create the control on the form
My problem
how can i get a certin control from the(System.Windows.Forms.Form.Controls),then handel its events of drag and drop ?
|
|
|
|
|
Hello,
I have an application made in asp with an SQL Database not published to the Web.I want to make another application via ASP.net that use the same database and post it to the server. (I think i have to post also these database)
Do the .net Remoting can allow to me accessing these database without posting it to the server.(costing of database hosting is very expensive).
Best Regards.
dghdfghdfghdfghdgh
|
|
|
|
|
I think you have a couple options. Either use SQL Express or SQL CE on the server (which are free), or host the database yourself and connect to it from your server.
“Time and space can be a bitch.”
–Gushie, Quantum Leap
{o,o}.oO( Looking for a great RSS reader? Try FeedBeast! )
|)””’) Built with home-grown CodeProject components!
-”-”-
|
|
|
|
|
I have made my own control inherited from System.Windows.Forms.UserControl
then by using reflection I have created an object from my own control at run time
after creation this control have been added to the collection of the container(form)
System.Windows.Forms.Form.Controls.
the problem is
When the control is in the form (before runtime) it's easy to handel any of its events but now how to handel this control's events as (MouseDown , MouseUp , DragDrop , DragEnter and DragOver) as this control has never been created yet ?
|
|
|
|