|
Hi, I have the following get / set class,...
internal class PostSections
{
public string title { get; set; }
public string[] highlight { get; set; }
public string[] highlight2 { get; set; }
public bool highlightFirstWord { get; set; }
public bool sendToValidation { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }
public Sectionquery[] sectionQueries { get; set; }
}
public class Sectionquery
{
public string id { get; set; }
public string name { get; set; }
public string[] keywords { get; set; }
public bool active { get; set; }
}
I can set the values for 'title', 'highlight' etc., but I cannot work out how to reference and set the values for items within the 'Sectionquery' class, eg: id, name, keywords and active.
Here's my code,...
var newPost = new PostSections()
{
title = "Title of Book",
highlight = list.ToArray(),
highlight2 = list.ToArray(),
highlightFirstWord = false,
sendToValidation = false,
startDate = "2022-11-22T00:00:00.000Z",
endDate = "2022-11-22T00:00:00.000Z",
sectionQueries = new Sectionquery[] { }
};
Using the above, my API call works, however I need to set values within that Sectionquery before calling the API. How to I reference them?
Hope my question's clear, thanks in advance.
|
|
|
|
|
Your array creating won't work - it creates an empty array (no elements) and an array can't be expanded or contracted.
Try doing the Sectionquery creating outside the PostSections instance creation: otherwise it gets confusing. The way I'd do it is to replace the array with a list to be more flexible:
internal class PostSections
{
public string title { get; set; }
public string[] highlight { get; set; }
public string[] highlight2 { get; set; }
public bool highlightFirstWord { get; set; }
public bool sendToValidation { get; set; }
public string startDate { get; set; }
public string endDate { get; set; }
public List<Sectionquery> sectionQueries { get; set; }
} That way, you can start by declaring the collection:
var queries = new List<Sectionquery>() And add your new items to it (even from a JSON / XML / CSV file or a database) without knowing in advance how many elements it needs.
Then just add your new items to it:
queries.Add(new SectionQuery() { ... });
queries.Add(new SectionQuery() { ... });
... And then change your PostSection instance creation to suit:
var newPost = new PostSections()
{
title = "Title of Book",
highlight = list.ToArray(),
highlight2 = list.ToArray(),
highlightFirstWord = false,
sendToValidation = false,
startDate = "2022-11-22T00:00:00.000Z",
endDate = "2022-11-22T00:00:00.000Z",
sectionQueries = queries
}; Make sense?
"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!
|
|
|
|
|
Other answer is probably better but if you need to use an existing structure...
You can create it dynamically (pseudo code)
int count = ....;
var sections = new Sectionquery[count];
for (int i=0; i < count; i++)
{
sections[i].id = ...
sections[i].name = ...
}
newPost.sectionQueries = sections ;
You can also create it statically using the same layout you have already used. Although typically that would not be useful for the the structure you gave. But it wouldn't be used for PostSections either.
Pseudo code (google for 'c# create array statically')
var newPost = new PostSections()
{
...
sectionQueries = new Sectionquery[]
{
new {
"1", // id
"xxx", // name
...
},
new {
"2", // id
"yyy", // name
...
},
}
};
|
|
|
|
|
Is it possible to use switch case instead of if statements with the code below this text?
C#:
<pre><pre lang="C#"> if number > 20
{
............
}
if number > 30
{
............
}
if number > 50
{
............
}
if number > 70
{
............
}
|
|
|
|
|
That depends on which language version you're using. Relational patterns[^] were added in C# 9.0:
switch (number)
{
case > 70:
{
...
break;
}
case > 50:
{
...
break;
}
case > 30:
{
...
break;
}
case > 20:
{
...
break;
}
} NB: Unlike older switch statements, the order of the cases matters. The first matching case will be applied, meaning that you need to reverse the order of your tests - 71 is greater than 20, so if case > 20 was first, that would be the case that matched.
Also note that you're using if rather than else if , so the code code > 20 will also execute for values greater than 70. There doesn't seem to be a goto case ... syntax for relational patterns, so if that's the required behaviour, you would need to duplicate the code in each case:
switch (number)
{
case > 70:
{
break;
}
case > 50:
{
break;
}
case > 30:
{
break;
}
case > 20:
{
break;
}
} This would potentially make the code messier than your current version.
And if the code in the if blocks modifies the number variable in any way, it gets even messier.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Since there is no else you should just use methods for each step. It would look like
Step1(number);
Step2(number);
Step3(number);
Step4(number);
|
|
|
|
|
I'm reading Clean Code book recently. At this book, they discuss everything by Java.
i wonder if Clean code concept still use in C# Development?
Is there any recommend book about clean code with C#?
|
|
|
|
|
"Clean code" isn't language specific: it's a way to develop code that is applicable to any language. THe authors of your book just used Java as an example because that is what they are most familiar with.
Google can find you many similar books involving C#: Clean code c# book - Google Search[^]
"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!
|
|
|
|
|
What is this thing you speak of, book?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
It's a tablet with no keyboard, mouse, or touch screen.
But ... the batteries last forever!
"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 am looking at this Google API WPF OAuth Example[^]
I understand most of what it's doing. In userinfoCall() it displays the Google user's info
Making API Call to Userinfo...
{
"sub": "",
"name": "Kevin Marois",
"given_name": "Kevin",
"family_name": "Marois",
"locale": "en"
}
What I need is to get the user's contacts. I've been searching & playing with this all day but I can't figure out how to do this.
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
I very much doubt if you can: I'd class my contacts list as "personal information" and I'd be pretty unhappy if any site I logged into using Google had access to them or my calendar.
There is also GDPR which would almost certainly apply, and Google isn't going to ignore that!
"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!
|
|
|
|
|
You, as the user, authorize it via an OAuth window. We already had this functionality in out app, then Google changed their Auth process. I'm just trying to upddate the app
See this[^]
I just can't figure out how to integrate that into their sample code
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
|
Certainly looks like that should answer the OPs question.
As a side note looking at that very quickly I didn't see any mention of what happens if the reader doesn't have permission to view those. Seems like that would be a very relevant error case. Certainly something that a developer should test.
|
|
|
|
|
jschell wrote: I didn't see any mention of what happens if the reader doesn't have permission to view those. I think that may be dealt with in the section about getting set up to use the People API.
|
|
|
|
|
I saw that already. I can't figure out how to integrate that first bit into their sample
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
I'm afraid that using the Google API's is not the easiest thing in the world, IMHO. You need to follow the link to the setup section and start there. It's a good few years since I set up my account (which has since been deleted) so i'm afraid I can't offer any insights.
|
|
|
|
|
OK, thanks anyway
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
|
|
|
|
|
Just curious - have you used any non trivial API ever that was easy? Perhaps not just initially but including support?
Myself I was used 'bitly' which seemed pretty easy except that they throttled using a HTTP 500 error with no other error message. So no way to know if there was a problem on their end or if one had just reached the throttle limit. Noting of course that HTTP 429 exists for exactly this reason.
|
|
|
|
|
Once I figured out the rules it was easy. But, as with most things, the learning curve was a bit steep.
|
|
|
|
|
I was right: What data Google shares
Important: Sign in with Google doesn’t share your Google Account password.
When you use Sign in with Google to access a third-party app or service, Google only shares the following information associated with your Google Account:
Your name
Your email address
Your profile picture
This data is only shared after you give permission. If you want to use Sign in with Google, you can't exclude any of these pieces of data.
In addition to your name, email address, and profile picture, the third party might request further access to some of your Google Account data. You might be able to get contact access if you asked for it when you tried to use OAuth as a added permission, but even that's dodgy with GDPR as it includes other people's personal info so technically you might need permission from each individual in my contacts in order to get them.
"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!
|
|
|
|
|
Yeah ... "contacts" are "personal" information; just what your spammer / hacker is looking for.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
I need to open the default browser with Process.Start(), and I need to know if the user has cancelled or closed the process/browser.
I can detect a cancel via the callback URL, but I can't seem to get Exited to work.
public class Program
{
public static void Main(string[] args)
{
var proc = Process.Start("www.codeproject.com");
proc.Exited += Program_Exited;
proc.WaitForExit();
Console.ReadLine();
}
private static void Program_Exited(object sender, EventArgs e)
{
}
}
What's the right way to handle this?
In theory, theory and practice are the same. But in practice, they never are.”
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
modified 6-Jun-23 20:00pm.
|
|
|
|