I am trying to output to a text file, with the following C# code. The problem is that my outputted information has a comma at the end of it and this won't work with the program that uses the file after. I'm trying to figure out how to get rid of this comma...
var toFile = Path.Combine(GetTextPath(),
string.Format(heatname + "_{0}.txt", DateTime.Now.ToString("yyyyMMdd")));
string ElementsNum = RoundedValues.Count.ToString();
DateTime dt = System.DateTime.Now;
var year = dt.ToString("yy");
var month = dt.ToString("MM");
var day = dt.ToString("dd");
var minute = dt.ToString("mm");
using (var fs = File.OpenWrite(toFile))
using (TextWriter sw = new StreamWriter(fs))
{
sw.Write("NA" + "," + dt.Hour.ToString() + "," + minute + "," + day + ","
+ month + "," + year + "," + "ALTEST " + "," +
"ALTEST " + "," + heatgrade + " " + "," + " " + "," + heatname + "," +
DT2.Rows[0][3].ToString() + "," + heatgrade + "," + "OE2" + "," + "," +
"," + "," + "," + "," + "," + " " + ElementsNum);
foreach (var pair in RoundedValues.Zip(Elements, (a, b) => new { A = a, B = b }))
{
sw.Write(pair.B.ToString() + ", " + pair.A.ToString() + ",");
}
}
You can use TrimEnd, for example:
var theString = "abcd,";
var trimmedString = theString.TrimEnd(new[]{','});
In your case, if I'm not mistaken, this is where you want it to happen:
sw.Write(pair.B.ToString() + ", " + pair.A.ToString() + ",");
If so, you can do this:
var pairs = pair.B.ToString() + ", " + pair.A.ToString() + ",";
sw.Write(pairs.Trim().TrimEnd(new[]{','}));
Here is a linqy way to do it. This would use the Aggregate function of linq.
var x = RoundedValues.Zip(Elements, (a, b) => new { A = a, B = b })
.Aggregate("", (old, item) => {
return old + (old == "" ? "" : ", ") +
item.B.ToSTring() + ", " + item.A.ToString();
});
sw.Write(x);
Version two (go go join!) uses linq to make a array of strings containing the pairs and then combine those pairs seperated by a comma using join.
string [] x = RoundedValues.Zip(Elements,
(a, b) => b.ToSTring() + ", " + a.ToString() ).ToArray();
sw.Write(String.Join(", ",x));
It might be that the following would work, but I'm not where I can test it ... this sure looks sexy (mostly because it is one line and everyone loves one line solutions):
sw.Write(String.Join(", ",
RoundedValues.Zip(Elements,
(a, b) => b.ToSTring() + ", " + a.ToString() )
));
Which would replace
foreach (var pair in RoundedValues.Zip(Elements, (a, b) => new { A = a, B = b }))
{
sw.Write(pair.B.ToString() + ", " + pair.A.ToString() + ",");
}
Another option is to use the StringBuilder.
...
StringBuilder sb = new StringBuilder();
foreach (var pair in RoundedValues.Zip(Elements, (a, b) => new { A = a, B = b }))
{
sb.AppendFormat("{0}, {1},", pair.B, pair.A);
}
sw.Write(sb.ToString().TrimEnd(new[] { ' ', ',' });
Related
How to obtain the type information of "t"?
How to get the type name of t
class Test
{
void main()
{
var t = new Test();
t.Equals(null);// object.Equals
//How do I get the type of "t" ?
}
}
private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
{
var invocationExpr = (InvocationExpressionSyntax)context.Node;
var memberAccessExpr = invocationExpr.Expression as MemberAccessExpressionSyntax;
if (memberAccessExpr == null)
return;
if (memberAccessExpr.Name.ToString() != nameof(object.Equals))
return;
var memberSymbol = context.SemanticModel.GetSymbolInfo(memberAccessExpr).Symbol as IMethodSymbol;
if (memberSymbol == null)
return;
_ = memberSymbol.ContainingType.SpecialType;// Object
}
I don't know why, but I cannot enter the breakpoint when debugging with vs.
So I can only debug in this way, but I can't find the correct method.
var str = memberSymbol.ContainingSymbol?.Name + Environment.NewLine
+ memberSymbol.AssociatedSymbol?.Name + Environment.NewLine
+ memberSymbol.ContainingType?.Name + Environment.NewLine
+ memberSymbol.ContainingType?.SpecialType + Environment.NewLine
+ memberSymbol.ContainingType?.ContainingType?.Name + Environment.NewLine
+ memberSymbol.ContainingType?.TypeKind + Environment.NewLine
+ memberSymbol.ContainingType?.OriginalDefinition?.Name + Environment.NewLine
+ memberSymbol.ContainingType?.BaseType?.Name + Environment.NewLine
+ memberSymbol.ContainingType?.MetadataName + Environment.NewLine
+ memberSymbol.ContainingType?.ContainingAssembly?.Name + Environment.NewLine
+ string.Join(" ", memberSymbol.ContainingType?.MemberNames ?? Enumerable.Empty<string>()) + Environment.NewLine
+ "***************" + Environment.NewLine
+ invocationExpr.FullSpan.ToString() + Environment.NewLine
;
File.WriteAllText(#"xxxxxxx", str);
I want to obtain "Test" type information or type string
I am writing an console application which writes to a text file.
I have written the code but my output doesn't give me what I need.
The output I get is as follows:
KCooke409155874935sa975891/1/2013
and it should be like this:
KCooke 409155874935 sa 97589 1/1/2013
The code I have is as follows:
foreach (var account in sortedAccounts)
{
var outputLine =
account.accountholder +
account.accountnumber +
account.accounttype.Substring(0, 2) +
account.amount +
account.date.ToShortDateString();
//output
File.WriteAllText(text, outputLine);
}
is it possible to split by a tab. I tried using .Split() but I get errors.
Thank you
You don't need splitting here, you are looking for join
string.Join("\t", account.accountholder,
account.accountnumber,
account.accounttype.Substring(0, 2),
account.amount,
account.date.ToShortDateString());
Adding the tab character should so what you need
foreach (var account in sortedAccounts)
{
var outputLine =
account.accountholder + "\t" +
account.accountnumber + "\t" +
account.accounttype.Substring(0, 2) + "\t" +
account.amount + "\t" +
account.date.ToShortDateString();
//output
File.WriteAllText(text, outputLine);
}
Insert a Tab character '\t' like this:
var outputLine = account.accountholder + "\t" +
account.accountnumber + "\t" +
...
Try this:
foreach (var account in sortedAccounts)
{
var outputLine = string.Format("{0}\t{1}\t{2}\t{3}\t{4}",
stringaccount.accountholder,
account.accountnumber,
account.accounttype.Substring(0, 2),
account.amount,
account.date.ToShortDateString());
//output
File.WriteAllText(text, outputLine);
}
You haven't actually included a tab character in your string...
var outputLine =
account.accountholder + "\t" +
account.accountnumber + "\t" +
account.accounttype.Substring(0, 2) + "\t" +
account.amount + "\t" +
account.date.ToShortDateString();
I would like to add multiple maps of the Google Maps API to a page of my C# ASP.NET application. However, with my current code, only the last map is shown. All the other divs that should contain a map are empty. As you can see in my code, the maps already have unique names (not on a nice way, but it is unique). Can someone explain what I am doing wrong?
It would be really appreciated.
Initialize Google:
protected void loadGoogle()
{
var googleScript = "function loadScript() {" +
"var script = document.createElement(\"script\");" +
"script.type = \"text/javascript\";" +
"script.src = \"http://maps.google.com/maps/api/js?sensor=false& callback=initialize\"; " +
"document.body.appendChild(script);" +
"}" +
"window.onload = loadScript;";
ClientScript.RegisterStartupScript(typeof(Page), "CreateGoogleMapScript", googleScript, true);
}
Create maps (called multiple times):
protected void createMap(String mapId, String address)
{
var script = "function initialize() {" +
"var geocoder = new google.maps.Geocoder();" +
"var latlng = new google.maps.LatLng(-34.397, 150.644);" +
"var mapOptions" + mapId + " = {" +
"zoom: 16," +
"center: latlng," +
"mapTypeId: google.maps.MapTypeId.ROADMAP" +
"};" +
" var map" + mapId + " = new google.maps.Map(document.getElementById(" + mapId + "), mapOptions" + mapId + ");" +
"var address = \"" + address + "\";" +
"geocoder.geocode( { 'address': address}, function(results, status) {" +
"if (status == google.maps.GeocoderStatus.OK) {" +
"map" + mapId + ".setCenter(results[0].geometry.location);" +
"var marker = new google.maps.Marker({" +
"map: map" + mapId + "," +
"position: results[0].geometry.location" +
"});" +
"} else {" +
"alert(\"Geocode was not successful for the following reason: \" + status); " +
"}" +
"});" +
"}";
ClientScript.RegisterClientScriptBlock(typeof(Page), "CreateMapScript"+mapId, script, true);
}
Called by:
While loop, with a literal.Text that adds a new div, followed by createMap(mapId, address);
UPDATE:
My new code:
var script = "function initialize() { var geocoder = new google.maps.Geocoder();";
for (int i = 0; i < maps.Count; i++)
{
String currentId = maps[i] as String;
String currentLocation = locations[i] as String;
script += " " +
"var latlng = new google.maps.LatLng(" + 500 + "," + 400 + ");" +
"var myOptions = {" +
"zoom: 16," +
"center: latlng," +
"mapTypeId: google.maps.MapTypeId.ROADMAP" +
"};" +
"var map = new google.maps.Map(document.getElementById(\"" + currentId + "\"), myOptions);" +
"var address = \"" + currentLocation + "\";" +
"geocoder.geocode( { 'address': address}, function(results, status) {" +
"if (status == google.maps.GeocoderStatus.OK) {" +
"map.setCenter(results[0].geometry.location);" +
"var marker = new google.maps.Marker({" +
"map: map," +
"position: results[0].geometry.location" +
"});" +
"} else {" +
"alert(\"Geocode was not successful for the following reason: \" + status); " +
"}" +
"});";
}
script += "};";
Now all maps are drawn, but only the last map has the right location. The other ones are based on the latlng variable. Why can't the geocoder be used multiple times?
Perhaps just the last initialize function is firing in the browser? Try having just 1 initialise function that initialises all your maps.
string res1 = "CalcAPI.GetXElementValue(" + "\"" + res[0] + "\"" + "," + "\"" + res[1] + "\"" + "," + false + ")"
in
res[0] = "IRS5555"
res[1] = "IRS001"
Output of this code is:
CalcAPI.GetXElementValue("IRS5555","IRS001",False)
but I want
CalcAPI.GetXElementValue("IRS5555","IRS001",false)
false in lower case.
Sorry for not providing full code...
false is not static...
public static object GetElementDataType(string DataType)
{
///Write here methos for fetching data type of current element.
object res = null;
switch (DataType.ToLower())
{
case "int":
res = 0;
break;
case "double":
res = 0.0;
break;
case "string":
res = "";
break;
case "myboolean":
res = false;
break;
default:
res = "";
break;
}
return res;
}
string res1 =
"CalcAPI.GetXElementValue(" + "\"" + res[0] + "\"" + ","
+ "\"" + res[1] + "\"" + ","
+ GetElementDataType("myboolean") + ")"
then result is
CalcAPI.GetXElementValue("IRS5555","IRS001",False)
but i want
CalcAPI.GetXElementValue("IRS5555","IRS001",false)
if i pass double
string res1 = "CalcAPI.GetXElementValue(" + "\"" + res[0] + "\"" + "," + "\"" + res[1] + "\"" + "," + GetElementDataType("double") + ")"
then result is
CalcAPI.GetXElementValue("IRS5555","IRS001",0)
but i want
CalcAPI.GetXElementValue("IRS5555","IRS001",0.0)
if i pass string
string res1 = "CalcAPI.GetXElementValue(" + "\"" + res[0] + "\"" + "," + "\"" + res[1] + "\"" + "," + GetElementDataType("string") + ")"
then result is
CalcAPI.GetXElementValue("IRS5555","IRS001",)
but i want
CalcAPI.GetXElementValue("IRS5555","IRS001","")
If your false is constant there, then you can use simple string. And it`s better to use string format:
string res1 = string.Format("CalcAPI.GetXElementValue(\"{0}\",\"{1}\",false)", res[0], res[1]);
Use false.ToString().ToLower().
try with
false.ToString().ToLower()
use ToLower() function like this
string res1 = "CalcAPI.GetXElementValue(" + "\"" + res[0] + "\"" + "," + "\"" + res[1] + "\"" + "," + false.ToString().ToLower() + ")"
Is there a more efficient way to handle this?
List<String> lstReferences = (from f in
(from section in courseSectionToCreate.SectionsToAdd
select new {
ReferenceNumber = section.Course.CourseNumber.Substring(0, 5) + "." +
section.Course.CourseNumber.Substring(5) + "." +
section.Session + "." +
section.Year + "." +
section.SectionNumber + ";"
})
select f.ReferenceNumber).ToList();
strReferenceNumber = lstReferences.Aggregate((a, b) => a + ", " + b);
Yes, you definitely don't want to be using Aggregate here. That is O(n^2) (it's Schlemiel the Painter's algorithm). Instead:
string referenceNumber = String.Join(", ", lstReferences);
This is better because String.Join will use a StringBuilder internally.
You can replace all that with this:
var strReferenceNumber =
String.Join(", ",
courseSectionToCreate.SectionsToAdd.Select(s =>
String.Join(".",
s.Course.CourseNumber.Substring(0, 5),
s.Course.CourseNumber.Substring(5),
s.Session,
s.Year,
s.SectionNumber) + ";"
)
);
How about:
var lstReferences = from section in courseSectionToCreate.SectionsToAdd
let courseNumber = section.Course.CourseNumber
let toJoin = new object[]
{
courseNumber.Substring(0, 5),
courseNumber.Substring(5),
section.Session,
section.Year,
section.SectionNumber
}
select string.Join(".", toJoin) + ";"
var strReferenceNumber = string.Join(", ", lstReferences);