|
Your instructor must have covered the necessary points already, right?
Were you not paying attention?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Quote: Go to ParentI didn't start yet because I have no idea to go on with it because the question is asking to design a program that uses nested for
loop statements to display students and their respective locations in a string matrix, and I have never come across such a question.
We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.
So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.
If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^] and then read your recent course notes again. When you understand them, read the whole question he set carefully, and think about what he is trying to get you to do. This isn't a complicated exercise - it's very basic stuff he needs you to learn here - so a little thought should get it working.
"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!
|
|
|
|
|
Hi, folks.
I’m developing an application in C# using Visual Studio. This application has a parent form from which the user can call other forms. My original problem was: if the user has already launched a form and he clicks again in the menu item to launch the same form, the program should only focus on the form. If the form is not on screen, the program should launch it. Another problem is that I have 1 form that is launched with 2 different data sets (different tables in a DB).
I solved this problem this way: I declared 2 variables for the forms and defined 2 routines:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
if (FormEfetivas == null)
{
FormEfetivas = new Frm_Saida();
FormEfetivas.MdiParent = this;
FormEfetivas.Text = "Saídas Efetivas";
FormEfetivas.Tag = "1";
FormEfetivas.Show();
}
FormEfetivas.Activate();
}
private void M2_SaidaPrevisao_Click(object sender, EventArgs e)
{
if (FormPrevisao == null)
{
FormPrevisao = new Frm_Saida();
FormPrevisao.MdiParent = this;
FormPrevisao.Text = "Previsão de Saídas";
FormPrevisao.Tag = "0";
FormPrevisao.Show();
} Now the problem is: when the user opens one form, then closes it, he cannot reopen it again, because the variables (FormEfetivas and FormPrevisao) are not eliminated when the user closes the form.
What am I forgetting here? Can anybody help me?
Thanks.
|
|
|
|
|
Add a handler to the Form.Closed event when you create the instance, and in the handler set the appropriate variable (for example FormEfetivas to null. The existing code will then generate a new instance when they try next time.
"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!
|
|
|
|
|
Hi, OriginalGriff.
I didn't follow you. Could you explain better? Or give me an example?
Thanks.
|
|
|
|
|
What part doesn't 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!
|
|
|
|
|
It's not a matter of making sense. I understand what you wrote, but I'm confused about how to do it.
Could you explain it?
Thanks.
|
|
|
|
|
Explain what? I have no idea which bits you are capable of doing for yourself!
So ... you know how to add an event handler, yes?
"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!
|
|
|
|
|
|
Simple enough:
private void M2_SaidaEfetiva_Click(object sender, EventArgs e)
{
var theForm = Application.OpenForms.OfType<Frm_Saida>().FirstOrDefault();
if (theForm is null)
{
theForm = new Frm_Saida();
theForm.MdiParent = this;
theForm.Text = "Saídas Efetivas";
theForm.Tag = "1";
theForm.Show();
}
theForm.Activate();
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Hi all,
I would appreciate a hand in designing my code.
I have a number of classes for different entities (currently 7 but there can be more in the future). I need a method in each of these classes that authorizes user's access to a file resource. Implementation varies but an exception must be thrown if user does not have access to the file in question.
Problem is, when the file is being accessed, I need to call the authorization method of the correct class based on the data on the file. Files are stored in a db table and one of the fields in that table determines which entity the file is related to and how to find which user has access to the file.
So I must read the file data from db, then see which entity it's related to and then call the authorization method of the correct class.
Is there a way in C# to accomplish this without having to make a switch case structure on the file field value and initialize a class and call a method?
Example of the classes used:
public class FileAttachment
{
protected void AuthorizeFileAccess(FileAttachmentData file)
{
}
}
public class ChatMessage
{
public int SenderUserId;
public int RecipientUserId;
}
|
|
|
|
|
Your words: "read the file data from the db"; i.e. no switch. The "file data" would identify the "class type" to be instantiated for your "method call".
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 am baffled by how the code in the 'Human and 'Cat classes can successfully call a generic static method (SetCache<T>(T value) ) without supplying the usual <Type> adornment.
Here's a sample usage of the code:
BeingCache cache = BeingCache.CacheInstance;
Human ahuman = new Human("joe", "male");
Cat acat = new Cat("Meow", true);
IBeing b1 = cache[1];
IBeing<Cat> b2 = (IBeing<Cat>) b1;
Cat cat = b2.IBInstance;
bool st0 = b2 is Cat;
bool st1 = b2 is Being<Cat>;
bool st2 = b2 is IBeing<Cat>;
Cat hepcat = BeingCache.GetBeing<Cat>(cache[1]);
IBeing<Cat> b3 = (IBeing<Cat>) cache[1]; The example: note that classes 'Human and 'Cat do not inherit from anything.
using System;
using System.Collections.Generic;
namespace InterfaceInheritance_2020
{
public class Human
{
public Human(string name, string gender)
{
Name = name;
Gender = gender;
BeingCache.SetCache(this);
}
public string Gender{ get;}
public string Name { get; }
}
public class Cat
{
public Cat(string name, bool haswhiskers = true)
{
Name = name;
HasWhiskers = haswhiskers;
BeingCache.SetCache(this);
}
public bool HasWhiskers { get; }
public string Name { get; }
}
public interface IBeing
{
Type BeingType { set; get; }
}
public interface IBeing<T> : IBeing
{
T IBInstance { set; get; }
}
public class Being<T> : IBeing<T>
{
public Being(T content)
{
IBInstance = content;
BeingType = typeof(IBeing<T>);
}
public Type BeingType { get; set; }
public T IBInstance { get; set; }
}
public sealed class BeingCache : List<IBeing>
{
private BeingCache()
{
}
public static BeingCache CacheInstance{ get; } = new BeingCache();
public static void SetCache<T>(T value)
{
CacheInstance.AddBeing(new Being<T>(value), typeof(T));
}
public void AddBeing(IBeing being, Type btype)
{
being.BeingType = btype;
CacheInstance.Add(being);
}
public static T GetBeing<T>(IBeing being)
{
return (T) ((IBeing<T>) being).IBInstance;
}
}
}
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
The type is inferred because you pass in a value of the type in your call
public static void SetCache<T>(T value)
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
Hi, Appreciate your response. I am still baffled: when BeingCache.SetCache is called within the two non-generic classes 'Human and 'Cat ... like this:
BeingCache.SetCache(this); // :wtf:
How does the generic static method:
public static void SetCache<T>(T value) ''
not throw a compile time error, or a run-time error ?
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
I had a similar conversation with jsop on here who wrote an excellent article on using reflection in a DAL - he had the method below in one of his classes
public static T ConvertFromDBValue<T>(object obj, T defaultValue)
{
T result = (obj == null || obj == DBNull.Value) ? default(T) : (T)obj;
return result;
}
and he made calls like this
object propvalue;
Type proptype;
propvalue = ConvertFromDBValue(propvalue, proptype.GetDefaultValue());
It just works - however if you change the parameter T defaultValue to a type other than T it won't compile unless you provide the type in <>.
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
A revelation to me Being able to do this saves me the trouble of having the non-generic classes inherit from a generic-typed abstract class that calls 'SetCache<whatever>
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Indeed
"I didn't mention the bats - he'd see them soon enough" - Hunter S Thompson - RIP
|
|
|
|
|
If you call
SetCache(x);
and "x" is type string then it compiles
SetCache<string>(x);
if "x" is an int then it compiles
SetCache<int>(x);
whatever type "x" is it assumes the type between the angled brackets is that type. The only time you absolutely have to supply the type is when the IDE can't work out itself what the type is. For example if you have
T GetCache<T>(string key)
then the IDE can't workout what "T" should be from the params
GetCache("username");
In the above code there is no way to work out what GetCache returns so you have to explicitly say;
GetCache<string>("username");
|
|
|
|
|
Thanks, this is a revelation to me
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
F-ES Sitecore wrote: then the IDE can't workout ... I think you mean the compiler.
|
|
|
|
|
I initially wrote the compiler but I didn't know if it was the compiler or VS, but thinking about it it's obviously the compiler 
|
|
|
|
|
|
thanks, Richard, it made my day to discover another thing i should have known
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|
|
Thank you, all, for your illuminating responses ! Any further ideas, comments, critiques, would be very appreciated. If you care to address the more theoretical issue of whether this example embodies a design-pattern, I am all ears. Is the facility to "rehydrate" from instances of objects cast to interfaces to the ab origine strongly typed instance a form of factory pattern, or just a hack to enable collections of mixed types in C#'s strongly typed codeaverse >
a message for: @RichardDeeming @HaroldAptroot @jschell
If you have time to consider what this code does in the light of your comments on my April 24th. post and code example: [^] ... does this example meet my stated goal of:Quote: the goal: use interfaces to enable creating a collection of different types of objects: and, then, at run-time, cast back to the generic type without hard-coding specific types.
a. without using 'dynamic
b. without using reflection
c. without cloning/copying (using something like 'Activator.CreateInstance)
«One day it will have to be officially admitted that what we have christened reality is an even greater illusion than the world of dreams.» Salvador Dali
|
|
|
|