In dotnet standard/core, how do I get the last Character of a string?
In dotnet Fx, we use
Char LastChar = MyString[MyString.Length - 1];
However, in dotnet standard (I'm using 2.0), this does not seem to be available...
See also questions close to this topic
-
Return child type from a method on an abstract class or force two constructors on child with different parameters types
I have an abstract class TypedIdValueBase which has subclasses like UserId or ImageId. I don't know how but I would like to have a Parse method (or a second constructor which is taking a string in parameter and not a Guid) and call it like that:
Guid valueG = Guid.NewGuid(); UserId userId1 = UserId.Parse(valueG ); //or new UserId(valueG ) ImageId imageId1 = ImageId.Parse(valueG ); // or new ImageId(valueG ) string valueS = "0f8fad5b-d9cb-469f-a165-70867728950e"; UserId userId2 = UserId.Parse(valueS ); //or new UserId(valueS ) ImageId imageId2 = ImageId.Parse(valueS ); // or new ImageId(valueS )
I would like to force each child class to have that method or a second constructor with a string parameter but I really don't know how to handle it. I know about some "simple" answers like that: Abstract Method That Returns an Instance of Derived Class
Someone already linked my previous question to another answer without reading well what I want to do. Pay attention to the type of the variables or parameters before to link and close it to an answer which is not answering the question. Thank you a lot.
The best I had was something like that:
string value = "0f8fad5b-d9cb-469f-a165-70867728950e"; UserId userId = (UserId)UserId.Parse(value); //or new TypedIdValueBase.Parse<UserId>(value)
Does anyone have an idea on how to do something like that ?
Here is the code without traces of my attempts:
public abstract class TypedIdValueBase : IEquatable<TypedIdValueBase> { public Guid Value { get; private set; } protected TypedIdValueBase(Guid value) { if (value == Guid.Empty) { throw new InvalidOperationException("Id value cannot be empty!"); } Value = value; } } public class UserId : TypedIdValueBase { public UserId(Guid value) : base(value) { } } public class ImageId : TypedIdValueBase { public ImageId(Guid value) : base(value) { } }
-
C# Adding items to Listview from List
I am trying to add items to a listwiew from a list. But my code doens't work properly. I mean there are no errors but items doesn't show up in the listwiew.
List<string> Yemekler = new List<string>(); List<string> Corbalar = new List<string>(); private void Label3_Click(object sender, EventArgs e) { Yemekler.Add("Karnıyarık"); Yemekler.Add("Köfte"); Corbalar.Add("Mercimek Çorbası"); Corbalar.Add("domato corba"); foreach (string yemek in Yemekler) { listView1.Items.Add(yemek); } foreach (string corba in Corbalar) { listView2.Items.Add(corba); } }
I hope you can help me ^^
-
How To Run A Process Command on Mac?
I'm trying to practice this Process class but it's been a flop since I could not figure out how to write the filenames and the paths correctly.
This is what I'm trying out but everytime I get the same error: Unhandled exception. System.ComponentModel.Win32Exception (2): No such file or directory
Process.Start("/Applications/Utilities/TextEdit.app/Contents/MacOS/TextEdit");
I tried different paths which were not much different from this one, but as I said idk what to do about this.
Do you know what's the accurate way of doing that?
-
JSON.NET deserialization of class containing a list of classes
So I'm trying to deserialize class "MyDoc" containing a list of classes (the problematic one's the list of MyPolyline class entities) that goes like that
class MyPoint2d { public double X { get; set; } public double Y { get; set; } [JsonConstructor] public MyPoint2d(double x, double y) { X = x; Y = y; } } class MyPolyline { public List<MyPoint2d> Points { get; set; } [JsonConstructor] public MyPolyline(List<Point2d> points) { Points = new List<MyPoint2d>(); foreach (Point2d p in points) { Points.Add(new MyPoint2d(p)); } } } class MyLine { public MyPoint2d StartPoint { get; set; } public MyPoint2d EndPoint { get; set; } [JsonConstructor] public MyLine(Point2d st, Point2d ed) { StartPoint = new MyPoint2d(st); EndPoint = new MyPoint2d(ed); } } class MyDoc { public List<MyLine> Lines { get; set; } public List<MyPolyline> Polylines { get; set; } }
(The Point2d class is that from AutoCad geometry but I'm using that "MyPoint2d" to limit the amount of properties that are being exported to json)
So after converting the file with standardMyDoc deserialized = JsonConvert.DeserializeObject<MyDoc>(jsonFileContent);
The list of MyLine classes after that is working fine with all the properies keeping the correct values yet reading through MyPolyline list returns a correct amount of entities with one problem which is all the points have properties X and Y equal to zero.
The json file itself is alright and I really can't come up with anything to get this thing to work. Just in case the json file structure is rather obvious but it looks like this:{ "Lines": [ { "StartPoint": { "X": 1594.9640653785937, "Y": 1490.1708760910014 }, "EndPoint": { "X": 1455.6957137348581, "Y": 1381.9184832054661 } } ], "Polylines": [ { "Points": [ { "X": 2155.1322935779444, "Y": 2022.1540617522687 }, { "X": 2291.3057975869833, "Y": 1728.326139136384 } ] } ] }
So the question would be - is there any good way to handle this?
-
"Delete Existing" is missing in VS 2019 publish for .NET Core Console app
While publishing to a folder, "Delete Existing" is not present for .NET Core Console app. But it exists when publishing an ASP.NET Core app..
Is it just me or it really is missing? Is it a feature or a bug?
-
How to de-serialize nested json into one object
{ "results": [ { "a": { "requestId": 1517, "whse": "501", "productKey": "1000", "crossKey": "1000", "crossAlt": "C9M", "crossLinebuy": "5299", "crossPercentage": 1.0000, "crossVendorAlpha": "NIBCO" }, "c": { "requestId": 1517, "whse": "501", "productKey": "1000", "productDescription": "3 WROT CXC 90 ELL 3-1/8 OD", "alt1": "C9M", "linebuy": "5299", "linebuyDescription": "WROT COP FTG 2-1/2\" & UP", "discountGroup": "0551", "discountGroupDescription": "WROT COP FTG 2-1/2 & UP", "newLinebuy": null, "demandChange": null, "buyQty": null, "futureReplenWhse": "501", "startDate": "2021-01-21T19:38:58.973", "endDate": "2022-01-21T19:38:58.973", "endDateOriginal": null, "requestMin": null, "requestMax": null, "unspsc": null, "leadTime": null, "upc": "039923313386", "vendorCode": "9056900", "vendorAlpha": "NIBCO", "listPrice": 277.20, "msds": null, "unNaId": null, "storageClass": null, "parcelFlag": "A", "gph": "15260701", "serializedProductLine": false, "serialRequiredFlag": null, "stockStatus": null, "programType": "DC Required", "dna": false, "nstk": false, "obsolete": false, "stockEligible": false, "hazmat": false, "dimDataPrimary": true, "dimDataWms": true, "workQueueId": 1, "approved1Date": null, "approved1AssocId": null, "approved2Date": null, "approved2AssocId": null, "approved3Date": null, "approved3AssocId": null, "approved4Date": null, "approved4AssocId": null, "approved5Date": null, "approved5AssocId": null, "rejectedDate": null, "rejectedAssocId": null, "rejectedNotes": null, } },
here's my json results from my web API. How can I de-serialize this into a list of an object using c# and newtonsoft. I have tried to use json properties in my model class however I'm a bit lost. Any suggestions? The data is being pulled from two different sql tables via a linq join and Entity Framework.
using (var context = new SCProcessContext()) { var balance = (from a in context.TblFutureForecastDetailsCross.Where(a => a.RequestId == RequestId) join c in context.TblFutureForecastDetails on a.RequestId equals c.RequestId select new {a,c} ).ToList(); var json = Newtonsoft.Json.JsonConvert.SerializeObject(balance); return Ok(new { results = balance });
-
No Encoding for Name field is specified, any non-ASCII bytes will be discarded
The following .NET 5.0 code using ICSharpCode.SharpZipLib
var gzipInputStream = new GZipInputStream(sourceStream); var tarInputStream = new TarInputStream(gzipInputStream); var gZipOutputStream = new GZipOutputStream(destinationStream); var tarOutputStream = new TarOutputStream(gZipOutputStream);
now emits warnings
[CS0618] 'TarInputStream.TarInputStream(Stream)' is obsolete: 'No Encoding for Name field is specified, any non-ASCII bytes will be discarded' [CS0618] 'TarOutputStream.TarOutputStream(Stream)' is obsolete: 'No Encoding for Name field is specified, any non-ASCII bytes will be discarded'
What
Encoding
should I specify when constructingTarInputStream
andTarOutputStream
? -
View models shared between WPF and Blazor in a .NET Standard project
Is it possible to keep a view models in a .NET Standard project?
I would like to have a common view models for two different applications: one written in WPF and the second one in Blazor. The problem appeared when I wanted to invoke my ICommand's event
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
in a separate thread and got an error: "System.InvalidOperationException: The calling thread cannot access this object because a different thread owns it.". I don't see Dispatcher support in .NET Standard, or any alternative to it. Can such view models be shared between different platforms (eg. Windows, Mac and Linux) at all? -
Run IronPython script with .netStandard 2.1 without any manual dependencies
I have created one py script which imports my .netStandard classLibrary and runs its execution
The issue is, when my class library .netStandard version is 2.0 or less it runs properly, But if the version is .netStandard 2.1 it asks for netStandard.dll as not found.
Now i have copied the .netStandard file from 'C:\Program Files (x86)\dotnet\shared\Microsoft.NETCore.App\3.1.5' location and pasted with the Py script location and now it ran successfully.
I don't understand why specifically with .netStandard v2.1 the DLR with Iron python does not detect the netStandards dll. or are there are any steps i am missing?
-
EntityFrameworkCore.Analyzers.dll does not contain any analyzers
I'm updating EntityFrameworkCore from ver 3.1 to 5.0.2 and receive this warning message:
"Microsoft.EntityFrameworkCore.Analyzers.dll does not contain any analyzers."
Any ideas what have caused this?
Target Framework of my project: .NET Standard 2.1