|
Would you mind explaining the code?
|
|
|
|
|
I have a gridview in which I need to combine two columns in one. One field is an true/false, this column I would like to add the string "Office" if it is true. The other field is a string. The problem I am having is that the field in the grid displays true/false and not the string "Office". I am very new to C# in regards that I am using SQL Server to retieve this data. I am sure that the select statment is correct. But the source on the design page is where my problem is...I think. Can you help?
|
|
|
|
|
I'm assuming your data source for this grid is a DataTable. Perhaps adding a DataColumn with an expression would help.
More Info[^]
|
|
|
|
|
Evening all (depending where you are of course !)
This is going to be tricky to explain but i'll give it my best.
Please bear in mind i am a novice to C#.
I'm going to run a loop to create a 'List' of 10 animals.
I'm using polymorhism to then cast each animal the type i need (cat, dog, etc).
newAnimals[0] = new Cat();
newAnimals[1] = new Cat();
I'm confused as to how i can then give each object a reference name so that i can reference individual object's and invoke methods on them ?
For instance - say i had 5 animas in the list and the first 2 were cats.
I need to be able to say -
cat1.eat();
cat2.play();
Is there a way i can do this ?
Thanks in advance
(loving the forum by the way !!)
|
|
|
|
|
No you can't (I don't think so), except you declare 5 variables like:
cat1 = newAnimals[0];
cat2 = newAnimals[1];
...
Then you can use these variables. However newAnimal[0].eat should work for you...
Life is 5: 3 me, 1 you.
Trying to find the missing part is the meaning of Life.
And sadness is when you find that part!
|
|
|
|
|
If you need to tell which animals are of what type to do something specific with them, then your inheritance tree is probably broken. You can add methods like GetAllCats, tho. Then you can do this:
foreach(Animal animal in newAnimals)
{
Cat c = animal as Cat;
if (c != null)
{
// Add c to a list to return, or do something with c
}
}
The as keyword attempts to cast an object as a derived object, and returns null if it fails.
I would also use a list instead of an array, so you can add as many animals as you like, unless your class requires you to use arrays at this point.
If you just mean accessing the objects, then newAnimals[0], newAnimals[1] is how you reference them.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks Cristian,
Your reply was v-helpful.
I've since tried
newcat1 = zooAnimals[0] as Cat;
but this doesn't seem to work either.
Can i not cast the object in this way ?
|
|
|
|
|
What does 'does not work' mean ? If zooAnimals is an array of Animals, and Cat is a class derived from Animal, then so long as newcat1 is a Cat and zooAnumals[0] is a Cat, it should work fine.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Cristian,
my appologies for not being very specific - still getting used to this 'posting' bit !
Ok - my code goes -
Animal newcat1;
zooAnimals = new List<Animal>();
for (i = 0; i < 2; i++)
{
Animal animal = new Animal();
zooAnimals.Add(animal);
}
newcat1 = zooAnimals[0] as Cat;
newcat2 = zooAnimals[1] as Cat;
the program then throws a 'Null Reference - Object reference not set to an instance of an object.'
When i hover the pointer, the 'newcat1' is still set to null
|
|
|
|
|
Well, you're adding Animal s to the list, not Cat s.
So what you need to do is:
Cat newcat1;
zooAnimals = new List<animal>();
for (i = 0; i < 2; i++)
{
Animal animal = new Cat();
zooAnimals.Add(animal);
}
newcat1 = zooAnimals[0] as Cat;
newcat2 = zooAnimals[1] as Cat;
</animal>
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
Hi there,
I was looking over your post and I think this is a sample of how to accomplish what you are looking for. Look this over:
using System;
using System.Collections.Generic;
public abstract class ZooAnimal
{
public abstract string Speak();
}
public class Cat : ZooAnimal
{
public override string Speak()
{
return "Meow";
}
}
public class Lion : Cat
{
public override string Speak()
{
return "Rowr!";
}
}
public class Program
{
public static void Main(params string[] args)
{
List<ZooAnimal> zooAnimals = new List<ZooAnimal>();
for (int i = 0; i < 2; i++)
zooAnimals.Add(new Cat());
for (int i = 0; i < 3; i++)
zooAnimals.Add(new Lion());
for (int i = 0; i < zooAnimals.Count; i++)
{
Lion lion = zooAnimals[i] as Lion;
if (lion != null)
Console.WriteLine("The lion says: " + lion.Speak());
else
{
Cat cat = zooAnimals[i] as Cat;
Console.WriteLine("The cat says: " + cat.Speak());
}
}
Console.Read();
}
}
See this is all just a matter of inheritance and making sure you instantiate your types correctly. In your code example you are adding two animals to the collection but trying to get out two cats. Remember - all cats are animals but not all animals are cats. To get the code you posted to work correctly, you would need to add cats to the zooAnimals collection instead of just animals. This works just fine in C# by the way - an object declared as a base type can always hold an instance of one of its derived types. To illustrate I created a three tiered inheritance example. Notice that in the above example you have ZooAnimals, Cats, and Lions. ZooAnimals is an abstract type, it exists only to serve as a base class for anything that I might consider a zoo animal. Then you have the Cat class. This is also a pretty general class - there are many types of cats, so I derived another class from Cat called Lion. Notice the hierarchy - All Lions are Cats but not all Cats are Lions (illustrated in the final for loop in the static method Main). All Cats are ZooAnimals (as far as this program is concerned at least) all ZooAnimals are not necessarily Cats. It must also follow then that since all Lions are Cats, and all Cats are ZooAnimals, then all Lions are ZooAnimals as well. Hope that helps!
"We are men of action; lies do not become us."
modified on Friday, November 21, 2008 11:39 PM
|
|
|
|
|
I think perhaps you should do some reading on OO. As others have said, you are trying to turn an animal into a cat. It's not a cat, you didn't ask it to be.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hey again.
I know i'm a pain for asking for help twice within a short period of time, but if I can get this working then it should be all the help I need.
I basically want to constrict the location or where the controls are drawn on a form. Ie, I want controls that are added to a form to only be drawn within a certain rectangle, and if they are placed outside of or hanging out of that rectangle, that section of the controls to not be drawn at all.
All the functions and properties built into the Form that reference to ClientSize are based solely for the entire form, but I want to be able to define a custom region for controls within the form to be drawn.
Here's an image example of what I mean[^]
I'm wondering whether I could control the true region from within the OnPaint function, or by using commands in WndProc to define the area to draw controls within.
If anyone can help, thanks a ton!
|
|
|
|
|
Could you not just put all your controls into a panel then arrange the panel so it defines the client area you need? The alternative is to use WM_NCCALCSIZE win32 message to define the client rectangle offset relative to the non-client bounds, but this is more complicated! (see NCCALCSIZE & NCCALCSIZE_PARAMS in msdn/google for more info).
"An eye for an eye only ends up making the whole world blind"
|
|
|
|
|
Yeh I just realised that there was an NCCALCSIZE windows message so i'm implementing the native methods now, hopefully it should work
Thanks
|
|
|
|
|
I am planning to do the following:
I will create some buttons or pictureboxes in a form, each of them represents one moudlue or function, then they will be connected to
do more functions, somehow like CAD.
Now I want to know what kind of controls (button, picture box and others) are better to be used in considering the displaying efficiency, especially when all these controls are refreshed.
thanks.
|
|
|
|
|
If you are looking for complete efficiency you should use DirectX to draw your own controls instead of GDI+.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|
|
Hey!
I have a form that uses some pretty heft painting, it draws a custom caption, close, minimize, maximize buttons and border.
It works really perfectly (lagless) when resizing the form, but when I move the form offscreen slightly and move it back in, it draws the bottom border (or the border that was moved off-screen) repeatedly onto the form's body.
My solution to this was to apply Invalidate() into the Form.Move event, but as you can imagine it lags the form heavily when you move the form now. It doesn't lag it to the point that it's slow as such, but it does apply quite a bit of memory (sometimes up to 11k memory whereas on resize it's only around 5k).
Is there any way that I could possibly prevent the form from drawing the borders repeatedly over the body (which looks ugly) without having to completely invalidate the form everytime the form is moved?
Thanks.
|
|
|
|
|
Invalidate region and create a custom region that is bigger than the form and slightly smaller.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|
|
Hm? I don't quite understand what you mean, could you elaborate a bit please?
Thanks
|
|
|
|
|
Sorry. Make one rectangle larger than your entire draw area, then make one slightly smaller. Combine them into a region that excludes the inside portion of the smaller rectangle and then call invalidate and pass in your region. The form will only draw the outer portion which will be very fast.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.
|
|
|
|
|
Mm that didn't help D:
Here's a screenshot of the problem:
Screenshot of the moving problem[^]
All I did was move the form below the taskbar/off the bottom of the screen so it draws the bottom border (which is custom drawn) onto the body repeatedly. Then when I do something to raise the OnPaint event again, the form is drawn properly. It's only when the actual form is being moved off the screen that it draws it like that.
I think it's because, when the client is moved off the screen the ClipRectangle in OnPaint is reduced to only the visible area on the screen, so the border is drawn where the end of the client is a the screen.
I might use a new Rectangle rather than ClipRectangle based on
new Rectangle(this.Left, this.Top, this.Width, this.Height); to obtain the full client region, rather than relying on ClipRectangle.
Thank you!
|
|
|
|
|
So you were using the clip rectangle as the size of the window? I expected something like that, because that would definitely explain why the border is drawn inside the window.
The clip rectangle is the area that the Paint event is expected to draw, and that can definitely be smaller than the window. If you for example have another window on top of your window, and your window has to be redrawn, you will get several calls to the Paint event. The visible window area is divided into rectangles, and the Paint event is called to draw each rectangle.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I have a generic class which I use to populate a BindingList which I then use as the DataSource of my DataGridView.
This class inherits from my LINQ table and adds some extra properties such as State etc...
The problem is that I have to creat a new class for every single LINQ table I choose to use this way, I need some way to make my inheritance generic.
This code works for that specific class (but it's not really generic since it only works for "myExampleTable"):
Please not that <<T>> is only written with double brackets because the correct syntax doesn't want to display in CP.
namespace BL.CustomLINQ {
public class StateTracker<<T>> : myExampleTable where T:class {
public enum ErrorState {
NoError = 0,
RowIsIncomplete = 1,
RowValidationFailed = 2
}
public int state{get;set;}
public ErrorState RowErrorState { get; set; }
public StateTracker() : base() { }
}
} But as I said, this code is useless for me!
This is what I really want to do, but I'm doing it wrong:
namespace BL.CustomLINQ {
public class StateTracker<<T>> : T where T:class {
public enum ErrorState {
NoError = 0,
RowIsIncomplete = 1,
RowValidationFailed = 2
}
public int state{get;set;}
public ErrorState RowErrorState { get; set; }
public StateTracker() : base() { }
}
} Note that I'm inheriting from the same class as the one I passed in as the generic.
The error I get (underlining T) is: Cannot derive from 'T' because it is a type parameter.
Is there any way to do this or should I try a different (less appealing ) approach?
|
|
|
|
|
I don't think this could work. Inheritance is handled at compile time while Generic type parameters are resolved at run time. There's no way for the compiler to know what the base class of of StateTracker<T> would be, and therefore what the functional interface to that base class would be. For example, in your default constructor you are explicitly calling the default constructor of the base class - but what if type T does not define a default constructor or the default constructor is hidden (private)? In my opinion you would be better off to create your StateTracker class to contain an element of Type T and interact with it in this manner even if C# did allow you to derive from a Generic type.
"We are men of action; lies do not become us."
|
|
|
|