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
class abc
{
public object test(params object[] par)
{
//I need Count of the parameter here which means to check par contains 1 or 1,2
}
}
I access the class like,
abc obj =new abc();
obj.test(1);
(or)
obj.test(1,2);
my question is, there is possible to send 1 or 1,2 .I need the count of the How many parameters are in the Object in test class?How to do this?
Use Array.Length property.
public object test(params object[] par)
{
var count = par == null ? 0 : par.Length;
}
You can use Length property of object array.
public object test(params object[] par)
{
int length = par == null ? 0 : par.Length;
}
You can use Length property
Try This:
int len=par.Length;
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to write a program which does the following:
There is a method called lengthOffTheWords. It receives an array of strings, and returns an array of numbers which represent the length of each individual string.
Ex: For the following input
{"I", "know", "a" , "friend"} the method returns {1,4,1,6} .
{"yes"} the method returns {3}.
{"me", "too"} the method returns {2,3}.
I would like to see an example of how to write it.
I would do something like this:
public int[] LengthOffTheWords(string[] array)
{
return array.Select(item => item.Length).ToArray();
}
I did not test this but it should do what you want.
int[] GetLengths(string[] array)
{
int[] structure = new int[array.Length];
for(int i = 0;i < array.Length;i++)
{
int length = array[i].Length;
structure[i] = length;
}
return structure;
}
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 9 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a method that needs to convert a string to the generic type:
T GetValue<T>(string name)
{
string item = getstuff(name);
return item converted to T // ????????
}
T could be int or date.
you can use Convert.ChangeType
T GetValue<T>(string name)
{
string item = getstuff(name);
return (T)Convert.ChangeType(item, typeof(T));
}
if you need to limit input types only for int and DateTime, add condition like below
if (typeof(T) != typeof(int) && typeof(T) != typeof(DateTime))
{
// do something with other types
}
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 want to compare the array element with number.
ArrayList arr_obj = new ArrayList();
double a=1;
arr_obj.Add(1);
arr_obj.Add(2);
for(i = 0; i < arr_obj.Count; i++)
{
if (arr_obj[i] == a)
{
Debug.Log("ext");
}
}
This code does not work. Any ideas?
Yo are comparing an object to a double which won't compile. You will either have to store doubles in the ArrayList (as below) or first cast the values to int and then to double Ie (double)(int)arr_obj[i] because you can only unbox to the exact same type as you boxed (in your case that's int)
If you wish to stick to an ArrayList, not recommended, change to
ArrayList arr_obj = new ArrayList();
double a=1.0;
arr_obj.Add(1.0);
arr_obj.Add(2.0);
for(i=0;i<arr_obj.Count;i++){
if ((double)arr_obj[i]==a) {
Debug.Log("ext");
}
}
or you could use a List<double>
var list = new List<double>{1.0,2.0};
var a=1.0;
for(i=0;i<list.Count;i++){
if (list[i]==a) {
Debug.Log("ext");
}
}
You can convert it with Convert.ToDouble(arr_obj[i])
You have to cast the ArrayList to double:
(double)arr_obj[i]
ArrayList can contain any type beacause it is a list of objects. So, if you are going to cast it like this, you need to be sure that each array list is of type double by only putting doubles in the array list, or check it before casting like:
if (arr_obj[i] is double)
{
if ((double)arr_obj[i] == a)
{
Console.WriteLine(arr_obj[i].ToString() + " is double");
}
}
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'm stuck in a problem.
I have a class :
public class CustomerViewModel
{
public string Name;
public string[][] values;
public bool[] flag;
}
I wish to get count of rows in values[][] where row !=null
Assuming you already have:
var vm = new CustomerViewModel();
And have populated the values array, then..
For a count of non-null rows try:
var count = vm.values.Count(i => i != null);
Or for all rows where values[row][0] is not null:
var count = vm.values.Count(i => i != null && i.Length > 0 && i[0] != null);
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 Json file in string (for example):
#{
"Url": "http://site.com/?q=windows8"
}
How can i take the information after ?q= on c# (windows 8). Sorry for my English.
You can use the querystring.
in Codebehind file
public String q
{
get
{
if (Request.QueryString["q"] == null)
return String.Empty;
return Convert.ToString(Request.QueryString["q"]);
}
}
then use the line below to get the value
var index = ('<%=q%>');
You can do simply this :
string s = "myURL/?q=windows8";
// Loop through all instances of ?q=
int i = 0;
while ((i = s.IndexOf("?q=", i)) != -1)
{
// Print out the substring. Here : windows8
Console.WriteLine(s.Substring(i));
// Increment the index.
i++;
}