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(';');
}
Related
what does out mean in a multidimensional param, if it's possible write an eg please, I'll really appreciate
public string GetTeamsInfo(out string[][] teams)
{
...
}
out means the parameter is an {out}put parameter
opposed to the default input parameter (note: an input parameter is not the same as a parameter using the in modifier, that is an input only parameter and imposes it own requirements)
this means it has no value at the beginning of the function and you need to assign one for it to be passed back to the calling code
see https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/out-parameter-modifier
public string GetTeamsInfo(out string[][] teams)
{
teams = new string[3][4];
return "test";
}
string[][] teams;
//here teams is null
string text = GetTeamsInfo(out teams);
//here teams is a array of string[]
I've defined List
private class Kamery
{
public int iIndeks;
public string strNazwa;
public Kamery(int Indeks, string Nazwa)
{
iIndeks = Indeks;
strNazwa = Nazwa;
}
}
List<Kamery> lKamery = new List<Kamery>();
I'd like to cast searched list of names to string array like:
string[] strNazwa = (string)lKamery.Find(item => item.iIndeks == iIndeks).strNazwa.ToArray();
But compiler says Cannot convert type 'char[]' to 'string'
Why? How it needs to be done?
I think you want:
string[] strNazwa = lKamery.Where(item => item.iIndeks == iIndeks)
.Select(item => item.strNazwa)
.ToArray();
That will give you a string array that contains each of the strNazwa values from the list for items that meet the Where condition.
Here's what your original code was doing:
string[] strNazwa = (string)
// get the one item that matches the condition
lKamery.Find(item => item.iIndeks ==Indeks)
// get the strNazwa property from that one item
.strNazwa
// return the string as a char array
.ToArray();
When you try to cast the char[] to a string it fails since you can't cast it. You can create a string from a character array but not via a cast.
I think your problem is that .Find returns only 1 value , the first match in the list.
https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.110).aspx
This value will be a string and by using .toArray , you are converting that string to a char[ ] and then trying to cast it back to string.
I'm not that good at c# , so the generic solution would be:
Declare the array, do a foreach and every time the id matches put the name into the array and inc the index. This limits it somewhat as you have to have a fixed size, would probably be better to use List instead.
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 am trying to read a string into an array and I get the error "Cannot implecitly convert type 'string' to 'string[]'.
The error occurs here:
string[] sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();
My full if else statement is below:
if (!string.IsNullOrEmpty(result.Tables[0].Rows[0].Field<string>("WebHTML")))
{
string[] sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();
NewsContent.Text = sepText[1];
if (!string.IsNullOrEmpty(sepText[0]))
Image1.ImageUrl = sepText[0];
else
Image1.Visible = false;
NewsTitle.Text = String.Format("{3}", Extensions.GetServerName(true), result.Tables[0].Rows[0].Field<int>("News_Item_ID"), result.Tables[0].Rows[0].Field<string>("Title").UrlFormat(), result.Tables[0].Rows[0].Field<string>("Title"));
Hyperlink1.NavigateUrl = String.Format("{0}/news/{1}/{2}.aspx", Extensions.GetServerName(true), result.Tables[0].Rows[0].Field<int>("News_Item_ID"), result.Tables[0].Rows[0].Field<string>("Title").UrlFormat());
}
else
{
Hyperlink1.Visible = false;
Image1.Visible = false;
}
Thank you for your help!
EDIT Code for URL Decode:
public static string UrlDecode(this string str)
{
return System.Web.HttpUtility.UrlDecode(str);
}
result.Tables[0].Rows[0].Field<string>("WebHTML") is going to give you the value of the WebHTML field in the first row in the first table which is a single string rather than a string[].
You may want to show your code for UrlDecode() since it looks like a custom implementation rather than one of the built-in framework versions.
You also declare the UrlDecode method to take a string as a parameter and return a string. Remember, a string is not the same thing as a string array.
It seems that you are trying to put:
result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();
which returns a string, into an array of strings.
Simply delare your sepText variable as a string rather than a string array and you should be good to go, e.g.:
string sepText = result.Tables[0].Rows[0].Field<string>("WebHTML").UrlDecode();
Later in your code you will clearly need to read the contents of the string like this:
Image1.ImageUrl =sepText;
Assuming the UrlDecode you are using is the one from here then the result is a string and not a string[] !
UrlDecode returns a string and you are assigning it to an array.
If you want the parts you will have to use the string to create an Url object.
Url url = new Url(result.Tables[0].Rows[0].Field<string>("WebHTML"));
and then get the parts.
See: Get url parts without host
I don't think URLDecode works the way you think it works. All URLDecode does is remove URL encoding from a string. It does not return an array of strings - only the decoded value of the string you gave it.
http://msdn.microsoft.com/en-us/library/system.web.httputility.urldecode.aspx
Example: Your web browser replaces a space with %20. This changes the %20 back to a space.
That's because the result of this line is "string" and you're trying to assign it to an array since UrlDecode do not produce an array. What you probably wanted is to use a method split() to create an array of separators?
I"m developing a simple application that have a line like this:
string[] values = ReadAll(inputFile);
As inputFile is a string, but how I can do this without conflicts(Cannot implicitly convert type 'string' in 'string[]')?
Assuming your ReadAll method has a signature like this
string ReadAll(string inputFile);
then the problem is not with inputFile but with the return value of the method which cannot be assigned to a string[].
Are you maybe looking for File.ReadAllLines?
string[] values = File.ReadAllLines(inputFile);
Or do you want to split a string by some delimeter?
string[] values = ReadAll(inputFile).Split('\n');
Based on the exception message you gave us, ReadAll(inputFile) returns a string, and you assign it to a string[], so that's why it doesn't work.
This would work:
string input = ReadAll(inputFile);
After this do you want to split the strings in some way? We'd need more details to help you further.