|
Is it possible to use the same void between multiple forms in the same project?
If it possible, how is it done?
|
|
|
|
|
No idea what that question means.
'void' is a keyword used in C# code.
The most frequent usage is to specify the return type of a method.
A much less frequent usage is as a void pointer in unsafe code usage.
|
|
|
|
|
If you mean a method as in:
void Foo() { ... } And you want to access it from a different form, then there are two things you need to consider:
1) If it is not declared as static then you need an instance of the containing form in order to call it:
MyContainingForm form = new MyContainingForm();
...
form.TheMethodYouWantToCall(); If it is declared as static then you prefix it with the containing form class name:
MyContainingForm.TheMethodYouWantToCall();
2) In both cases, the appropriate access modifiers must be used when declaring both the containing class and the method: Access Modifiers - C# Programming Guide | Microsoft Learn[^]
You may also need to explicitly prefix the namespace name as well.
"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!
|
|
|
|
|
Let's say that I have this Worker class which uses a Func to process data
public class Worker
{
public Func<int, List<string>> DataSource { get; set; }
public void Execute(int num)
{
var data = DataSource(num);
data.ForEach(x => Console.WriteLine(x));
}
}
I want to call a repository method using the func. Here's the repo method
public class Repo
{
public List<string> GetData(int num)
{
var results = new List<string>();
for (int x = 0; x < num; x++)
{
results.Add($"Result{x}");
}
return results;
}
}
And here's the class that fires it all
internal class Program
{
static void Main(string[] args)
{
var repo = new Repo();
var myClass = new Worker();
myClass.DataSource = repo.GetData;
myClass.Execute(10);
Console.ReadLine();
}
}
Here's the question...
How would I code this so that I wouldn't have to pass in the param to Execute? Can I define the Func with the param as part of it? Maybe something like:
myClass.DataSource = repo.GetData(10);
I don't want to have to pass any parameters into the Worker class. Can the func somehow accept parameters?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 4-May-23 0:58am.
|
|
|
|
|
Sounds like you're looking for "partial application":
Currying vs partial function application | Jon Skeet's coding blog[^]
For example:
public static class Functional
{
public static Func<TResult> Apply<TArg, TResult>(this Func<TArg, TResult> fn, TArg arg)
{
return () => fn(arg);
}
}
public class Worker
{
public Func<List<string>> DataSource { get; set; }
public void Execute()
{
var data = DataSource();
data.ForEach(x => Console.WriteLine(x));
}
}
internal class Program
{
static void Main(string[] args)
{
var repo = new Repo();
var myClass = new Worker();
myClass.DataSource = Functional.Apply(repo.GetData, 10);
myClass.Execute();
Console.ReadLine();
}
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
That's exactly what I need.
Thank you
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Make 'num an optional parameter that provides a default value which the caller can override:
public static int defaultNumValue = 10;
public class Repo
{
public List<string> GetData(int num = defaultNumValue)
{
var results = new List<string>();
for (int x = 0; x < num; x++)
{
results.Add($"Result{x}");
}
return results;
}
}
«The mind is not a vessel to be filled but a fire to be kindled» Plutarch
|
|
|
|
|
Greetings,
Do you know of any C#/NET libraries to validate an XML file with Schematron.
I searched but couldn't find anything.
Thanks for your help.
Anthony
|
|
|
|
|
|
I have a project that will end up language dependent. I am also fairly new to XML in terms of reading it from files. I need to use XML for information used by the project and that information will be available in several languages. So the basic structure of the XML is this:
<InfoNode>
<Language ID="enUS">
<DataClassNode>
<Name>SomeName</Name>
<Abbrev>SN</Abbrev>
<Description>Some Description of what this represents</Description>
</DataClassNode>
</Language>
</InfoNode>
The code that does this and then the code that deserializes the information is this:
public static DataClassNode Deserialize(XmlReader reader, CultureInfo culturalLanguage)
{
XmlReader found = null;
XmlReader _default = null;
while (found == null)
{
if (reader.ReadToFollowing("Language"))
{
if (reader.MoveToAttribute("ID"))
{
string id = reader.ReadContentAsString();
reader.MoveToElement();
if (id.Equals(culturalLanguage.Name))
{
found = XmlReader.Create(reader.ReadElementContentAsString());
}
else if (id.Equals(DefaultCulture.Name))
{
_default = XmlReader.Create(reader.ReadInnerXml().ToStream());
}
}
}
else
break;
}
if (found == null)
{
if (_default == null)
throw new Exception("Default Language Not Found!");
else
found = _default;
}
DataContractSerializer serializer = new DataContractSerializer(typeof(DataClassNode));
DataClassNode flagtag = (DataClassNode)serializer.ReadObject(found);
if (flagtag == null)
throw new Exception("Cannot deserialize DataClassNode Object!");
return flagtag;
}
I have been able to navigate to the language and make sure the one needed is found. Now I need to populate a class instance with the data inside the language node. I am using DataContractSerializer so I want a new XMLreader instance that contains the content of the language node ONLY. Not sure how to do this though. I know how to read strings etc... just not sure how to read the contents of the language node. I have tried what is above. I have tried InnerXML and OuterXML. Both of these return "None" as my XML. My comments point to where my knowledge falls short.
modified 20-Apr-23 10:00am.
|
|
|
|
|
I think you should be using "XPath navigation".
Select Nodes Using XPath Navigation | Microsoft Learn
"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
|
|
|
|
|
The XmlReader is pretty low level, and I would not recommend you use it.
Better options:
- Use XDocument[^]. The examples on the page are focusing on creating the document, but use the static Load or Create methods when you already have the file.
- Make a full C# class representing your XML and use the DataContract serializer - then get your data from there using standard C# code.
XmlReader is very good at dealing with XML where you do not want to keep everything in memory - if that is a requirement, then you might have made a mistake somewhere earlier in the project.
That said, the XmlNodeReader[^] is doing what you are asking for.
|
|
|
|
|
Thanks,
I used a mix of XmlReader and XmlDocument to resolve it so it is similar to your suggestion.
|
|
|
|
|
Banging my head on a likely simple problem...
I'm getting a 405 on a WebSocket connection with WSS but not WS.
Vanilla project in VS with this code. Starting with HTTP works. Starting under HTTPS faults 405.
public static class Program {
public static void Main (string [] args) {
var builder = WebApplication.CreateBuilder ( args );
var app = builder.Build ();
app.UseWebSockets ();
app.MapGet ( "/" , (HttpContext http)
=> http.Response.WriteAsync ($$"""
<script>
self.Socket = new WebSocket(`${location.origin.replace("http", "ws")}/socket`);
</script>
""" ) );
app.MapGet ( "/socket" , Connect);
app.Run ();
}
static void Connect (HttpContext HttpContext) {
_ = HttpContext.WebSockets.AcceptWebSocketAsync ().Result;
}
}
modified 14-Apr-23 11:30am.
|
|
|
|
|
If you google "405 error", you get a description of why it occurs:
How to Fix HTTP Error 405 Method Not Allowed: 11 Methods[^]
But the most likely reason is simple: the remote site does not support HTTPS - so HTTP requests succeed, but secure ones fail. The only cure for that would be to contact the remote admins, and ask them to support HTTPS (which will require a valid certificate). Investigating that is where I'd start.
"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!
|
|
|
|
|
This is all in my own environment and https works on the site/server but not with sockets (wss).
This is (basically) code that I had running fine on .Net6, so I'm thinking there could be a config setting. I discovered it when I started upgrading (to WebApplication ) some old (working) apps.
It also works (WSS) when I'm running Fidder. If I close Fiddler, it quits working.
|
|
|
|
|
OriginalGriff wrote: does not support HTTPS - so HTTP requests succeed, but secure ones fail.
I don't think so.
The 405 is a HTTP error. So the HTTP server is responding to the request with that error.
The 'S' means that the protocol is actually HTTP (and thus TCP) with SSL (or perhaps TLS) in place.
The SSL protocol runs on the TCP layer and not the HTTP layer. If SSL is not in place (TCP) then for client server two things can happen
1. The client expects SSL but server is not doing it: Then it results in a timeout in the connection for the client because the client is expecting a response (the SSL protocol) which never happens.
2. The client is not using SSL but the server is: In this case the server ends up closing the socket because the the client never sent the expected SSL protocol messages. Client sees error message along the lines of the 'remote end' closed the socket. But it will not show up until the client attempts to use the socket (so connection worked but then later usage fails.)
|
|
|
|
|
I think I'm getting closer. I found a change in Edge that says it no longer uses the Windows certs - but manages its own. It doesn't seem to pick up my/VS self-gen'ed dev cert. Going to try to figure out how to do a manual import and see if that helps.
|
|
|
|
|
How to get XSI:type in inner class while converting to XML
|
|
|
|
|
You already posted this question in Q&A. There's no need to post it again.
What you do need to do is provide more information about what it is that you're actually trying to do here. That question could mean many things and the solution will depend on what it is that you're trying to do here.
|
|
|
|
|
I want to run a these commands in parallel and get the results as each finishes.
private void Load()
{
_apiProxy.GetNavigationItems(NavigationItemType.Company);
_apiProxy.GetNavigationItems(NavigationItemType.Employee);
_apiProxy.GetNavigationItems(NavigationItemType.Project);
}
What's the right way to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
modified 7-Apr-23 1:17am.
|
|
|
|
|
Why not use three separate await Task.Run commands in sequence?
"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 always thought await returns back to the caller, so at the first await, the rest are not fired until the previous one completed
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Sorry - I didn't see the "in parallel" bit in your original post.
"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!
|
|
|
|
|
NP, so what's the best way to run them in parallel and wait for results from each one separatly?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|