"use of unassigned local variable" error when passing ref parameter [closed] - c#

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
This code generates the error:
used of unassigned local variable 'namespace2'
XNamespace namespace2;
string partText = Declaration.partText;
Declaration.partText = string.Empty;
string str = "";
IEnumerable<XElement> source = InputXDoc.Descendants(Function.GetNamespace(ref namespace2, "").GetName("body"));
if (source.Descendants<XElement>(Function.GetNamespace(ref namespace2, "").GetName("div")).Count<XElement>() > 0)
{
IEnumerable<XElement> introduced5 = InputXDoc.Descendants(Function.GetNamespace(ref namespace2, "").GetName("body"));
if (introduced5.Descendants<XElement>(Function.GetNamespace(ref namespace2, "").GetName("div")).First<XElement>().Attributes("id").Count<XAttribute>() > 0)
{
IEnumerable<XElement> introduced6 = InputXDoc.Descendants(Function.GetNamespace(ref namespace2, "").GetName("body"));
this.ChapterName = introduced6.Descendants<XElement>(Function.GetNamespace(ref namespace2, "").GetName("div")).First<XElement>().Attributes("id").First<XAttribute>().Value;
}
}
Why did I encounter this?

From MSDN for ref:
An argument that is passed to a ref parameter must be initialized
before it is passed. This differs from out parameters, whose arguments
do not have to be explicitly initialized before they are passed. For
more information, see out.
so you need to write:
XNamespace namespace2 = null;
Its always good practice to initialize your variables anyways!
Note: I initialized to null because I don't know what your parameter actually needs to be initialized to. Check the documentation for the function you are calling, you will likely need it to be something else.

Related

get a value from an object property and assign it to a C # string value [closed]

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 2 years ago.
The community is reviewing whether to reopen this question as of 2 years ago.
Improve this question
I have a below code to capture an objects values:
var key = fulfillment.GetType().GetProperties().FirstOrDefault(p => p.Name.ToLower().Contains("operator")).GetValue(fulfillment);
the code return:
the Operator property type is:
[JsonProperty(PropertyName = "operator")]
public object Operator { get; set; }
i want to get the name value of the index 1 -> OMS_OPERATOR_AUTOMATED and assign it to another string variable. How can i do this ?
Final answer after looking at code and data structure the answer was:
var foundOperator = (Dictionary<string, object>) fulfillment.Operator;
var teste = foundOperator["name"];

Checking if json key exists for a given array index? [closed]

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
}

How to pass array to method [closed]

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
It's probably a stupid question, but anyway.
My problem is that I can't pass uninitialized array, but I don't know if my array need to hold 5 or 30000 elements, eg. so it will be a waist of memory to initialize big array.
Should I use List<T> instead, or?
I've noticed people tend to return array instead of list, which is mutable, and therefore much more convenient, so there must be a performance issue with lists. Is that so?
make it into an 'out' parameter and all should be well:
private void x()
{
string sTestFile = "this is a test";
string[] TestFileWords;
FixConcatString(sTestFile, out TestFileWords);
}
private void FixConcatString(string splayfile, **out** string[] sWordArray)
{
char[] charSeparators = new char[] { '-' };
splayfile = splayfile.ToLower();
splayfile = splayfile.Replace(#"\", " ");
sWordArray = splayfile.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
}

left hand side of an assignment must be a variable [closed]

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

Whats the "Params" keyword in method parameter? [closed]

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 9 years ago.
Improve this question
Whats the "Default" keyword in this method?
public IEnumerable<T> createEmpty(Object key)
{
foreach (var item in MyCollection)
{
T element = new T();
element = default(T);
yield return element;
}
}
You mean the "Default" keyword?
Question already answered here: What does default(object); do in C#?
Thats only a method that returns the default value for that type, for example:
Int32 number = default(Int32); // returns 0
Object myObject = default(Object); // returns null
bool flag = default(bool); // return false

Categories