|
neodeaths wrote: is there a best practice The reason I stopped liking "best practices" is because people stop thinking once they're handed one.
How you will save the data is not dictated by the source of the data; it's how it will be used that dictates it's form. Since it's a webservice, I'd expect one to call the service again if the data is needed - getting new values.
If you simply want to cache the results; I'd recommend serializing it as a binary file. If it is part of a product that's locally installed, you check how that product saves/retrieves data. If you're going to be fetching large resultsets to be manipulating them locally, I'd suggest a local database. That is, if there's only one user. If there's more users, reading from different computers, you'd probably need a centralized database-server. Or, if security dictates - a webservice.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi
I've had a suggestion on a very basic bit'o'code and I want people's thoughts on it. The example is a voucher that has been redeemed. We produce a batch code for the voucher e.g. "ABC" and a code, e.e. "1234". The user sees the full remption code "ABC1234".
My code:
public void Foo
{
public int? Code { get; set; }
public string BatchCode { get; set; }
public string RedemptionCode
{
get
{
if(Code.HasValue)
return BatchCode + RedemptionCode;
return "";
}
}
}
Code review suggestion:
public void Foo
{
public int? Code { get; private set; }
public string BatchCode { get; private set; }
public void SetClaimedCode(string batch, int? code)
{
if(code.HasValue)
throw new AgumentException("...");
Code = code;
BatchCode = batch
}
public void GetClaimedCode()
{
return BatchCode + RedemptionCode;
}
}
My thinking is Claimed Code is a noun --> poperty. The reviewer thinks that there shouldn't be any processing in a property, as it is doing something it is a method.
Aside from the concat (which you may or may not agree with) and the visibility changes, and, to a certain extent the exception throw what do people think about the property vs method?
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Keith Barrow wrote: public string RedemptionCode
{
get
{
if(Code.HasValue)
return BatchCode + RedemptionCode;
return "";
}
}
I hope that's a typo, otherwise you've got infinite recursion in your property getter.
I agree with making the setters for Code and BatchCode private, since you need to set them both at once, and you can only set them once. Similarly, you can't use a setter to replace the SetClaimedCode method, since it needs two values.
I'd be inclined to leave the GetClaimedCode as a read-only property, and rename the SetClaimedCode method to something like Claim or Redeem . If the code parameter is required, I'd also make it an int rather than a Nullable<int> . Also, attempting to redeem the voucher twice isn't really a problem with an argument, so an ArgumentException isn't appropriate.
public string RedemptionCode
{
get
{
return Code.HasValue ? BatchCode + Code : string.Empty;
}
}
public void Claim(string batch, int code)
{
if (Code.HasValue) throw new InvalidOperationException("Voucher has already been claimed.");
Code = code;
BatchCode = batch;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yep, typo on the recursive code should have been just Code , I actually simplified a bunch of irrelevant stuff out. I agree on the exception part, we do a lot of this sort of thing here (it's practically a return methodology for anything out-of-the-ordinary). I'm sure I read an article actually advocating this somewhere, the idea was interesting but it seemed a short route to spaghetti without discipline.
Where I'm working we have lots of code reviews and there is enough difference for the way I usually work and how they want things done that it is jarring & I'm beginning to second guess myself. One of the big things is the split between methods and properties, this is a bad example I suppose due to the write-once nature of the properties confuses the issue. One of the things I need to sort out whether a)we drink the cool-aid here a little too much or b) I've not kept up with newer thinking on how c# should be structured. Obviously a bit of both is an answer, or probably the most likely answer.
[Edit]
Forgot my manners - thanks for your suggestions.
“Education is not the piling on of learning, information, data, facts, skills, or abilities - that's training or instruction - but is rather making visible what is hidden as a seed” “One of the greatest problems of our time is that many are schooled but few are educated”
Sir Thomas More (1478 – 1535)
|
|
|
|
|
Keith Barrow wrote: Where I'm working we have lots of code reviews and there is enough difference for the way I usually work and how they want things done
Which only indicates that the process is flawed. Code reviews should serve the following purpose
1. Bugs
a. Logical, requirements, security etc. Result: code must be fixed.
b.Associated with assumptions in other parts of the system. Result: Determine which code should be fixed then fix it.
2. Suggestion: Where one person states their preference for how it should be done. Result: Up to the developer whether they think it is worthwhile while fixing bugs whether to touch it.
3. Comment: Where the reviewer notes that they think the implementation is worth pointing out such that others might do the same in the future. Result: Normally nothing.
Sounds to me that someone or several someones seems to think that 2 requires a mandate and thus they, not you, are missing the point of reviews. And management doesn't understand it either. Micro-management doesn't do anyone any good. And it costs money as well.
|
|
|
|
|
Keith Barrow wrote: The reviewer thinks that there shouldn't be any processing in a property,
As stated that of course is nonsense. The ENTIRE point of properties is to allow processing in the property.
Now perhaps they just had an issue with this particular class.
That said and for the specific case then because the two values are nonsensical unless handled as a unit then one method would be my preference. However I might also create a data object that holds only those two entities as well since they are tied together.
|
|
|
|
|
I disagree. Properties were created because the language designer didn't like accessors and getters and wanted a cleaner method for acessing and getting members. While it certainly adds the ability to add logic, that is a major faux paus to do any more than is absolutely necessary at the bare minimum. Methods are more appropriate when actually functionality is desired.
I prefer:
public static string MakeSomeValue(double value1, string value2){
return some work
}
public string MakeSomeValue(){
return MakeSomeValue(Value1, Value2);
}
Unless I am using data-binding then all rules of good design are out the window just to support Satan.
Reference to accessors and setters.
http://en.wikipedia.org/wiki/Mutator_method[^]
|
|
|
|
|
Ennis Ray Lynch, Jr. wrote: I disagree. Properties were created because the language designer didn't like
accessors and getters and wanted a cleaner method for acessing and getting
members. While it certainly adds the ability to add logic, that is a major faux
paus to do any more than is absolutely necessary at the bare minimum. Methods
are more appropriate when actually functionality is desired.
That of course is ridiculous.
People used get/set because they perceived a need to somethimes add code to those.
Sometimes doesn't mean always and it doesn't mean inappropriately.
If one disallows that possibility entirely then there is no point in using get/set (or properties.)
Ennis Ray Lynch, Jr. wrote: Reference to accessors and setters
And your link says "...optionally validates it" so in your world where no code at all is allowed in there how exactly does one do that?
|
|
|
|
|
I have no argument against either of those.
|
|
|
|
|
Okay, I have a Drill/Exercise/Homework Assignment that my book has asked me to complete.
It seems fairly simply enough, from a end-user point of view.
(Probably easy for you old pros, I'm just beginning my coding career)
Anyway, here is my assignment:
"Drill 4-2
Write a C# program to compute the value of a number raised to the
power of a second number. Read the two numbers from the keyboard"
The first time I thought about doing this, just assigning two variables (integers) and assigning them a predefined value.
I would enter those values into the console window and get my answer.
I thought about doing it that way just to satisfy the homework assignment.
This would also assume I would be the only one using this and that was the only one that knew what values to enter.
Not very interactive and lazy after I thought about it.
So, I wrote down an outline and started following it. After a while, I found myself breaking away and changing things a little bit and adding on to it.
Now, keep this in mind… I'm just beginning, I'm just a newbie.
I have learned some shortcut methods, but… They're still just a little bit cryptic to me.
I also tried to comment as well as I could to explain what I was doing and thinking.
Down below is my code and what I have so far.
There is only three things (that I need to know right now, I think).
For right now, I just want to keep this as simple as possible.
I know I'm going to have to call the Main method.
How do I programmatically clear the screen and call the Main method to start the program over?
(We haven't covered this in my course/book yet)
Those were the first two things I need to know.
Third thing:
I probably should create another method for the second user input?
Okay, a few more things pop up in my head.
if the answer to the second variable/user input is incorrect and answer no, will the first variable/user input still be usable if I have to start the second question over?
It will still be in memory right?
I know I'm going to have to change my code towards the bottom if I create a new method for the second question//variable/user input.
<pre lang="c#">
using System;
using System.Text;
namespace CalcPowerOfNums
{
class Program
{
//Declaring the two main string variables to be used in our calculation.
string firstUserString;
string secondUserString;
static void Main(string[] args)
{
//Ask the user for the first number.
Console.WriteLine("Enter your first number and press the Enter/Return key");
string firstUserString = Console.ReadLine();
//Make sure this number is correct.
Console.WriteLine("You want to find the power of {0}?\n" , firstUserString);
//Declaring, Initializing string variables for user answer.
string firstAnswer = "";
//Make user confirm or deny their choice.
Console.WriteLine("Press the lowercase letter y for yes");
Console.WriteLine("Press the lowercase letter n for no");
Console.ReadKey();
//If user answer is yes, move on… It user answer is no, start program over.
do
{
if (firstAnswer == "y")
continue;
if (firstAnswer == "n")
}
//Ask the user for the second number.
Console.WriteLine("\nEnter your second number and press the Enter/Return key");
string secondUserString = Console.ReadLine();
//Make sure this number is correct.
Console.WriteLine("You want to use this number to find the power of {0}?\n", firstUserString);
//Declaring, Initializing string variables for user answer.
string secondAnswerYes = "y";
string secondAnswerNo = "n";
//Make user confirm or deny their choice.
Console.WriteLine("Press the lowercase letter y for yes");
Console.WriteLine("Press the lowercase letter n for no");
Console.ReadKey();
//Check to see if both first and second answer is yes.
if (firstAnswerYes == secondAnswerYes)
{
//Declare integers and assign the first and second user strings
//Then convert strings into integers
//Then use integers to calculate
//Then print the answer to screen
int firstUserInt = Convert.ToInt32(firstUserString);
int secondUserInt = Convert.ToInt32(secondUserString);
Console.WriteLine(Math.Pow(firstUserInt, secondUserInt));
}
Console.ReadKey();
}
}
}
</pre>
<a href="http://www.widmarkrob.com">My Coding Journey</a>
|
|
|
|
|
There are some excellent samples that show how to do this sort of thing the best way, in Charles Petzold's .NET Book Zero[^].
Use the best guess
|
|
|
|
|
|
Nope. Still no question there. Edit the post, rather than adding a new one, and explain what it is you are trying to achieve, and what you have done so far.
|
|
|
|
|
Use Google[^]; that's what it's there for.
Use the best guess
|
|
|
|
|
|
Help you with what? I don't see a question there.
|
|
|
|
|
I'm not looking for any code but for some possible references/links to find what I'm looking for.
I'm want to be able to export an Outlook profile (2010) to a PST file. I know you can do this in Outlook but it is time consuming to open Outlook and perform this action, then import it in the new profile.
I've looked over the assembly reference and not really finding anything: http://msdn.microsoft.com/en-us/library/office/bb652780(v=office.14).aspx[^] to do this.
I do see some information on the web about exporting calendar, contacts, tasks, etc to different formats such as Access Database, csv, etc... but haven't found anything to export to a PST using C#
Thanks in advance for any information!
|
|
|
|
|
I want to create a design where I have a pool of perhaps 5 worker threads, and any number of outside threads can post any number of work items for the worker threads to process.
The worker threads will wait in a loop using WaitOne() , and the outside threads will post the work items using calls to Release() .
Is there anything improper about this design? Am I misusing the semaphore, or doing anything not recommended?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I'd use the C# ThreadPool class instead of inventing your own.
|
|
|
|
|
Thank you, I was not aware of the ThreadPool class.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
hi
how are you
as i see you answer many questions about the http packet
and if you can help me please i will be very thankful for you
i am new in the programing in c# language
i have a pcap file , i open it in wiresharh program.
i nead to read this pcap file (which it is saved on my pc in a specific folder) in c# and display specific fields( time of the packet, ip source , ip destination , info(as it apear in wireshark) )
but not for all pachets just for the http protocol(as it aprear in wireshark)
i am using the (Tamir.IPLib.Packets) library and i program this
private static void device_PcapOnPacketArrival(object sender, Packet packet)
{
if( packet is IPPacket)
{
IPPacket etherFrame = (IPPacket)packet;
Console.WriteLine("time {0}, source ip address {1}, destination ip address {2} ",
etherFrame.Timeval.Seconds,
etherFrame.SourceAddress,
etherFrame.DestinationAddress);
}
}
this program print the fields (time ,source ip ,detination ip) but for all the packets
how can i specific just for the http protocol ( as it appear in wireshark)
thanks for your time
can you answer me on my email
mais_irreem@yahoo.com
|
|
|
|
|
Never post your email address in any forum, unless you really like spam! If anyone replies to you, you will receive an email to let you know.
Edit your question, and remove your email.
The universe is composed of electrons, neutrons, protons and......morons. (ThePhantomUpvoter)
|
|
|
|
|
Hi,
I have the above options to save user comments in MySQL in my Windows Form application (C#)?
Which one you recommend?
Technology News @ www.JassimRahma.com
|
|
|
|
|
Unless you are looking for storing shapes, tables etc you could go ahead with RTF.
|
|
|
|
|
That depends entirely on your appications requirements, not our opinions.
"Comments" where I work could be an entire Word document. It doesn't matter what we think, only what your requirements dictate.
|
|
|
|
|