|
I created a simple search in Google and I found this[^] for resizing and this[^] for dragging in less than a minute. Always try Google, MSDN and CP articles first, they're more and more and even more faster. Maybe you should visit here[^] before you post your post here[^]
|
|
|
|
|
I have a program that I didn't write, but maintain. Recently the assembly version was incremented to 1.0.1.0 and when it tries to load serialized objects (binary formatter) that were made with 1.0.0.0 I get an InvalidCastException. There was nothing changed in these on the objects themselves so there should be really need to be a cast.
|
|
|
|
|
|
Thanks for the reply.
Because there was no actual change in the class (just version) I was able to just change the Version number listed in the binary file to the new version and it cast fine from there on out.
|
|
|
|
|
I have a window manager class. It maintains references to open User and Item forms:
At the top I create an array list:
private static ArrayList oWindows = new ArrayList();
The I have a method to return an instance of a form in the collection:
public void GetOpenForm(FormTypes type, int iRecordKey)
{
form oRetVal = null;
foreach(form oForm in oWindows)
{
if (oForm.iRecordKey == iRecordKey)
{
}
}
}
Problem is, the references to oForm in the code above are not User or Item forms, so the iRecordkey reference fails to compile. Add to this, the Item and User forms are not based off any single base form.
How do I make oForm "know" about iRecordKey?
Everything makes sense in someone's mind
|
|
|
|
|
You need to cast the Object in the list to the appropriate type.
Why not use a typesafe container like List(T)[^] instead?
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
How do I know what type to cast it to? I would have to have a CASE: statement for each potential type I might need to use.
Everything makes sense in someone's mind
|
|
|
|
|
KMAROIS wrote: I would have to have a CASE: statement for each potential type I might need to use.
Sure, why not? Makes sense to someone.
led mike
|
|
|
|
|
hmm...if you don't know the types of objects in your
collection, how is the compiler supposed to know?
If you're mixing object types in the collection then you don't
have a lot of choice.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
So! I finally am getting into more and more C#/.NET development, I am wondering if the following is possible using reflection and some other .NET-fu that I now nothing about (yet).
Here is what I am thinking... Presume I have several streams of CSV data that I want to convert into object form. While it is possible to parse the CSV and manually assign properties for each value, I have to write specific code for each object and (expected) stream format that I want to process.
I am wondering if this code can be generic so that I can just pass in the CSV stream, an object, and a set of properties that are on that object, and the code can extract the values, and use reflection (or something) to get at the property by its name, and set the value. Something like this:
String sCSVString = "<code>Value1,ValueABC</code>,Value2,<code>ValueXYZ</code>";
-----------------------------
AssignPropertiesFromCSV( sCSVString, objMyObject, "Prop1", "Prop2", null, "Prop4" );
Where the result would be that objMyObject would have Prop1 , Prop2 and Prop4 set to Value1, ValueABC and ValueXYZ respectively, skipping over the Prop3 property due to the null parameter passed in.
Is it possible to use reflection to dynamically examine objMyObject to find out if it has a Prop1 property, and then set it to a value?
My guess is that some ORM frameworks may work this way, but I have yet to delve into them and wanted to get a feel for it from the more C#-experienced devs here first.
While examples are not required, they are welcome. I just want to know if this is worth looking into or not.
Thanks!
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
|
|
|
|
|
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
|
|
|
|