|
Interfaces cannot have any implementation, which is what I want
|
|
|
|
|
Well, abstract will force the subclass to implement, but that prevents you from having a base implementation.
You could do this with a pair of methods...
public void Walk()
{
WalkInternal();
}
protected abstract void WalkInternal();
This way, the subclass is forced to implement WalkInternal() , but all external calls would go to the base class's Walk() (Which can't be overridden).
|
|
|
|
|
When I need this type of functionality, I normally implement an abstract On method that the sub classes have to override. Here's an example:
public abstract class Animal
{
public void Walk()
{
Console.WriteLine("Starting the legs off now...");
OnWalk();
}
protected abstract void OnWalk();
}
|
|
|
|
|
Member 8209737 wrote: can someone give me some pointer as to go about this ?
In general it isn't usually worth time trying to get someone else to do the 'right' thing.
For example even if you force it one can still throw an exception or just call the base method (whether that is right or wrong.)
One uses interfaces and abstract classes not to force others to do the correct thing but rather to implement functionality that would otherwise be difficult, hard, impossible or obscure via some other methodology. If done correctly. If not done correctly then the design if flawed anyways so implementation details become meaningless.
One uses unit tests, system tests, etc to insure that correct functionality is implemented.
|
|
|
|
|
Member 8209737 wrote: can someone give me some pointer as to go about this ?
Does it have to be on the class level, or would it be alright to have it at the object-level (say, an event[^]?)
class SomeAnimalClass
{
public event EventHandler IsWalkin;
protected virtual void OnWalk()
{
Console.WriteLine("Doing walking stuff for " + this.GetType().Name);
EventHandler isWalkin = IsWalkin;
if (isWalkin != null)
{
isWalkin(this, EventArgs.Empty);
}
else
throw new NotImplementedException("DoWalk isn't implemented in " + this.GetType().Name);
}
public void StartWalkin()
{
OnWalk();
}
}
class Cat: SomeAnimalClass { }
class Dawg: SomeAnimalClass { }
class Program
{
static void Main(string[] args)
{
SomeAnimalClass animal = new Cat();
animal.IsWalkin += (o, s) =>
Console.WriteLine("specific walking stuff for this animal of type " + animal.GetType().Name);
animal.StartWalkin();
SomeAnimalClass otherAnimal = new Dawg();
try
{
otherAnimal.StartWalkin();
}
catch (NotImplementedException nie)
{
Console.WriteLine(nie.Message);
}
Console.ReadKey();
}
}
Output;
Doing walking stuff for Cat
specific walking stuff for this animal of type Cat
Doing walking stuff for Dawg
DoWalk isn't implemented in Dawg
Reposted, seems I did something wrong as the prev. post doesn't show up
Bastard Programmer from Hell
|
|
|
|
|
I like the idea, and someone else suggested using event as well, however I think it would add to the complexity and would make a developers life harder.
Im going to have a think and have a play around with the suggestions
|
|
|
|
|
In a word, no. The closest thing is to declare the method abstract, but as others have pointed out, you can't have an implementation together with abstract.
However, if you have a base implementation, and it's valid for the subclass simply to call base.Walk, why do you want to require it to be overridden in the first place? Subclasses which do not will inherit that version of the method anyway, essentially having an implicit 'Walk() { base.Walk{} }' in terms of usage.
There's really three common scenarios that I've seen:
- You want polymorphic behaviour, but the base class doesn't know what that might be. This is what abstract methods are for.
- You want to allow subclasses to provide behaviour, but the base class has some idea what it a sensible default. For this you should use a virtual method, even if the default action is nothing.
public virtual void Walk(){} - You know what the basic template for behaviour is, but you want subclasses to be able to customise parts of it, or you are missing information. I tend to do this with the 'MethodName'/'MethodNameInner' approach, where MethodNameInner is usually protected abstract, but if there is a reasonable default (often empty), it can be protected virtual instead.
public void Walk(){
Leg legToMove = LegToWalkWith;
legToMove.MoveForward();
}
protected abstract Leg LegToWalkWith { get; }
or more realistically (pseudocode as I can't remember exactly how this works right now):
public void ReadConfig(string configFile){
XmlDocument xmlDoc = GetXmlDocument(configFile);
XmlElement elem = xmlDoc.GetElement(ConfigNodeXpath);
foreach(XmlNode node in elem.ChildNodes){
XmlElement subelem = node as XmlElement;
if(subelem == null) continue;
}
}
protected abstract string ConfigNodeXpath { get; }
Your situation sounds more like the second scenario (you want to provide a default implementation) and therefore a virtual method should do the job nicely.
|
|
|
|
|
In SQL Server 2008, I have a table with several columns. One of the columns is "Item Type". My application currently generates a basic report which says something like this:
# Units Received 100
# Units Tested 85
# Units Shipped 60
# Units Awaiting Shipping 25
I have been instructed to change this report in a way that will break the units down by the "Item Type" field. I have received a printed example which was created in MS Excel 2007 which looks like this:
Total
# Units Received 100
Laptop 50 50% (Percent of total)
Netbook 30 30%
Desktop 20 20%
# Units Tested 100
... Amt Pct%
etc., etc.
I am using LINQ To SQL (C#, .NET 4.0, VS2010). Should I read all of the necessary records into a collection in memory and then create collections based on each type, by category (received, tested, etc.)? Or is there a better way to do this?
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
IMHO, presenting the data to the user is job of the front end code. Thus, the layout should be done by the application. So, you should get the data from the database and then change it the way you like for display.
This does not mean that you should get each and every thing from the database. Just get the relevant data from the database and format it the way you like.
"The worst code you'll come across is code you wrote last year.", wizardzz[ ^]
|
|
|
|
|
Thanks, d@nish. I understand what you are saying. I guess I could have clarified that I was uncertain about how to retrieve the data (efficiently) from SQL Server, not about how to present the data. But I think I have figure it out now.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
You can use DataReader since that is faster than DataSet. You can make use of SQL Profile and Database Tuning Advisor to improve the query performance. Checking the execution plan would help as well.
"The worst code you'll come across is code you wrote last year.", wizardzz[ ^]
|
|
|
|
|
If these numbers are going to remain the order of magnitude in question, yes, load the whole thing into memory and do whatever analysis you want in code. I agree with D@nish that it belongs there in principle, and with only a few hundred rows, performance shouldn't be a consideration.
|
|
|
|
|
Okay, thanks. I implemented it this way, loading the data into memory and then manipulating the data from there. It is quite fast -- and definitely quick enough for the purpose of the report. However, I have several thousand rows, not just hundreds.
djj55: Nice but may have a permission problem
Pete O'Hanlon: He has my permission to run it.
|
|
|
|
|
Linq to Sql support the group by clause which might be helpful to you.
|
|
|
|
|
i have tried
Guid.NewGuid().ToString().GetHashCode().ToString("x").ToUpper();
but I do not get Unique Codes after 10,000 tries
i need to add ABCDEFGHIJKLMNOPQRSTUVWXYZ and 1234567890
somebody help!!!
|
|
|
|
|
How many characters do you need? There are a number of random password generators available, have you looked? One of those should be sufficient.
No comment
|
|
|
|
|
GUIDs are unique (before you mess about with hashcodes and stuff). What is your requirement and why can't you use those?
|
|
|
|
|
GUIDs are large enough that they'll usually be unique (But not guaranteed)... But once you take the hash code, you're dropping from a 16-byte GUID to a 4-byte integer, and limiting it even more by the hashing algorithm...
So why not build your own algorithm? If you just need them to be unique, you can start from "AAAA" (Or whatever length) and just increase as if it was a number... AAAA, AAAB, AAAC, ..., AAAZ, AAA0, AAA1, and so on.
If you want them to look random and be unpredictable, just generate a number of random integers from 0 to 36, and convert each one to a letter or digit... String them together, and you've got a random ID... Then just keep a hash/dictionary of previous IDs to compare against, and re-generate if you create a duplicate.
|
|
|
|
|
Guid should already satisfy your requirement in creating unique identifiers. They're already a product of cryptographic hashing functions, so you shouldn't need to hash them again. You can use ToString with a format provider to get a string representation of the value without the dashes.
Or, as an alternative, you can always roll your own UUID implementation according to the standards.
One other note - GetHashCode isn't a cryptographic hash, nor is it guaranteed to be consistent between frameworks or platforms. Check the Remarks section at MSDN.
|
|
|
|
|
How do I add row data directly to a DataGrid Class?
I am using a free opensource class from a company that I will not name (even if it is RadiantQ) that I like a lot. It has this cool MuttiColumnTreeList control that is a combination of a tree control and a datagrid. It comes with an example code that you can look at and everything. It is pretty cool. It is inherited from the DataGrid class.
The problem is that I am kind of new to databinding on this level and so I would like to just go ahead and write some code that forces the data that I have gathered from another class into the control.
So I looked online for how to do that for a DataGrid class and the information is not easily available. Can anyone help?
|
|
|
|
|
Which DataGrid? Are you talking about the XAML based version? If so, please take your questions to the Silverlight/WPF forum as they are more appropriate there.
|
|
|
|
|
It seems that once the databinding is done, and if you change the data you have to rebind to the control. This is what was giving me difficulty before. So what I have to do is run some command like this:
this.mutlicoolgridview.ItemsSource = null;
this.mutlicoolgridview.ItemsSource = SampleData.GetSampleDataNew();
The problem I am having now is this. After running his command about one thousand times, I actually run out of memory. I think that doing this:
this.mutlicoolgridview.ItemsSource = null;
is not such a good idea. Is there a better command to do to free up the memory?
|
|
|
|
|
Xarzu wrote: It seems that once the databinding is done, and if you change the data you have
to rebind to the control.
No you don't. You're doing it wrong. With XAML binding, you bind to a collection that implements ICollectionChanged (such as an ObservableCollection ), then you update the original collection - you don't create a new one, you don't rebind to it.
|
|
|
|
|
Thank you, Pete. I will change and test my code. Is there some way I can force a ICollectionChanged message?
|
|
|
|
|
If you're using an ObservableCollection , you don't have to. It raises the CollectionChanged events for you when you add or remove items.
|
|
|
|