Strange object returned when using content picker - c#

Im currently making usercontrol for umbraco that takes all Nodes indside a Node names "Features"...
And it all works perfekt, until i wanted to get content from the "Content Picker" property (named linkToPage).
When i tried to use GetProperty("linkToPage").Value, I got an error about it being an object.
So i then added it to a var and debugged, and saw it returned somthing a bit strange...
var linkIdVar = child.GetProperty("linkToPage");
Returns:
- linkIdVar {1081} umbraco.interfaces.IProperty {umbraco.NodeFactory.Property}
- [umbraco.NodeFactory.Property] {1081} umbraco.NodeFactory.Property
Alias "linkToPage" string
Value "1081" string
+ Version {00000000-0000-0000-0000-000000000000} System.Guid
+ Non-Public members
Alias "linkToPage" string
Value "1081" string
+ Version {00000000-0000-0000-0000-000000000000} System.Guid
And i can't seem to get the Value to a Int without getting error about it being an object...
So does anyone know how to get arround this, or know a better way to get the page from a Content Picker?

I think you might need to unbox the value to an integer, like so:
var val = (int) child.GetProperty("linkToPage").Value;
However, if the content of the property value is not an integer, but a string, as the debugging information seems to indicate, then you need to convert to an integer, like so:
var val = int.Parse(child.GetProperty("linkToPage").Value as string);

Related

How to change text dynamic in unity

at line 161,I want to insert my text in parameter t,but it won't change when i debug it.although the parameter tmp had alredy changed.
I want to change this Text in UI,when my parameter t changes.
With respect to your specific issue, Insert is defined as:
public string Insert (int startIndex, string value);
and returns a new string. In C#, strings aren't modified, new strings are created. In this way, they act like a value type, even though they're a reference type. In other words, once a string is created, it is never modified - it's 'immutable'. So, you need to store your newly created string.
In cases like this, I like to use the string interpolation, as it allows me to get a slightly clearer representation of what the final string will look like.
var tmp = System.Text.Encoding.UTF8.GetString ( e.Message );
t.text = $"{tmp}\n{t.text}"; // Note that a newline is represented as \n
Or, if you add the System.Text namespace; you could reduce it down to:
using System.Text;
...
t.text = $"{Encoding.UTF8.GetString ( e.Message )}\n{t.text}";
The string type in c# is immutable, therefore Insert returns a new string instead of modifying the current one.
Do:
t = t.text.Insert(0, tmp + "//n");
See also
How to modify string contents in C#

Parsing the value of an integer using a string?

I have Script2 which has int restaurant1Current, along with several other variables with similar names. On another script, I'm trying to parse the value of restaurant1Current using this code. category is a string with the value of "restaurant1".
string currentStringBuffer = "Script2." + category + "Current";
int currentNumber = int.Parse(currentStringBuffer);
I want to be able to use the same code as I change the value of category hence the need to parse it using a string instead of referencing the variable directly. Is this the correct syntax or am I missing something? As is the code tells me "The input string is not in the correct format" but when debugging it shows that the string contains the value "Script2.restaurant1Current" which again is an int. So why wouldn't I be able to parse the value to currentNumber in the next line?
public Enum Categories{
Restauraunt,
Grocer,
Bank
}
Enum stands for enumeration, it is a numbered list. with this particular enum,
Categories.Bank =2; so you can call or assign.
something.category=2;
or
int rando = Categories.Bank

C# - Converting from string to my object

In my code I load a text file and read it, looking for specific parameters. After I get them, I save them as string, and now I need to send them to a set-method, that only gets MyProperties type variables. ("MyProperties" is an enum class, and the method that receives the parameter needs to get an enum of this type)
So my question is: how can I convert them from string to MyProperties type?
Reading the file:
string input = File.ReadAllText("C:/avi/properties/" + propertiesFile);
After I get a parameter I need, I save it in a string var named mode.
string mode; // ---> Needs to be "MyProperties mode;"
vpa.Set(MyProperties.Mode, mode);
Solutions like checking if (mode.equals("string")) or "switch" are too long because there is a lot to check.
Been able to find an answer on another post, this is what i used:
TypeConverter converter = TypeDescriptor.GetConverter(type);
MyProperties prop = (MyProperties)converter.ConvertFrom(mode);

Setting string values to Viso shapedata

I have a visio shape with shape data column Prop.Name of type string
when i try to set its value using
Visio.Cell propCell = _shapeList[i].get_Cells("Prop.Owner");
propCell.FormulaForceU = "asd";
I get an error: #NAME?
This does not happen if i pass a numeric string .
How can i pass characters other than number ?
propCell.FormulaForceU = "\"asd\"";
I am trying to update the shape property value with the normal string as like 'Set Value' it gives error #NAME?.
After giving
c.Formula ="\"Set Value\"";
it begins to work.

Accessing DataContractJsonSerializer<T> properties via re-factored foreach loop

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.

Categories