String[] vals = s.Split(';');
String o = "X=" + vals[0] + " Y=" + vals[1] + " Z=" + vals[2];
var xValue = vals[0];
var yValue = vals[1];
var zValue = vals[2];
I want to use my variable "xValue, yValue, zValue" on another page. How do I make it public for me to use it.
This is the public class I created
public class CommonData
{
public static string o = string.Empty;
public static void SetData(string s)
{
String[] vals = s.Split(';');
o = "X=" + vals[0] + " Y=" + vals[1] + " Z=" + vals[2];
}
}
Making them public will solve your issue. You can access them further using object of that class.
I simulated your case in a console application. Declare those variable outside of method.
class Program
{
public static string xValue;
public static string yValue;
public static string zValue;
static void Main(string[] args)
{
string s = "fdf;fdfd;fdf";
string[] vals = s.Split(';');
string o = "X=" + vals[0] + " Y=" + vals[1] + " Z=" + vals[2];
xValue = vals[0];
yValue = vals[1];
zValue = vals[2];
Console.ReadLine();
}
}
Related
I want the the timeToString values change based on the count changing.So when the count of seconds decrement the second.Count.ToString("59") should decrement to "58".In the debug the PondoreCount function is working as expected, however I can't see the value decrementing in the console.
public class PonodoreTimer : Clock
{
private Clock _seconds;
private Clock _minutes;
public PonodoreTimer()
{
_seconds = new Clock();
_minutes = new Clock();
_seconds.Count = 59;
_minutes.Count = 24;
// string timeToString = _minutes.Count.ToString("24") + ":" + _seconds.Count.ToString("59");
}
public string PondoreTimerInString()
{
string timeToString = _minutes.Count.ToString("24") + ":" + _seconds.Count.ToString("59");
return timeToString;
}
public void PonodoreCount()
{
//string timeToString = _minutes.Count + ":" + _seconds.Count;
//Console.WriteLine(timeToString);
while (_minutes.Count != 0)
{
_seconds.Decrement();
if(_seconds.Count == 0)
{
_minutes.Decrement();
_seconds.Count = 59;
}
}
}
}
The whole thing can be replaced by:
var ts = TimeSpan.FromMinutes(25);
var one = TimeSpan.FromSeconds(1);
while(ts > TimeSpan.Zero){
ts = ts - one;
Console.WriteLine($#"{ts:mm\:ss}");
}
24:59, 24:58 ...
If you want it to count down from 25:00, put the WriteLine before the subtract
Change this string
string timeToString = _minutes.Count.ToString("24") + ":" + _seconds.Count.ToString("59");
to this
string timeToString = _minutes.Count.ToString() + ":" + _seconds.Count.ToString();
or you always get "24" and "59"...
I have List as mentioned below. Now When I am going to add new string value into this list, My method GetNewCopiedValue has to check the new text value into the list lstNames.If the name does not exist in the list it should return the value as it is. If the new string value is exist already in the list, it has to return the string with respective index number as like EC(1).For example, If I am sending EC(1) again to the list, the method has to check the value in the list and It should return EC(3) since EC(1) is exist already in the list, the method has to check the similar values and it should return the value with next index number which is not there in the list.
Main()
{
List<string> lstNames=new List<string>{"Ecard","EC","EC(1)","EC(2)","NCard(1)"};
var copiedValue= GetNewCopiedValue(lstNames,EC(2));
Console.WriteLine("Copied Text is :"+copiedValue);
}
public static string GetNewCopiedValue(List<string> lstNames,string sourceLabel)
{
label = sourceLabel;
if (lstNames.Any(i => i.Equals(sourceLabel)))
{
var labelSubstring = sourceLabel.Substring(0, sourceLabel.Length - 2);
var sameNameList = lstNames.Where(i => i.Contains(labelSubstring)).ToList();
int count = sameNameList.Count+1;
label = labelSubstring + count + ")";
while (lstNames.Any(i => i.Equals(label)))
{
var indexLabel = sourceLabel.Substring(sourceLabel.Length-2,1);
var maxIndex = sameNameList.Max(i =>int.Parse( i.Substring(sourceLabel.Length - 2, 1)));
int labelCount = maxIndex + 1;
label = labelSubstring + labelCount + ")";
}
}
return label;
}
Actual Input:
EC(2)
Expected output:
EC(3)
I have tried with some logic but it was not working with all input strings. It worked for few cases only.
Please help me on this.
https://dotnetfiddle.net/dFrzhA
public static void Main() {
List<string> lstNames= new List<string>{"Ecard","EC","EC(1)","EC(2)","NCard(1)"};
var copiedValue= GetNewCopiedValue(lstNames, "EC(1)");
Console.WriteLine("Copied Text is :" + copiedValue);
}
public static string GetNewCopiedValue(List<string> lstNames, string ValueToCopyInList) {
string newName;
if (!lstNames.Contains(ValueToCopyInList)) {
newName = ValueToCopyInList;
} else {
int? suffix = ParseSuffix(ValueToCopyInList);
string baseName = suffix == null ? ValueToCopyInList : ValueToCopyInList.Substring(0, ValueToCopyInList.LastIndexOf('('));
suffix = suffix ?? 1;
newName = baseName + "(" + suffix + ")";
while (lstNames.Contains(newName)) {
suffix++;
newName = baseName + "(" + suffix + ")";
}
}
lstNames.Add(newName);
return newName;
}
public static int? ParseSuffix(string value) {
int output;
if (string.IsNullOrEmpty(value)) return null;
if (!value.EndsWith(")")) {
return null;
}
var idxStart = value.LastIndexOf('(');
var strResult = value.Substring(idxStart + 1, value.Length - (idxStart + 2));
if (int.TryParse(strResult, out output))
return output;
return null;
}
I have this Tokinizer Class that breaks up a string input:
public class Tokinizer
{
public static Regex r = new Regex(
"(?<Equals>=)" +
"|(?<Plus>\\+)" +
"|(?<Minus>\\-)" +
"|(?<Divide>\\/)" +
"|(?<Multiply>\\*)" +
"|(?<Exclamation>\\!)" +
"|(?<GreaterThan>\\>)" +
"|(?<SmallerThan>\\<)" +
"|(?<OpenParenthesis>\\()" +
"|(?<CloseParenthesis>\\))" +
"|(?<OpenBracket>\\[)" +
"|(?<CloseBracket>\\])" +
"|(?<OpenBrace>\\{)" +
"|(?<CloseBrace>\\})" +
"|(?<Colon>\\:)" +
"|(?<SemiColon>\\;)" +
"|(?<Comma>\\,)" +
"|(?<FullStop>\\.)" +
"|(?<Quatation>\\\")" +
"|(?<Char>[a-zA-Z0-9])" +
"|(?<space>\\s+)", RegexOptions.ExplicitCapture);
public static void GetTokens(string input)
{
foreach (var t in r.Matches(input))
{
Console.WriteLine("Named Group : Token Value");
}
}
I want to print out the the name of the capture group aswell as the value from a list of matches, is this possible to do?
For example when I give the input "var++" it should output:
Char : v
Char : a
Char : r
Plus : +
Plus : +
You can use Regex.GroupNameFromNumber
public static void GetTokens(string input)
{
foreach (Match match in r.Matches(input))
{
for (int i = 1; i < match.Groups.Count; i++)
{
var group = match.Groups[i];
if (group.Success){
Console.WriteLine("{0} : {1}", r.GroupNameFromNumber(i), match);
break;
}
}
}
}
FIDDLE
Utility Function:
public static string GetProperties<T>(string alias="")
{
if (alias.Length>0)
{
return typeof(T).GetProperties().Select(x => x.Name).Aggregate((x, y) =>
alias + " = " + alias + "." + x + "," + Environment.NewLine + alias + " = " + alias + "." + y + ",");
}
else
{
return typeof(T).GetProperties().Select(x => x.Name).Aggregate((x, y) => x + Environment.NewLine + y);
}
}
Code:
public class ContainerInLog
{
public int ContainerInLogID { get; set; }
public int ContainerInID { get; set; }
public int CargoID { get; set; }
public int LoadStatus { get; set; }
}
public static void Main()
{
string list = GetProperties<ContainerInLog>("y");
Console.WriteLine(list);
}
Result:
y = y.y = y.y = y.ContainerInLogID,
y = y.ContainerInID,,
y = y.CargoID,,
y = y.LoadStatus,
Expected Result:
ContainerInLogID = y.ContainerInLogID,
ContainerInID = y.ContainerInID,
CargoID = y.CargoID,
LoadStatus = y.LoadStatus,
If you are really stuck on returning the entire concatenated string instead of returning an enumerable of them, I wouldn't use Aggregate here, just use string.Join. Also, you can simplify the statement by crafting the string inside the Select. For example:
return string.Join(
Environment.NewLine,
typeof(T)
.GetProperties()
.Select(x => $"{x.Name} = {alias}.{x.Name},"));
Bonus: If you change the separator to $",{Environment.NewLine}" you can remove the inline comma and you won't get the final comma on the end of your string (example fiddle).
#DavidG has the nicer solution here, but whats going wrong with your aggregation is the following:
.Aggregate((x, y) =>
alias + " = " + alias + "." + x + "," + Environment.NewLine + alias + " = " + alias + "." + y + ",");
The Aggregate selector function (your (x,y)) takes the following form:
Func<TAccumulate,TResult> resultSelector
That means in your case, x is the accumulated aggregate result already, eg "ContainerInLogID = y.ContainerInLogID".
But to make the next aggregate, you transform x again: alias + " = " + alias + "." + x, making "y = y.y = y.ContainerInLogID". And so on for each following property, each one adding another prefix of "y = y.".
Hi all I am using this function to get name's of the input fields from browser. Problem is that in couple of my sites input fields have the same position, so i cant cycle thrue them correctly. Any ideas how to do this cycle in some different way as thrue position?
Thank you.
public void hladame_fieldy ()
{
//fieldy
string nazov_fieldu;
decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[#type='text']");
string field = "#type='text'";
int b = 1;
for (b = 1;b<=celkovy_pocet_fieldov;b++)
{
nazov_fieldu = selenium.GetAttribute("xpath=//input[position()="+b+" and "+field+"]#name");
Console.WriteLine(nazov_fieldu);
}
Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
}
Since you have the amount of elements you can just go through them as an array
public void hladame_fieldy ()
{
//fieldy
string nazov_fieldu;
decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[#type='text']");
string field = "#type='text'";
int b = 1;
for (b = 1;b<=celkovy_pocet_fieldov;b++)
{
nazov_fieldu = selenium.GetAttribute("xpath=//input[" + b + "]#name");
Console.WriteLine(nazov_fieldu);
}
Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
}
That way you just go through all the input elements in the DOM from top to bottom.
final solution:
public void hladame_fieldy ()
{
//fieldy
string nazov_fieldu;
decimal celkovy_pocet_fieldov = selenium.GetXpathCount ("//input[#type='text']");
int b = 1;
string pomoc = "";
for (b = 1;b<=celkovy_pocet_fieldov;b++)
{
nazov_fieldu = selenium.GetAttribute("xpath=//input[#type='text'" + pomoc +"]#name");
pomoc = pomoc + " and #name!= '" + nazov_fieldu + "'";
Console.WriteLine(nazov_fieldu);
}
Console.WriteLine ("Celkovy pocet fieldov je = " + celkovy_pocet_fieldov);
}