|
That's actually what I'm trying to do, but while using generics.
I'm probably overdoing it.
<edit>I certainly had my head screwed on the wrong way yesterday, what I was trying to do is certainly not possible.
Factory it will be.</edit>
modified 2-Oct-14 3:44am.
|
|
|
|
|
Jörgen Andersson wrote: without the caller knowing what type to expect
Why bother then? Use a single class or a proxy.
|
|
|
|
|
Generics cannot be carried to runtime as generics, i.e., the generic type must be specified at compile time. In C#, the only type that can be determined at runtime is
dynamic Thus:
class A { }
class B:A { }
class C:B { }
public static dynamic ConfigSetting(string s)
{
switch (s)
{
case "A":
return new A();
case "C":
return new C();
case "double":
return (double)123;
case "int":
return 123;
default:
return null as string;
}
}
|
|
|
|
|
Hi,
I'm working on a course for 98-361 Software Development Fundamentals. I am given the following example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Lesson02
{
abstract class Polygon
{
public double Length {get; protected set;}
public double Width {get; protected set;}
abstract public double GetArea();
}
class Rectangle: Polygon
{
public Rectangle(double length, double width)
{
Length = length;
Width = width;
}
public override double GetArea()
{
return Width * Length;
}
}
class Program
{
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10, 20);
Console.WriteLine(
"Width={0}, Length={1}, Area = {2}",
rect.Width, rect.Length, rect.GetArea());
}
}
}
Here is a paragraph from the text that I am struggling with:
The properties Length and Width in the Polygon class are declared with a protected access modifier for the set accessor. This means that access to the set accessor is available only inside the Polygon class and its derived classes. You can still get the value of the Length and Width properties in the Main method, but you’ll get an error if you attempt to assign a value to these properties.
What I would like to know is to see code that shows how to use the set accessor properly and what does it look like if you try to assign a value from Main. I understand it will fail but I'd like to see it for myself. I am new to C# and unsure of how to introduce the code myself for an accurate demo.
Unfortunately, my instructors apparently don't write code so they are unable to answer my question.
Thank you!
Rob
modified 1-Oct-14 12:32pm.
|
|
|
|
|
robwm1 wrote: code that shows how to use the set accessor properly What do you mean by: "set properly" ?
The access modifier used on the 'set method in a Property, and the access modifier used on the Property itself determine what type of write access users of instances of the class will have.
... you are using .NET's auto-generated Properties here that create an invisible 'backing field ...
A 'private set restricts write access to the declaring class only: that would be useless here since the declaring class is 'abstract, and meant to be inherited from. You already know what using a 'protected setter does. With no access modifier the 'set inherits the access-modifier-state of its Property declaration.
You could also use 'internal as the access-modifier for the 'set: that would limit write access to only the assembly in which the Property is declared.robwm1 wrote: Unfortunately, my instructors apparently don't write code so they are unable to answer my question. Uh oh !
« I had therefore to remove knowledge, in order to make room for belief » Immanuel Kant
|
|
|
|
|
Richard Deeming seems to be on track with my question. If you look at his response, that is what I was looking for.
Thanks a lot for taking the time! I appreciate it!
|
|
|
|
|
robwm1 wrote: code that shows how to use the set accessor properly
Look at the constructor of the Rectangle class; it assigns values to the two properties, which succeeds because Rectangle is derived from Polygon .
robwm1 wrote: what does it look like if you try to assign a value from Main
You'll get a compilation error. The easiest way to see it is to try it:
static void Main(string[] args)
{
Rectangle rect = new Rectangle(10, 20);
rect.Width = 0;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Ok, that is what I was thinking in regards to causing the failure from Main. After the Rectangle object is instantiated, what if later during execution there is a need to change the Length or Width values. Is there a way to do that or would I have to instantiate another instance with the values I need. You certainly confirmed that it cannot be done from Main. It makes me think that the Rectangle class would need to be designed differently.
This is all just hypothetical. The only thing I'm trying to accomplish is a better understanding of how you could use an accessor that has a protected access modifier.
|
|
|
|
|
If you need to change the values, you have three options:
- Remove the
protected modifier from the <set>set accessors. The properties can then be set from any code.
abstract class Polygon
{
public double Length { get; set; }
public double Width { get; set; }
abstract public double GetArea();
} - Add a public method to the
Polygon or Rectangle class to change the property values. This method can then be called from any code.
abstract class Polygon
{
public double Length { get; protected set; }
public double Width { get; protected set; }
abstract public double GetArea();
public void ChangeDimensions(double newLength, double newHeight)
{
Length = newLength;
Height = newHeight;
}
} - If you want to preserve the immutability[^] of the class (Hint: This is almost always a good idea!), you'll need to create a new instance of the class with the new dimensions.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Perfect! That was exactly the explanation that I needed. I'm really disappointed that my instructors were unable to answer this. Looks like I'll be teaching myself...
|
|
|
|
|
This was a great question that led through to a very good explanation. I, for one, learnt something from this discussion.
Keep asking well thought questions like that here and you will learn...a lot!
I don't speak Idiot - please talk slowly and clearly
"I have sexdaily. I mean dyslexia. Fcuk!"
Driven to the arms of Heineken by the wife
|
|
|
|
|
I'm sure I'll have plenty more. Stay tuned!
|
|
|
|
|
At my windows application reporting services project:
I have two reports: reportA and reportB and one report viewer.
I want to change these 2 reports according to my choose from a combo box.
I can display reportA, but then when want to change to reportB,
this error message displays: The source of the report definition has not been definition.
I used :
reportViewer1.Reset();
reportViewer1.LocalReport.DataSources.Clear(); reportViewer1.LocalReport.ReportEmbeddedResource = "TERMINAL_BENDING_APPLICATION.Report2.rdlc";
this.reportViewer1.RefreshReport()
I used everything but still the same. Please help me.
|
|
|
|
|
Please do not cross post.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
hello, i have a question. I have an web application(project A) and console application(Project B), when the user input 3 value in 3 different textbox in Project A and press submit button, how do i run the exe file and pass that value into console application(Project B).
|
|
|
|
|
You need something intermediary. Your best bet in this case being a file. The web app writes to it, the console reads from it. A database would be a good (or even better) alternative, especially if there is already one attached to the webapp.
If this is something for production you'll probably need to some metadata to the file like sessionid, datetime stamp and stuff like that. And you'll need to handle some logic. (which numbers have been processed, don't process things that are currently being written (incomplete), ... )
You could also start your console from the webapp probably and pass the numbers as arguments...
If a console can listen to windows messages you could use that construct.
Hope this gives some ideas.
|
|
|
|
|
Are you saying ... "run the exe file" ... you want the Web Application to start the Console Application each time you want to pass the TextBox content ? Or, is the Console Application always running when the Web App is running ?
« I had therefore to remove knowledge, in order to make room for belief » Immanuel Kant
modified 1-Oct-14 5:55am.
|
|
|
|
|
yeah, I want the console application run every time i pass the textbox content.
|
|
|
|
|
Member 11025924 wrote: I want the console application run every time i pass the textbox content. I think this is a mistake, and Kornfeld explains why, eloquently, in his responses to you.
You might think about having one Application launch both the Console Application and a WebBrowser (using Process.Start), then doing something to notify the Console App when you wish to pass out the TextBox values (FileWatcher ?), but I think there'll be security issues there, also. Allowing a browser to write arbitrary files in local storage is ... an issue.
« I had therefore to remove knowledge, in order to make room for belief » Immanuel Kant
modified 1-Oct-14 5:55am.
|
|
|
|
|
When you user input his values he is on the browser, on his own pc! Do you want to run the console app there? Or post the values back and run the console app on the server?
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
|
Some important point...
1. Running exe from within a browser's page is seen as unsafe behaviour
2. In some browser this not supported at all (at least without some add-ins or Java/Silverlight like frameworks)
3. Even in browsers support running the exe it can be blocked by user configuration (and for most disabled by default as a security issue)...
4. The exe you want to run should be installed on client machine - it will not come over the web page
For summary - it smells like a very bad ide/design...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Thanks for the sharing. But it is possible to do it programmatically?
|
|
|
|
|
It depends on the tools you are using...
As much as I know Chorme and Firefox will not let you to do it using only JavaScript, but IE has its ways...
You can create a Java block that will do it and it will work for all browsers (probably)...
I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)
|
|
|
|
|
Kornfeld Eliyahu Peter wrote: but IE has its ways...
Not any more. ActiveX controls in new browsers are no longer supported.
|
|
|
|