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
Trying to put an integer data from database(Linq to sql) into a label getting this error exception:
left-hand side of an assignment must be a variable property or
indexer
Code:
protected void Page_Load(object sender, EventArgs e)
{
DataClassesDataContext data = new DataClassesDataContext();
var visit = (from v in data.SeeSites where v.Date == todaydate select v).FirstOrDefault();
int seennow = visit.See; // On This line I can put data in seenow variable, no problem
Convert.ToInt64(lblSeeNow.Text) = visit.See; // exception error appears here
}
Try:
if (visit.See != null) {
lblSeeNow.Text = visit.See.ToString();
}
You cannot assign something to a function result. In your case lblSeeNow.Text is of type String hence usage of ToString(); method of your Int value.
You need to use
lblSeeNow.Text = visit.See.ToString();
Convert.ToInt64(lblSeeNow.Text) = visit.See;
As you mentioned, this is the issue.
Convert.ToInt64 is a method. But you're trying to save a value to it.
You can't.
Just do this
lblSeeNow.Text = visit.See.ToString();
I think you want
lblSeeNow.Text = visit.See.ToString();
You can't assign anything to
Convert.ToInt64(lblSeeNow.Text)
because it evaluates to a number.
Convert.ToInt64(lblSeeNow.Text) isn't a variable. It takes the value in lblSeeNow.Text and converts it to a long. There isn't a variable to store stuff in anymore.
You probably want this:
lblSeeeNow.Text = visit.See.ToString();
You should convert the integer to string, also add a check for being sure that visit is not null
lblSeeNow.Text = visit != null ? visit.See.ToString() : string.Empty
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have a small piece of code to refactor. Someone wrote cast in such style:
list.OrderBy(u => (int)u.Original).First();
Sometimes this code throw Invalid Cast Exceptions (field Original is of type object).
Example:
list[0].Orginal = 200,
list[1].Orginal = 85
Everything is Ok.
list[0].Orginal = 275452,
list[1].Orginal = 154754
Throws Exception
Anyone know why?
Since it sometimes throws the invalid cast exception, it means you sometimes have non-int types of instances in your list. You can avoid them before casting, but this is not really a good type of a design I guess.
I would do as I show below as a quick fix.
list.Where(u => u.Original is int).OrderBy(u => (int)u.Original).First();
Then I would go ahead and check what am I missing as following:
list.Where(u => !(u.Original is int)).ForEach(u => Console.WriteLine(u.Original.GetType()))
Then fix the list beforehand.
As others suggested you should avoid the unnecessary cast. In your class simply change the type of Orginal from object to int and you won't need the cast in the LINQ query.
The code compiles and runs with no problem.
https://dotnetfiddle.net/dadrYJ
These are not the Datatypes you are looking for
What you actually meant was
https://dotnetfiddle.net/ygJTyU
list[0].Orginal = 2147483647;
list[1].Orginal = 154754;
where n > 0
Given that. The bug is pretty clear.
Refactor:
var value = int.MaxValue + n;
list[0].Orginal = value;
Refactor:
int64 value = int.MaxValue + n;
list[0].Orginal = value;
Therefore:
public int Lambda_OrderBy(Foo u)
{
return (int) u.Orginal;
}
Refactor:
public int Lambda_OrderBy(Foo u)
{
int64 value = u.Orginal;
return (int) value; // FAIL!
}
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 7 years ago.
Improve this question
This code throws an exception if the key does not exist.
For example, if the key exists for a position in an array index the code is okay, even if the value is null. But, if the key does not exist the code throws an exception`. The code in the select token parenthesis is dynamic (a string variable).
r["Value"] = json.SelectToken($.Objectives[x].state).ToString() ?? "";
You can't call ToString() on a null value.
JToken value = json.SelectToken("$.Objectives[x].state");
r["Value"] = (value != null) ? value.ToString() : "";
You could use the tenary operator to return a default value if x doesn't exist
r["Value"] = $.Objectives[x] ?
json.SelectToken($.Objectives[x].state).ToString() ?? "
: '';
OR
r["Value"] = x >= $.Objectives.Length ?
json.SelectToken($.Objectives[x].state).ToString() ?? "
: '';
I'm not sure why you end the line with a double quote. Maybe a typo? But I didn't fix it, that code is what you started with.
In javascript, if a given variable has a value, it will return true to the following:
if(r["Value"]){
//this only runs if r["Value"] exists
}
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 am making a sort of statistical software that firstly needs to 'detect' the datatype of an array.
Firstly, X[,] is an array of sometype, can be all strings, all double, all ints or a combination of all.
Now, for every column X[] I need to know the datatype. Like:
If everything is 0 or 1, then Boolean (or binomial)
elseIf everything is integer, then integer
elseIf everything is double, then double
else: String
I need something like this in C#.
So it seems what you're trying to do here is find the "lowest common denominator" of types here. The most derived type that all of the items in the collection "are".
We'll start out with this helper method to get the entire type hierarchy of an object (including itself):
public static IEnumerable<Type> BaseClassHierarchy(object obj)
{
Type current = obj.GetType();
do
{
yield return current;
current = current.BaseType;
} while (current != null);
}
Now we can take a sequence of objects, map each to its hierarchy, intersect all of those sequences with each other, and then the first item of that result is the most derived type that is common to all of the other objects:
public static Type MostDerivedCommonType(IEnumerable<object> objects)
{
return objects.Select(o => BaseClassHierarchy(o))
.Aggregate((a,b)=> a.Intersect(b))
.First();
}
One simple idea is you can try to cast/parse as the different types and if that fails, move on to the next type. A very brief example of this is:
foreach (var element in myArray) {
double parsedDouble; int parsedInt;
var defaultValue = element.ToString();
if (Double.TryParse(defaultValue, out parsedDouble)) {
// you have something that can be used as a double (the value is in "parsedDouble")
} else if (Int32.TryParse(defaultValue, out parsedInt)){
// you have something that can be used as an integer (the value is in "parsedInt")
} else {
// you have something that can be used as an string (the value is in "defaultValue")
}
}
I believe that should probably get you started. Good luck!
Note
As other's have said - it is better to use strong types in C#. In most cases you can probably select a single type and use that rather than performing the checks above.
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;
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++;
}