|
If the object is created based on the info in the CSV-file, you could implement ISerializable interface, which allows you to control the serialization and deserialization.
If you only want to set the values of an existing object based on CSV input, I would create a method on each class (based on an interface) which sets the properties for the class and understands the input paranmeter (CSV string). The input string would be easiest to handle using String.Split-method.
In both cases you wouldn't need to use reflection.
If you want to use reflection, you would have a method with two (csv+object) mandatory parameters and a third one as parameter array, for properties list. In this case you would examine the type of the object and then find each property's PropertyInfo given in a parameter using object type's GetProperty method. After that you can use the SetValue method for the PropertyInfo t set the value of the property (just check that the property is writable).
Hope this helps,
Mika
The need to optimize rises from a bad design
|
|
|
|
|
Try this
void AssignPropertiesFromCSV(string csv, object obj, params string[] propertyNames) {
string[] csvValues = csv.Split(',');
int index = 0;
foreach (string propertyName in propertyNames) {
if (!string.IsNullOrEmpty(propertyName))
obj.GetType().GetProperty(propertyName).SetValue(obj, csvValues[index], null);
index++;
}
}
|
|
|
|
|
I think that is exactly what I am looking to do. Thanks a lot! I will start playing with that now.
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Navaneeth has given you a solution.
But... theres no benefit here to keeping these in properties. Assuming you have your Sale object taking a CSV row, just keep the row as an array when you split it:
class BaseCSVThing
{
string[] data;
BaseCSVThing(string Row) { data = SplitRowIntoArray(Row); }
}
class Customer : BaseCSVThing
{
string Name { get { return data[0]; } }
}
Or if you wanted to be less lazy, you could implement your own CSV serializer :P
Or you could take Navaneeth's example, but throw in some attributes to indicate the column - so you have something like [CSV(Column = 3)] string Foo { get;set; }.
|
|
|
|
|
Those are some good ideas as well. Treating all of the internal data as String s will be less efficient for me because I would have to keep converting them to their actual types (int s, Decimal s, DateTime , etc.) over and over again.
I have not played around with Attributes yet, and that sounds interesting, but since I may have various CSV formats, where certain values may be in different locations, I am not sure if that would work.
But thanks anyway for the ideas -- great food for thought!
Peace!
-=- James Please rate this message - let me know if I helped or not!<hr></hr> If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong! Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road! See DeleteFXPFiles
|
|
|
|
|
Good afternoon!
I'm a beginner with C#, but I'm trying to make a connection from my PC to a remote location system using a dial-up modem. I don't know from where I need to start. There's any kind of algorithm, tutorials or other documentation that could help me? To do this application, i need other software beside Microsoft Visual Studio?
My best regards.
|
|
|
|
|
You need to install your application on the remote location. Your application should be able to listen on a port. You can send messages to this application from other end through this port.
Look at TCPListener and Socket classes.
|
|
|
|
|
You could search, Google and MSDN and CP Articles are more faster and reliable. Take a look here[^] in CP
While (true) { Human.isLearnable = true; }
|
|
|
|
|
So I was wondering, which is more used, VB or C#. I was told that C# is the most recent and the most powerful. It doesn't even have to stop there, is there any other languages in the same category thats more powerful?
|
|
|
|
|
There's no "more or less powerful", it all depends on what you're trying to do with the language. Each language/framework has its strengths and weaknesses, there's no "perfect" language.
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Christopher Clarke wrote: which is more used, VB or C#
I use C# and VC++ at work. We have very few applications written on VB.NET. .NET applications written on any language will be converted into MSIL on compilation. So you can't say which language is powerful.
VC++ team blog says, VC++ compiler generates most optimized MSIL compared to other two. I think you will get a slight performance difference, but it is not noticeable.
|
|
|
|
|
N a v a n e e t h wrote: I think you will get a slight performance difference, but it is not noticeable.
WOW!
led mike
|
|
|
|
|
N a v a n e e t h wrote: VC++ team blog says, VC++ compiler generates most optimized MSIL compared to other two.
However, very little of the omptimisation is done when creating the IL code. Most of the optimisation is done by the JIT compiler when creating the native code from the IL code.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
The most used programming language is profanity, because sometimes, you just gotta say "f***".
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
John Simmons / outlaw programmer wrote: most used programming language is profanity, because sometimes, you just gotta say "f***"
That has to be the funniest thing I've read/heard today.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
|
F*** yeah, it's the funniest thing I've heard/read thus far, and today is still early here in Southern Cal
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Got my 5! Lost count of how many times I said that today!
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
John Simmons / outlaw programmer wrote: The most used programming language is profanity
Not sure. I believe Lying probably gives profanity a good run for it's money.
led mike
|
|
|
|
|
led mike wrote: John Simmons / outlaw programmer wrote:
The most used programming language is profanity
Not sure. I believe Lying probably gives profanity a good run for it's money. Big Grin
That's the difference between the private/academic world and the business world.
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots.
-- Robert Royall
|
|
|
|
|
led mike wrote: I believe Lying probably gives profanity a good run for it's money.
At least if you include buzzwording, bullshitting and statistics...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
|
Completely correct.
One of my favourite metrics is "number of uses of F*** in the source" over time.
|
|
|
|
|
C#, but more from expressiveness than from "powerfulness".
On the other hand, if you pit a hundred random VB programmers against a hundred random C# programmers in an arm wrestling tournament, the winner will likely be a C# programmer.
|
|
|
|
|
Yeah, with the new C# boxing features
While (true) { Human.isLearnable = true; }
|
|
|
|