|
1) you don't new up the strategies on start, you new them up when they are requested
2) its not really necessary to cache the strategy objects unless they are expensive to create (new'ing up a class with an empty constructor is very cheap)
3) at the bottom of a strategy pattern is a switch
4) you are doing too much in the switch, all the switch should do is return the instance and the switch should be "internal". I.e., you'd have a static strategy factory class that has a static GetStrategy(someEnum) and returns the ICalculationMethod object. So your caller just needs to do StrategyFactory.GetStrategy(someEnum); The GetStrategy() method is going to have the switch.
5) your suggested use case is a trivial case and probably strategy is overkill for simple calculations... a more realstic scenario might be FedEx vs UPS shipping for example. Your strategy class will have methods for calculating the shipping, tracking, interacting with FedEx or UPS web service, etc. So it'll be much more then just a single method.
If your strategies are all 5 to 10 lines, I'd say strategy is overkill.
|
|
|
|
|
Voted-up. Seems like a very good analysis to me. Why build a rocket-ship that can go to outer-space if your destination is the moon
«OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. » Alan Kay's clarification on what he meant by the term "Object" in "Object-Oriented Programming."
|
|
|
|
|
BillWoodruff wrote: Why build a rocket-ship that can go to outer-space if your destination is the moon I seem to recall that the moon is in outer space!
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
Hi,
I am using dynamic datetimepicker that the user can add to any number. But I am having postback problem. Once I submit it, the return page should have all the submitted values including those added dynamic datetimepicker. Right now when i submit and when the page returns, all other static controls with data are there but no dynamically added datetimepicker controls.
Below is the code I used to add datatimepicker dynamically using javascript:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<link rel="stylesheet" media="all" type="text/css" href="jquery-ui.css" />
<link rel="stylesheet" media="all" type="text/css" href="jquery-ui-timepicker-addon.css" />
<script type="text/javascript" src="jquery-1.11.1.min.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script type="text/javascript" src="jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="jquery-ui-sliderAccess.js"></script>
<script type="text/javascript">
$(function () {
var counter = 1;
jQuery("#<%= btnD1Add.ClientID %>").click(function (event) {
event.preventDefault();
jQuery("<li>").appendTo(".Date1More");
jQuery("<label id='lblD1StartTime" + counter + "'>StartTime:</label><input type='text' id='txtD1StartTime" +counter + "' name='txtStartTime1'/>").timepicker({
hourGrid: 10,
minuteGrid: 10,
timeFormat: 'hh:mm tt'
}).appendTo(".Date1More");
counter++;
});
});
</script>
<asp:Button ID="btnD1Add" runat="server" Text="Add more time slots" />
<li>
<ul class="Date1More">
</ul>
</li>
</html>
Now how to use postback for these controls? I had tried to use following code in code but didn't work. I even tried it using !Ispostback() function under pageLoad but no effect.
protected string[] txtStartTime1;
protected void Page_Load(object sender, EventArgs e)
{
txtStartTime1 = Request.Form.GetValues("txtStartTime1");
}
protected void Page_Init(object sender, EventArgs e)
{
try
{
if (txtStartTime1.Length > 0)
{
TextBox txtStTime1 = new TextBox();
txtStTime1.ID = "txtD1StartTime";
txtStTime1.CssClass = "timepicker";
this.Controls.Add(txtStTime1);
}
}
catch(Exception ex){}
}
Please help..
Dhyanga
modified 4-Dec-14 11:06am.
|
|
|
|
|
|
Thanks. Yes I did.
Dhyanga
|
|
|
|
|
My function takes resource file name as input and returns Bitmap object.
GetManifestResourceNames returns fully qualified resource names. Sample Input is like "Fileopen.png".
How to find the required resource without doing any string operation ? Or any other way is also welcome.
public static Bitmap GetBitmap_FromResource(string ImageName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string strResName = GetReqdResourceName(assembly, ImageName);
if (string.IsNullOrEmpty(strResName))
return null;
Stream imgstream = assembly.GetManifestResourceStream(strResName);
if (imgstream == null)
return null;
Bitmap Img = new Bitmap(imgstream);
return Img;
}
public static string GetReqdResourceName(Assembly assembly, string strResName)
{
string[] resNames = assembly.GetManifestResourceNames();
return null;
}
|
|
|
|
|
What exactly do you mean by "find the required resource without doing any string operation"? Please explain exactly what problem you are trying to solve.
|
|
|
|
|
Hi! I have a problem with how my program handles an invalid input.
It goes like this:
Enter string: Apple
Enter integer: 1
Enter string: Orange
Enter integer: 2
Search array: Apple
Search found with string name Apple with integer number 1
Search array: Banana
Search found with string name Apple with integer number 1
Object searchValue = null;
string searchString;
do
{
searchString = GetCorrectInput.String(message);
if (!searchString.Equals("Quit"))
{
for (int i = 0; i < objectArray.Length; i++)
{
if (searchString== objectArray[i].studentName)
{
searchValue = objectArray[i];
}
}
if (searchValue == null)
{
Console.WriteLine("{0} was not found", searchString);
}
else
{
Console.WriteLine("Search found string name {0} with integer number {1}", searchValue.stringName, searchValue.integerNumber);
}
}
} while (!searchString.Equals("Quit"));
Console.WriteLine("Thank you!");
Banana is supposed to be not found but its not
|
|
|
|
|
You forgot to set searchValue back to null. The proper place to do that would be at the top of the loop right after searchString is being set.
Regards,
Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
is there a way to avoid using null?
|
|
|
|
|
Member 11284734 wrote: is there a way to avoid using null?
Why would you want to do that?
Please explain in detail!
Your code (if it even is your code which I'm now beginning to seriously doubt) uses the check for null explicitly so that would be the way to go.
You could of course use a special instance of whatever class ( it is not shown in your code sample) to signify that the searchString wasn't found, but that special instance also would have to be assigned to searchValue every time somewhere at the top of the loop before coig through the array comparing searchString against studentName .
Regards,
Manfred
"I had the right to remain silent, but I didn't have the ability!"
Ron White, Comedian
|
|
|
|
|
Apart from resetting searchValue variable to null, I think you also should break out if for loop as soon as you found the matching value in array.
|
|
|
|
|
Any answers are appreciated.
Thanks....
|
|
|
|
|
What Operating System?
With Windows (7) I have been using NET USE to create a mapped drive, COPYing to it, and then using NET USE to /delete the mapped drive.
f:\>net use ?
The syntax of this command is:
NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]
|
|
|
|
|
Hi,
I would like to ask why I am getting the following error on the below code please?
The error is: invalid length for a base-64 char array or string
and this is the code:
string server = cipher_utility.Decrypt<RijndaelManaged>(xmlClass.read_registry("Softnames", "ServerHost"), "{1234567-1234-1234-1234-123456789012}", "{12345678-1234-1234-1234-123456789012}");
Thanks,
Jassim
Technology News @ www.JassimRahma.com
|
|
|
|
|
Probably because the encrypted data has not been stored (or encrypted) correctly. And you would find debugging your code much easier if you did not write compound statements. It is just as efficient, and so much easier to debug, and to read, to write it as:
string encryptedData = xmlClass.read_registry("Softnames", "ServerHost");
string server = cipher_utility.Decrypt<RijndaelManaged>(encryptedData, "{1234567-1234-1234-1234-123456789012}", "{12345678-1234-1234-1234-123456789012}");
|
|
|
|
|
so how to go with the fix?
Technology News @ www.JassimRahma.com
|
|
|
|
|
What fix? Your encrypted data is either incomplete or has been corrupted. You need to diagnose the issue to find out how that happened.
|
|
|
|
|
I can't explain it properly in the subject section, it's not as simple as that.
Here is the code snippet in question.
UserPass = reader["Password"].ToString();
So UserPass is my string variable and reader is an SqlReader object.
Here is it being instantiated:
SqlDataReader reader = myCommand.ExecuteReader();
(myCommand is an SqlCommand obj)
reader["Password"]
Password is a column name in my Sql DB. Basically I've just told the reader to go look within that column and used a query to specify where abouts.
I want to set the reader["Password"] equal to an empty string to compare against user input. Now there aren't any errors when I do this but it does throw an exception:
An exception of type 'System.InvalidOperationException' occurred in System.Data.dll but was not handled in user code
It doesn't give much more detail other than that and I'm not entirely sure what's wrong.
|
|
|
|
|
Nothing you provided looks obviously wrong. Which statement throws the Exception? We may need to see the query.
And provide the code in proper order.
modified 3-Dec-14 14:47pm.
|
|
|
|
|
did you debug to identify on which line you had the issue?
Mark the answer as accepted if that worked for you .
And for down-voters please specify the reason to improve the solution .
|
|
|
|
|
HI...
I am beginner in WPF and I have a problem with saving data from DataGrid to text file (text file is usedby other program). I check on google, bing and find nothing. Please help.
|
|
|
|
|
I'd suggest you post your code and show where you are stuck.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Member 10024517 wrote: I have a problem You need to explain clearly what that problem is if you want assistance.
|
|
|
|