Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i'm need to know how to add a value to a property using reflection
example i have property with name Health
i want to increase the value by 10
var prop = typeof (Character).GetProperty(actionParams.PropertyName);
var oldValue = (int)prop.GetValue(client.Character);
prop.SetValue(client.Character, Convert.ChangeType((oldValue + 10), prop.PropertyType));
but i get cast error
System.InvalidCastException: Specified cast is not valid.
Most probably your property with name Health is not of type int, so the cast (int)prop.GetValue(client.Character); fails.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I keep seeing the following type of syntax:(string[])myList.ToArray(typeof(string));
What does it mean when the object type is declared at the front of the object in brackets, before calling a method on it?
I am struggling to locate explanations becuase I don't know what this setup would be called.
Any help appreciated.
Thanks
it's Called a Casting, Casting is usually a matter of telling the compiler that although it only knows that a value is of some general type, you know it's actually of a more specific type. For example:
object x="any string";
string s=(string)x;
if we are using the upper one then it may possible that it will through the exception at runtime like if you are using
object x="string";
int s=(int)x;
it will through the Exception at runtime unable to cast
but if you use as oprator then it will return a null rather then throwing an exception.
object x = new object();
string y = x as string; // Now y is null because x isn't a string
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I want to instantiate application object and initialize a list with it.
I have this code in VB.net but I cant convert it to C#.
this is VB code:
If Not IsNothing(Application("MessageList")) Then
MessageList = Application("MessageList")
End If
You have to compare the Application collection object with null. Also type cast the object returned by Application collection to MessageList if MessageList is a type.
if(Application["MessageList"] != null)
MessageList = (List) Application["MessageList"];
Edit based on comments
The MessageList of List of Message and declared
List<Message> MessageList = new List<Message>();
Your condition would be.
if(Application["MessageList"] != null)
MessageList = (List<Message>) Application["MessageList"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have property which holds filenameOnly without extension.
string fileNameOnly = "myFileName";
now I want to perform multiple checks on selected location for example c:\ for existed filename+fileextension (.jpg, .png, .gif, .bmp)
so, if c:\myFileName.jpg exist than assign that value to string filename="myFileName.jpg" variable.
what is the best and quickest way to do this?
Try something like this to get an array of files that fit your pattern:
string[] files = Directory.GetFiles(
DirectoryPath,
String.Format("{0}.*", fileNameOnly));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a typed dataset with a column called price, which is of string datatype. And I'm showing this via ultrgrid/datagrid
How can I show the price in number format.
For example: price ==123456
in the grid it should be like 12,354,56
Try this
var a = price.ToString("##,###,##");
Reference
Custom Numeric Format Strings!
Look at some of the related links to the right of the screen. Some of them even ask the same question as you !
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
SQL statement :
select 'iphone',count(*) from tablename where id=5
Result:
Name Num
'iphone' 50
How should i do with linq to get the same result(SQL statement)?
thanks in advance.
var Result = new { Name = "iPhone", Num = yourSequence.Count(x => x.Id == 5) };