|
I have several supporting classes that are all exactly the same except for one property, which is a List, something like this :-
public class DecimalClass
{
public int x;
public string y;
public List<decimal> Details;
}
public class StringClass
{
public int x;
public string y;
public List<string> Details;
}
Now in my main class I need to have several of these base classes , and so I thought of having one generic class like so :-
public class MyClass<T>
{
public int x;
public string y;
public List<T> Details;
}
and having a List<MyClass<T>> myClasses in my Main class, but this would mean the List would only be able to hold one Type, and I would need it to hold several different Types, the total number and Types of which would remain unknown until the class is created further down the line. If anyone can understand what I am trying to do, and can suggest a solution I would be more than grateful, as at the moment I am having to create a main class for each combination of sub classes that are required.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Hmmmm. Ultimately all classes derive from object , so you could store the data in a List<object>, but this wouldn't give you any advantage over storing as an ArrayList. This means you'd have a lot of boxing/unboxing to take care of, and you'd end up having to use reflection to work out what type to convert it back to.
Alternatively, you could store it in a Dictionary<Type, List<object>> and use that to avoid having to use the reflection part. Just a thought.
|
|
|
|
|
Hi Pete,
Thanks for your input. I think I will have to go with List<object> , but decided to pass the Type in as a parameter to the constructor for the sub class with a private Type variable, as it meant the least changes to existing code, though if I had to start from scratch again, I would have used the dictionary as you suggested. Thanks again for your time.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Make the base class
public class MyBaseClass
{
public int x;
public string y;
public IEnumerable Details;
} And derive the generic class from it.
public class MyClass<T> : MyBaseClass
{
public MyClass<T>()
{
Details = new List<T>();
}
} That way, you could hold a List<MyBaseClass> baseClassList in your Main class.
Ciao,
luker
|
|
|
|
|
Unfortunately, holding a List of MyBaseClass I would need to use reflection to cast from MyBaseClass to MyClass in the Main Class and so I would be back to where I started, as I need to know the Type of object I am dealing with. Thanks for your thoughts.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Don't cast. Use polymorphism instead.
Declare the methods you need in MyBaseClass and override them in MyClass<T> .
Calling baseClassList[0].SomeMethod() will cause the correct override to execute.
Ciao,
luker
|
|
|
|
|
I think you need to explain more thoroughly what you are actually trying to do. The list of 'lots of stuff' seems confused and that should almost certainly be a List<BaseClass> (or an interface, possibly) where BaseClass defines all the things that you want to do on the different MyClass types.
Similar to the answer above, I would factor out the common, non generic parts into a base class (note that this isn't necessarily the same BaseClass referenced in the first paragraph, though it could be; the question is not detailed enough to be able to tell):
public abstract class BaseClass {
public int x;
public string y;
public virtual IEnumerable BaseDetails { get; set; }
}
public class MyClass<T> : BaseClass {
public IList<T> Details { get; set; }
public override IEnumerable BaseDetails {
get { return Details; }
set { Details = (IList<T>)value; }
}
}
BaseClass should also define virtual methods for whatever operations you are going to want to do on things pulled out of the list.
|
|
|
|
|
Maybe this helps:
public class Test {
public void Run() {
List<MyBase> all=new List<MyBase>();
all.Add(new MyGeneric<int>());
all.Add(new MyGeneric<string>());
foreach(MyBase b in all) {
log("Holding a list of "+b.MyType);
}
}
public class MyBase {
public Type MyType;
public MyBase(Type type) {
MyType=type;
}
}
public class MyGeneric<T> : MyBase {
public int x;
public string y;
public List<T> Details;
public MyGeneric() : base(typeof(T)) {}
}
}
|
|
|
|
|
I would like to create a custom usercontrol with some custom drawing using TextBoxRenderer. All I get with TextBoxRenderer.DrawTextBox() and also with ComboBoxRenderer.DrawTextBox() are single solid gray rectangles not using Visual Styles. When I use ComboBoxRenderer.DrawDropDownButton() the drawing is perfectly using Visual Styles. The Application.EnableVisualStyles() is called in program.cs and TextBoxRenderer.IsSupported returns true. In the code below I added a textbox to show Visual Styles are on.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using System.Drawing;
namespace WindowsFormsApplication1
{
public class UserControl2 : UserControl
{
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
this.BorderStyle = BorderStyle.FixedSingle;
TextBoxRenderer.DrawTextBox(e.Graphics, new Rectangle(10, 10, 50, 50), TextBoxState.Normal);
TextBoxRenderer.DrawTextBox(e.Graphics, new Rectangle(70, 10, 50, 50), TextBoxState.Hot);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, new Rectangle(10, 70, 20, 20), ComboBoxState.Normal);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, new Rectangle(70, 70, 20, 20), ComboBoxState.Hot);
TextBox tb = new TextBox();
tb.Location = new Point(10, 100);
this.Controls.Add(tb);
}
}
}
Using: Win7 Prof 64 bit, visual studio 2008, .net 3.5
Thanks in advance for any suggestions...
|
|
|
|
|
Have you checked the ...Renderer.IsSupported to see if visual styles are supported for you?
|
|
|
|
|
Yes... see text in my post. I removed these if's in my post to keep the listing a bit shorter, but this is what I have
if (TextBoxRenderer.IsSupported)
{
TextBoxRenderer.DrawTextBox(e.Graphics, new Rectangle(10, 10, 50, 50), TextBoxState.Normal);
TextBoxRenderer.DrawTextBox(e.Graphics, new Rectangle(70, 10, 50, 50), TextBoxState.Hot);
}
if (ComboBoxRenderer.IsSupported)
{
ComboBoxRenderer.DrawDropDownButton(e.Graphics, new Rectangle(10, 70, 20, 20), ComboBoxState.Normal);
ComboBoxRenderer.DrawDropDownButton(e.Graphics, new Rectangle(70, 70, 20, 20), ComboBoxState.Hot);
}
|
|
|
|
|
Hello Everybody,
I am using threading in my application. In which have there is a problem is occurred while executing the program for depth Algo.
if i am executing Thread for normal function then it works properly. if i execute the same thread for depth function there are generates the problem of Memory Usage and CPU usage. After that the application can't response and generate not enough memory error.
So please can u provide the help for controls the Thread if the memory is not available.
Thanks
If you can think then I Can.
|
|
|
|
|
Without the relevant code fragment, it isn't possible to answer that with absolute confidence, but I assume that your depth function is recursive, and that it is calling itself with the wrong parameters, causing it to loop on a node of some form.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Manfred R. Bihy: "Looks as if OP is learning resistant."
|
|
|
|
|
I have created a desktop app for facebook.I even got api and secret key from facebook,but it doesnt work for other user.It works good for me but it doesnt allow other user to login into the app.Plz help me
|
|
|
|
|
facebook is evil dont support them your soul will be lost... i hope that is helped you my friend
|
|
|
|
|
Yeah I know brother,but there should be some way to do this
|
|
|
|
|
rahul_galgali wrote: but it doesnt allow other user to login into the app
Without some further information about what your app is doing it is impossible to guess what might be happening.
|
|
|
|
|
rahul_galgali wrote: ,but it doesnt work for other user
What's that supposed to mean? Does it log in with your credentials, does it crash, does it complain about your key, does it say "OutOfMemoryException". What?
rahul_galgali wrote: It works good for me but it doesnt allow other user to login into the app
That could be caused by them not having .NET installed. Once you verified they have, we'll move on the the next point, being the difference between 32 and 64 bits, verifying that you're not running from a mapped network-drive, and so on.
Bastard Programmer from Hell
|
|
|
|
|
Hi
I am writing a form application to just display the movie names. I have a second class called Movie where I have hard coded two movies to dispplay in the Form class. Both Form amd Movies class are in same directlry and have same namespace. But still I am getting msg
"The type or namespacen name Movies does not exist in the namespace MoviesIsaw (are you missing an assembly reference?))
Can you pls tell me what Iam doing wrong.
|
|
|
|
|
Is the class called Movie or Movies ?
I would create both so each movie is an individual object and all movies can be added to a collection called Movies
public class Movie
{
private string name;
public Movie(string name)
{
this.name = name;
}
public string Name
{
get { return name; }
}
}
public class Movies : List<Movie>
{
}
|
|
|
|
|
Thansk muh for swift response. This is what I have
MoviesIsaw.Form1.cs
namespace MoviesIsaw
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string mn;
mn = MoviesIsaw.Movies.MovieArray[0];
testBox1.Text = mn;
}
}
}
Movies.cs
using System;
using MoviesIsaw;
namespace MoviesIsaw
{
public class Movies
{
str [] MovieArray = {"Benher","TOP GUN"};
}
}
|
|
|
|
|
|
Hooray, Tx much it works now. got the concept as well.
Two mistakes I did
did not create the object - Still thinking in my old procedural way. (11 years of it does not die that fast..)..)
Did not include the cs file part of the project
Thanks for the help and am happy about this great site..
cheers, Ravi
|
|
|
|
|
Hello,
It looks like you made a mistake somewhere in the typing and you know how ,use the word "Movies" instead of "Movie" as your seckond class is called.
It would be good to see the piece of code that is related to this error.
All the best,
Peric Zeljko
periczeljkosmederevo@yahoo.com
|
|
|
|
|
Hi,
I am facing some issue to set the IPv6 address programatically to the adapter. For IPv4 Win32_NetworkAdapterConfiguration WMI class was used to set the IP addresss.
Could some one help to share the API or any other way to set the IPv6 address.
Thanks
|
|
|
|
|