On this declaration:
string[] TardyEvenEmorys;
...Resharper tells me, "Field 'TardyEvenEmorys' is never assigned."
Later on in the code, assignments are made to the string[]:
TardyEvenEmorys[1] = string.Empty;
TardyEvenEmorys[2] = string.Empty;
TardyEvenEmorys[3] = string.Empty;
TardyEvenEmorys[4] = string.Empty;
...and then actual values are conditionally added:
foreach (KeyValuePair<int, string> entry in itemNumberTardyPairs)
{
TardyEvenEmorys[entry.Key] = entry.Value;
. . .
...finally, those values are used in this way:
string url = GetTardyFilename(TardyEvenEmorys[Number]);
So what is Resharper telling me? That I should instantiate the string[] on declaration, or...???
You're assigning individual elements of the array, but never actually creating the array itself.
So what is Resharper telling me? That I should instantiate the string[] on declaration, or...???
You need to instantiate the array somewhere. This could be during the declaration, or later. For example, to do it during the declaration, you would need to add:
string[] TardyEvenEmorys = new string[5]; // Some appropriate length
Without this, the first time you assign one of the elements, you'll get an exception since the array is null.
You are missing following code somewhere and should be getting null pointer exception:
TardyEvenEmorys = new string[22];
Related
var temp = toCheck.GetData(DataFormats.FileDrop);
I have the code above in my program. toCheck is an IDataObjecct containing a file(image to be specific) path.
When I debug, I see the value stored under as such:
temp -> {string[1]}
[0] -> "C:\....rest of path"
Everything is correct but when I try to access the string inside, I cannot. If I use the toString() method it returns System.String[] but not the filepath.
Any suggestions on how I can retrieve the filepath inside of the variable?
temp -> {string[1]} Means that temp is an array of type string with one element.
[0] -> "C:\....rest of path" Means that the element at index 0 is equal to the path.
Since GetData returns the string array boxed as an object, you'll also have to unbox it first. Then you can simply access that element like this:
string[] strArr = temp as string[];
string s = temp[0];
temp is an array of strings, that is why you are getting that. If you want to get the first string of the array then:
var mystring = temp[0];
That should do
Also you can check if the string[] has any string with:
if(temp.Length > 0) myString = temp[0];
where myString is a variable of type string
I found the answer!
string[] arr = ((IEnumerable)temp).Cast<object>().Select(x => x.ToString()).ToArray();
Using the above statement you can go from type object to an array of string.
You need to have included using.System.Linq for it to work.
var temp = toCheck.GetData(DataFormats.FileDrop);
string[] arr = ((IEnumerable)temp).Cast<object>().Select(x => x.ToString()).ToArray();
string path = "";
foreach(string a in arr)
path = a;
This ended up being all the code if anybody is curious. I iterate over the array because otherwise it would skip the rest of the function when I debugged. Not sure what was causing this but this works. Thank you all for your help!
The strings I try to initialize with temp.Split results are always null.
G.ReadLine() is simply a "name%path" format. I also changed the encoding to unicode for certainty that there is no encoding difference between file and program.
Here is the relevant fragment:
StreamReader g = new StreamReader(path + "database.txt",Encoding.Unicode);
do
{
String temp;
temp = g.ReadLine();
//wr.WriteLine(temp);
try
{
names[ii] = temp.Split('%')[0];
Thank you for help
So now we found the route of the problem.
The instantiation of names was wrong.
Instead of
string[] names = null;
Something like
string[] names = new string[5];
must be used. The Problem here is that you have to now before how many strings the array will contain.
I recommand you to use an List of strings so something like that:
List<string> names = new List<string>();
and then use it with:
names.Add(temp.Split('%')[0]);
"name%path".Split('%')[0] returns "name"
So the problem is not Split function, but in temp which value does not have "name%path" pattern.
I use reflection to iterate over my object fields. To read a field value I use
object elementValue = element.GetValue(value)
because I don't know what kind of type I will get. My object has also a field of type char[]. When I read it using GetValue(value) I receive a variable of type object{char[]}. I would like to convert it into char[]. But how can I do it? I cannot iterate over it.
Is this what you're looking for?
char[] array = (char[])elementValue;
{char[]} is not a type. Debugger displays it for convenience. It is actually char[] only. So just a cast is enough.
For example following code will be displayed as {string[]} in debugger.
object elementValue = new string[] { "asdfasd" };
Just do
char[] arr = (char[])elementValue;
I'm putting a concatenated string into the Tag property of a component this way:
Tag = String.Format("{0};{1};{2}", AThis, AThat, ATheOtherThing);
Now how do I get it out, as Tag is an object? Trying to do this:
String[] someStuff = Tag.Split(';');
I get, "'object' does not contain a definition for 'Split' and no extension method 'Split' accepting a first argument of type 'object' could be found
The type of Tag is object but the Split method is on String. You need to cast Tag back to String in order to call Split
string[] someStuff = ((string)Tag).Split(';');
As object can be cast to and from any other data type, you can skip the string.Format() completely, and assign a string[]
Tag = new string[] { AThis, AThat, ATheOtherThing };
and
string[] someStuff = (string[])Tag;
or use object[] if AThis, AThat, ATheOtherThing are different data types.
Unless you have some driving need for it as a string another way would be a struct that held your three values, then just set tag to it, and to get it back cast it. No more formating and splitting then. More importantly if you add a fourth item, refactor the struct, job done.
A safe way to convert the Tag back to string is to use the as keyword. If the Tag contains something else than a string it does not throw an exception but returns null
string s = Tag as string;
string[] someStuff = null;
if (s != null) {
someStuff = s.Split(';');
}
I have the code below.
The line string content = twitterMsg.text; is creating the error 'Use of unassigned local variable' for twitterMsg. I don't seem able to access my TwitterSearchResponse.results.text fields in my DataContractJsonSerializer<TwitterMain> collection.
TwitterSearchResponse.results is an array (set of object properties) with several string fields attached with names like text and user_info.
Can anyone help with this??
Updated code below. I am still highly confused about why I am not able to iterate over my TwitterSearchResponse.results properly and assign content = twitterMsg.text
For what it's worth, here is my DataContractJsonSerializer method:
String url = String.Format("http://search.twitter.com/search.json?q={0}&rpp=20", Server.UrlEncode(txtSearchFor.Text));
// parse the JSON data
using (MemoryStream ms = new MemoryStream(wc.DownloadData(url)))
{
DataContractJsonSerializer jsonSerializer =
new DataContractJsonSerializer(typeof(TwitterMain));
TwitterSearchResponse = jsonSerializer.ReadObject(ms) as TwitterMain; // read as JSON and map as TwitterOut
}
And here is the original posted code where the issue lies.
public List<MatchCollection> returnMatches(DataContractJsonSerializer<TwitterMain> TwitterSearchResponse)
{
List<MatchCollection> messageLinks = new List<MatchCollection>();
foreach (TwitterResult twitterMsg in TwitterSearchResponse.results)
{
string content = twitterMsg.text;
// capture internet protocol pre-fixed words from message
string pattern = #"...";
messageLinks.Add(Regex.Matches(content, pattern, RegexOptions.IgnoreCase));
// capture #username twitter users from message
string atUsernamePattern = #"#([a-zA-Z0-9-_]+)";
MatchCollection PeopleMatches = Regex.Matches(content, atUsernamePattern, RegexOptions.IgnoreCase);
}
return messageLinks;
}
I suspect it's actually reporting the use of the unassigned local variable MessageLinks. Your use of twitterMsg looks fine.
So, the big question is: what do you want to return if there aren't any results? If you're happy returning null, just assign the value when you declare MessageLinks.
Next question: do you really only want to return the last MatchCollection you find? That's what the current behaviour is: you're looping over all the variables, setting the same local variable each time (i.e. replacing the previous value) and then returning that last value.
Final question: any reason why you've got a camel-cased method name (returnMatches), a Pascal-cased local variable (MessageLinks), a Pascal-cased parameter name (TwitterSearchResponse) and a camel-cased property (text)? I would assume that text is due to it coming from JSON that way - but it's a good idea to follow normal .NET naming conventions otherwise.