|
OK ... if it is realized ...
But exactly that was the thing I supposed ...
We could get there here ... if you answered my last questions ...
|
|
|
|
|
Right,Ralf.
Yes. I think you are right.
You had helped me some times. I appreciate that.
I wish you luck.
|
|
|
|
|
Suppose in my project I'm referencing another project as follows:
using static ProjectA;
Prior to using the static keyword in the using statement, one of my methods was able to create an instance object of a class called Products from ProjectA, set the values of its members, and return the instance object of this class as shown below:
Product prod = new Models.Product();
prod.ID = unit.ID;
prod.Dept =unit.Department;
return prod;
Now I'm getting errors stating that I cannot use an instance object to access members of the Product class. I tried to resolve this issue by going to the Product class in ProjectA, adding the static keyword to the ID and Dept properties, and creating a static constructor. I can now set values of the ID and Dept properties using the class name as shown below:
Product.ID = unit.ID;
Product.Dept =unit.Department;
The only problem I have now is I'm still getting an error stating that I cannot use an instance object to access members of the Product class on the line Product prod = new Models.Product();. Please point out what I'm doing wrong.
|
|
|
|
|
using static takes a fully-qualified type name, and makes the static members and nested types of that type available without prefixing them with the class name.
For example:
using static System.Console;
...
WriteLine("Foo"); using static directive - C# Reference | Microsoft Docs[^]
Based on your description, ProjectA is a namespace; in which case, you just need using , not using static .
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 4-Sep-20 9:09am.
|
|
|
|
|
Richard Deeming wrote: of that type available with prefixing them I think you meant "without".
|
|
|
|
|
Coffee deficiency strikes again!
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
greetings guys. im building an android application and i have some second thoughts on some error handling case.
I have a method that gets data from the internet by calling this method:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task<string> GetDraws(Uri url, string date)
{
Dictionary<string, string> StoredDates = new Dictionary<string, string>();
StoredDates = JsonConvert.DeserializeObject<Dictionary<string, string>>(StoredDatesList);
var contents = string.Empty;
HttpClient client = new HttpClient();
if (StoredDates != null)
if (StoredDates.ContainsKey(date))
{
contents = StoredDates[date];
}
else
{
var current = Connectivity.NetworkAccess;
if (current != NetworkAccess.Internet)
return null;
client = new HttpClient();
contents = await client.GetStringAsync(url);
var res2 = JsonConvert.DeserializeObject<RootObject>(contents.ToString());
if (180 == res2.content.Count)
{
StoredDates.Add(date, contents);
StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
}
}
else
{
StoredDates = new Dictionary<string, string>();
contents = await client.GetStringAsync(url);
var res2 = JsonConvert.DeserializeObject<RootObject>(contents.ToString());
if (180 == res2.content.Count)
{
StoredDates.Add(date, contents);
StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
}
}
return contents;
}
the if statement
current != NetworkAccess.Internet) checks if internet is available, when internet is not available i return null and i check if the data is null and im displaying a message(error, internet is not available etc).
I find this approach very bad and im trying to think how is the proper way to handle this. i cannot show a message to the user from the GetDraws() function.
maybe the correct way for this approach is to have a public variable like bool internetError = false; and to make if false every time i call GetDraws(), make it true if internet is not available and check its state after GetDraws?
Internet connection is not necessary every time GetDraws() is used and that is why im not checking before i called this function for internet connection
|
|
|
|
|
If your app can continue without the data, and the user can do nothing about it, there is no point in telling them.
It's called: graceful degradation.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
No, i meant that not all the times data is needed. When it does, it will throw an error if you dont have
|
|
|
|
|
Slightly off topic perhaps, but you are repeating the same block of code twice and could refactor all of this into a single function and just call that function twice:
contents = await client.GetStringAsync(url);
var res2 = JsonConvert.DeserializeObject<RootObject>(contents.ToString());
if (180 == res2.content.Count)
{
StoredDates.Add(date, contents);
StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
}
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
Oh thank you. Any improvement is highly welcome, i have refactor it to:
public static string StoredDatesList
{
get => Preferences.Get(nameof(StoredDatesList), string.Empty);
set => Preferences.Set(nameof(StoredDatesList), value);
}
public static async Task<string> GetDraws(Uri url, string date)
{
var StoredDates = JsonConvert.DeserializeObject<Dictionary<string, string>>(StoredDatesList);
var contents = string.Empty;
var current = Connectivity.NetworkAccess;
var client = new HttpClient();
if (StoredDates != null)
if (StoredDates.ContainsKey(date))
{
contents = StoredDates[date];
}
else
{
if (current != NetworkAccess.Internet)
return Helpers.Settings.Common_Error_NoInternetConnection;
contents = await DownloadResults(url, date, StoredDates, contents, client);
}
else
{
if (current != NetworkAccess.Internet)
return Helpers.Settings.Common_Error_NoInternetConnection;
StoredDates = new Dictionary<string, string>();
contents = await DownloadResults(url, date, StoredDates, contents, client);
}
return contents;
}
private static async Task<string> DownloadResults(Uri url, string date, Dictionary<string, string> StoredDates, string contents, HttpClient client)
{
contents = await client.GetStringAsync(url);
var res2 = JsonConvert.DeserializeObject<RootObject>(contents.ToString());
if (180 == res2.content.Count)
{
StoredDates.Add(date, contents);
StoredDatesList = JsonConvert.SerializeObject(StoredDates, Formatting.Indented);
}
return contents;
}
...
public const string Common_Error_NoInternetConnection = "Error_NoInternetConnection";
So i would check every time if the return sting is equal to Common_Error_NoInternetConnection, Does this sounds like a solid idea?
modified 4-Sep-20 16:37pm.
|
|
|
|
|
I feel dumb asking, but maybe someone has done it already.
I get why it bombs, "$0.00" and convert to 0.00
I find it hard to believe that I have to write a complex custom type converter for this.
And in the CSV file , "$53.00",
public double SoldFor { get; set; }
Map(m => m.SoldFor).Index(25).Default(0.00).TypeConverterOption.NumberStyles(NumberStyles.Currency);
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
|
I'll give that a whirl
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Wrote a TypeConverter, Took me awhile to understand how it works
I tried modifying the input text, with replace, but was not able to change it's value.
So I went with TryParse to generate a new double to send back, and just used substring to chop the $ off.
I bet the 2nd parse will fail now that I think about it. it should be TryParse as well.
public class PriceConverter<T> : DefaultTypeConverter
{
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
{
var result = 0.00;
if (text.StartsWith("$"))
{
double.TryParse(text.Substring(1, text.Length - 1), out result);<br />
}
else
{
result = double.Parse(text);
}
return result;
}
}
And the map
Map(m => m.SoldFor).Index(25).TypeConverter<PriceConverter<double>>();
Hope this helps somebody in the future
If it ain't broke don't fix it
Discover my world at jkirkerx.com
modified 4-Sep-20 12:40pm.
|
|
|
|
|
jkirkerx wrote: Hope this helps somebody in the future Not if they thing about it. You declare the ConvertFromString with a return of object you then use var inside the method and call the method expecting a double . It would not pass revue!
Also you may want to cater for other currency symbols.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
It's my first type converter, and first time using CSVHelper. Didn't even know it existed till 2 days ago.
It didn't like a return value of double, so I left the object.
I will work on the currency system; or wait I have no way of knowing what marketplace the file is coming from, like Japan, UK which can be French, German, Spanish, English. String Array match.
It's not a program for sale, just for my use. You must think I will grow really fast and sell around the world, appreciate the lift in spirits there. Next markets I enter will be Canada and Malaysia. I can't afford to ship goods to Europe.
Appreciate the feedback seriously, I'll make those changes. But sticking with NoSQL.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I'm working on it, but really wanted to see some results today like my first database/collection write.
Thank God I only have the 2 CSV files to download. But Amazon allows for uploading Inventory, Prices, and shipping plans. Not sure about the format, probably CSV.
I know what I want to be able to do in this app, and the data I need to see. Using both marketplaces over the last 4 months has shown me that I don't have all the data I need to fill out a tax return, or show the state how much tax was collected. I can't account for all the inventory I purchased and sold. And I'm not a wiz with Excel, don't even know where to start with it. I don't even know how much money I made so I can pay myself. I've sold so much, that I don't have time to hand enter all the data. Just too many entries.
public class AmazonDateConverter<T> : DefaultTypeConverter
{
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
{
var ci = CultureInfo.GetCultureInfo("en-us");
return DateTime.Parse(text, ci);
}
}
public class AmazonPriceConverter<T> : DefaultTypeConverter
{
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
{
var result = 0.00;
if (text.StartsWith("$"))
{
double.TryParse(text.Substring(1, text.Length - 1), out result);
}
else
{
double.TryParse(text, out result);
}
return result;
}
}
public class EBayYesNoConverter<T> : DefaultTypeConverter
{
public override object ConvertFromString(string text, IReaderRow row, MemberMapData memberMapData)
{
if (text.ToLower().Equals("yes"))
{
return true;
}
else if (text.ToLower().Equals("no"))
{
return false;
}
return false;
}
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
The IRS don't care.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I was talking about my state and sales tax, they are brutal.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I'm stumped here. Writing a program for myself to keep track of Amazon and EBay sales. On this Ebay report, the first line is a string of comma's, and then the header. Some of the header columns are unquoted and some are quoted to represent text.
I don't know if the string comma's is a blank line, or what. And not sure how to tell CsvHelper about the quotes in the column names.
I guess the string of commas is a line, because the line has a line feed. That's solved.
My problem is the usual won't work issue. Guess I could edit the file and remove the commas and test to see if the column reads, No, the first column doesn't read and it doesn't have quotes. So it's the first line that's my issue.
I wonder why EBay put that line of commas in as the first line, must mean something.
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Sales Record Number,"Order Number","Buyer Username","Buyer Name","Buyer Email","Buyer Note","Buyer Address 1","Buyer Address 2","Buyer City","Buyer State","Buyer Zip","Buyer Country","Ship To Name","Ship To Phone","Ship To Address 1","Ship To Address 2","Ship To City","Ship To State","Ship To Zip","Ship To Country","Item Number",Item Title,"Custom Label","Sold Via Promoted Listings","Quantity","Sold For","Shipping And Handling","Seller Collected Tax","eBay Collected Tax","Electronic Waste Recycling Fee","Mattress Recycling Fee","Additional Fee","Total Price","eBay Collected Tax and Fees Included in Total","Payment Method","Sale Date","Paid On Date","Ship By Date","Minimum Estimated Delivery Date","Maximum Estimated Delivery Date","Shipped On Date","Feedback Left","Feedback Received","My Item Note","PayPal Transaction ID","Shipping Service","Tracking Number","Transaction ID","Variation Details","Global Shipping Program","Global Shipping Reference ID","Click And Collect","Click And Collect Reference Number","eBay Plus","Authenticity Verification Program","Authenticity Verification Status","Authenticity Verification Outcome Reason"
,"","","","","","","","","","","","","","","","","","","","",,"","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","","",""
My map
Map(m => m.SalesRecordNumber).Name("Sales Record Number").Index(0);
Map(m => m.OrderNumber).Name("Order Number").Index(1);
Map(m => m.BuyerUsername).Name("Buyer Username").Index(2);
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Are you sure it's not an artifact of your download process? How did you get teh data?
The first row of a CSV file can normally be one of three things:
1) A header row giving the column titles.
2) The first row of data
Or
3) A row indicating a "custom separator": "Sep=|" for example is used by Excel: Comma-separated values - Wikipedia[^]
I've never seen a valid CSV file with anything else as the top row.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I downloaded it again thinking the same thing.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I've seen one case in 30 years where the first TWO rows were headers. Yeah, it's not what anyone would call "standard", but there it was.
The first row was column types and the second row contained the column names.
|
|
|
|
|
Since you're mapping by index, you could skip the first two lines in the TextReader before you pass it to the CsvReader , and tell it there is no header:
using (TextReader reader = ...)
{
reader.ReadLine();
reader.ReadLine();
using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
{
csv.Configuration.HasHeaderRecord = false;
csv.Configuration.RegisterClassMap<FooMap>();
var records = csv.GetRecords<Foo>()
...
}
}
...
public class Foo
{
public string SalesRecordNumber { get; set; }
public int OrderNumber { get; set; }
public string BuyerUsername { get; set; }
}
public class FooMap : ClassMap<Foo>
{
public FooMap()
{
Map(m => m.SalesRecordNumber).Index(0);
Map(m => m.OrderNumber).Index(1);
Map(m => m.BuyerUsername).Index(2);
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|