|
if(obj1 is B) should work for you
|
|
|
|
|
|
As lukasz_nowakowski says, is is what you want. The alternative is as
if (obj is B)
{
B bInstance = (B) obj;
...
} or
B bInstance = obj as B;
if (bInstance != null)
{
...
} You can use either, I prefer the second as I prefer explicit null tests. That's just personal preference though.
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
If you want to just add it to objects' list, then is is enough, but if you want to use object obj as instance of class B, then B instance = obj as B; is the path to follow. To find out why read for example this[^].
|
|
|
|
|
The work with polymorphic objects after comparison is very clear to me.
I just didn't know how to compare them in C#...
Thanks!
|
|
|
|
|
FYI - while the use of is and as are what's appropriate for your problem, as lukasz_nowakowski and OriginalGriff have said, your example code used GetType ... which put me in mind of how to do a similar test on Types, rather than objects:
Type typeOfC = typeof(C);
Type typeofSomeObject = someObject.GetType();
if (typeOfC.IsAssignableFrom(typeofSomeObject))
{
}
|
|
|
|
|
Hello everybody!!!
we have a problem with crystal reports and c#.
we would like to pass the data to the report directly with parameters, but we don't know how to add the pages in the report.
this is the code we have. we pass the parameters to the report witin a forreach, but we just get a report with only one page and with values of the last row of the dataset in the report.
Any idea please???
try
{
SqlDataAdapter daCategory = new SqlDataAdapter();
daCategory.SelectCommand = new SqlCommand();
daCategory.SelectCommand.CommandType = CommandType.StoredProcedure;
daCategory.SelectCommand.Connection = kon.SqlConn;
daCategory.SelectCommand.CommandText = "asp_admin._DatosReport";
SqlParameter parameter = daCategory.SelectCommand.Parameters.Add("@numero", SqlDbType.Int);
parameter.Value = instalazioa;
parameter = daCategory.SelectCommand.Parameters.Add("@Fecha", SqlDbType.DateTime);
parameter.Value = Convert.ToDateTime(FechaElegida);
DataSet ds = new DataSet();
daCategory.Fill(ds, "Termico");
if (ds.Tables["Termico"].Rows.Count > 0)
{
ParameterValues CurrentParameterValues = new ParameterValues();
foreach (DataRow rw in ds.Tables["Termico"].Rows)
{
NInst = new ParameterDiscreteValue();
Rep = new ParameterDiscreteValue();
Dev = new ParameterDiscreteValue();
datosActuales.NInstalacion = Convert.ToInt32(rw["NInstalacion"]);
datosActuales.NRepetidor = Convert.ToInt32(rw["NRepetidor"]);
datosActuales.NDevice = Convert.ToInt32(rw["NDevice"]);
//meter los valores de las variables
NInst.Value = datosActuales.NInstalacion;
//reporte.SetParameterValue("Instalacion", NInst);
Rep.Value = datosActuales.NRepetidor;
//reporte.SetParameterValue("Rep", Rep);
Dev.Value = datosActuales.NDevice;
// reporte.SetParameterValue("Dev", Dev);
CurrentParameterValues.Add(NInst);
CurrentParameterValues.Add(Rep);
CurrentParameterValues.Add(Dev);
}
}
}
finally
{
kon.closeConexion();
}
//se crea el report en pdf
reporte.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "Termico");
}
|
|
|
|
|
|
I have stumbled on some bug with IntelliSense in Visual Studio 2005.
The bug/problem is that IntelliSense don't display the drop-down-list for a class instance returned by a method.
Here is a sample code (Don't mind the compile error in the Main()-method, we will get to it):
class Program
{
static List<Car> carList = new List<Car>();
static Cars cars;
static void Main(string[] args)
{
carList.Add(new Car("Black", "Volvo"));
carList.Add(new Car("Blue", "Volvo"));
carList.Add(new Car("Black", "Audi"));
cars = new Cars(carList);
string c = cars["Volvo"]
foreach (Car car in cars["Volvo", "Blue"])
Console.WriteLine(car.Color + " : "+ car.Model);
}
}
class Cars
{
List<Car> carList;
public Cars(List<Car> carList)
{
this.carList = carList;
}
public Car this[string model]
{
get
{
foreach (Car car in carList)
{
if (car.Model == model)
return car;
}
return null;
}
}
public List<Car> this[string model, string color]
{
get
{
List<Car> cars = new List<Car>();
foreach (Car car in cars)
{
if (car.Model == model && car.Color == color)
cars.Add(car);
}
return cars;
}
}
}
class Car
{
string color;
string model;
public Car(string color, string model)
{
this.color = color;
this.model = model;
}
public string Color
{
get { return color; }
set { color = value; }
}
public string Model
{
get { return model; }
set { model = value; }
}
}
Notice the cars["Volvo"] in the Main()-method which returns a Car-type instance. I want to pass the Color property to the string c variable.
When I want the drop-down-list to show the properties of the Car instance cars["Volvo"]. it's gone.
I can still pass the car instance's Color property to the string c variable but I have to manually type: .Color; .
I dug in this a bit deeper and found out if you comment out:
public List<Car> this[string model, string color] property in the Cars class,
the IntelliSense is back working and the cars["Volvo"]. displays the drop-down-list.
I made a new property in the Cars class.
The public List<Car> this[string model, string color] was still commented out.
public List<Car> this[int amounth]
{
get
{
List<Car> list = new List<Car>();
for (int i = 0; i < amounth; i++)
list.Add(carList[i]);
return list;
}
}
Same result in the Main() method.
I guess this is a bug in the IntelliSense. Can I fix this in some way?
/Steffe!
|
|
|
|
|
You can buy a 3rd party intellisense tool. The only one I know of is Visual Assist by Whole Tomato Software[^]. That's the only way to fix Intellisense in Visual Studio.
.45 ACP - because shooting twice is just silly ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001
|
|
|
|
|
|
ReSharper can also replace Visual Studio's intellisense (at least I think it does...it may just "enhance" its functionality).
The shout of progress is not "Eureka!" it's "Strange... that's not what i expected". - peterchen
|
|
|
|
|
Hello,
I developed a windows application on VS2010 using .net framework 4
How can i convert it to .net framework 3?
|
|
|
|
|
Have a look here[^].
My signature "sucks" today
|
|
|
|
|
If you are using any of the features that were introduced in .NET 4 (e.g. SortedSet), then you need to convert that portion of the code.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
I've got a central SQL server database. My solution has a WCF service responsible for synchronizing data with clients running SQL Server or SQL Server Express.
The service uses Microsoft Sync Framework and it works well, but the Sync Framework requires a direct connection to my client's server, and clients that are running SQL Server Express may not have a static IP address to let my application locate them.
How can I overcome this problem? Does Sync Framework have an intermediate service that can exchange datasets of changes on both server and client? Or can I use Sync Framework to get datasets of changes?
Dad
|
|
|
|
|
for example, server chars in the image.
How to get the char?
|
|
|
|
|
After spending around 1 hour I finally found who's causing the problem. And it was TextureBrush which has something wrong, maybe not ?
Check this image
http://tinypic.com/r/2uzrrqx/6[^]
I want to draw tiles of it on a big image but on specific region, so to do that...I wrote the following code(for testing only)
Bitmap b = new Bitmap(512, 300);
Graphics g = Graphics.FromImage(b);
TextureBrush tb = new TextureBrush(Image.FromFile(@"C:\dsfsdf.png"));
g.FillRectangle(tb, 80, 0, 512 - 80, 300);
tb.Dispose();
g.Dispose();
pictureBox1.Image = b;
result is
http://tinypic.com/r/mifb5h/6[^]
now the problem is, TB is drawing from (0, 0) offset and trimming the outer part of rectangle. But it should draw from (80, 0). I know I can do that by changing its offset(tb.TranslateTransform(80, 0)) but why this is happening in first place. That suppose to be default.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
I'm not convinced anything is wrong (except for a anisotropic scale factor)
I expect a TextureBrush to repeat the input image (like rolls of wallpaper), with a fixed phase, i.e. the texture gets anchored to the origin of the destination, so separate pieces would stay "in phase".
what is causing the solid pink color?
check your PictureBox sizemode.
best is to (temporarily) set PictureBox SizeMode to AutoSize, to convince yourself the image is right (or wrong)
well, actually best is not to use a PictureBox at all, they seem to always confuse people.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: what is causing the solid pink color?
pink is background color, just selected to make PB different.
AFAIK, TB's default is that it creates images from (0, 0) and not from the rectangle origin we specify in param which should be default.
TVMU^P[[IGIOQHG^JSH`A#@`RFJ\c^JPL>;"[,*/|+&WLEZGc`AFXc!L
%^]*IRXD#@GKCQ`R\^SF_WcHbORY87֦ʻ6ϣN8ȤBcRAV\Z^&SU~%CSWQ@#2
W_AD`EPABIKRDFVS)EVLQK)JKQUFK[M`UKs*$GwU#QDXBER@CBN%
R0~53%eYrd8mt^7Z6]iTF+(EWfJ9zaK-iTV.C\y<pjxsg-b$f4ia>
-----------------------------------------------
128 bit encrypted signature, crack if you can
|
|
|
|
|
Hi ever1,
Do you know whats the problem of this code. I am trying to print out the Base class variable via Derived class. Is it Possible?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Person
{
protect string name;
protect string family;
protect int age;
public string _name
{
get { return name; }
set { name = value; }
}
public string _family
{
get { return family; }
set { family = value; }
}
public int _age
{
get { return age; }
set {
age = value;
if (value > 100)
{
throw new ArgumentException(string.Format("Error.... !"));
}
}
}
public Person(string Name, string Family, int Age)
{
this.name = Name;
this.family = Family;
this.age = Age;
}
public Person()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Student:Person
{
private string id;
public string _id
{
get { return id; }
set { id = value;}
}
public Student(string Name, string Family, int Age, string ID)
: base(Name, Family, Age)
{
this.id = ID;
}
public Student()
{
}
public override string ToString()
{
return base.string.Format(name,family,age)+string.Format("\nID:{0}", id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("Allen","Wilson",28);
Student s = new Student();
s._id = "30069802";
Console.WriteLine(p);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
|
|
|
|
|
I don't see any fundamental problem here.
Of course your code is:
1.
not quite accurate; a compiler would throw lots of error messages at you.
2.
rather unusual in ignoring all known coding standards; public methods and properties normally have names starting with a capital, and not an underscore.
3.
slightly wrong in Student.ToString() which needs return string.Format(...) pretty much like the one you commented out in Person.ToString()
Why is it you create such code snippets without compiling them? the .NET framework holds its own compiler (csc.exe) for free, yes it is a command-line tool; but then there also is the free Visual Studio Express Edition which you can freely download and install on any sufficiently modern Windows PC.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
thx for ur reply, I fixed it up. the problem is I passed some value to base class but when i write it it shows me nothing.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Person
{
protected string _name;
protected string _family;
protected int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public string Family
{
get { return _family; }
set { _family = value; }
}
public int Aage
{
get { return _age; }
set {
_age = value;
if (value > 100)
{
throw new ArgumentException(string.Format("Error.... !"));
}
}
}
public Person(string name, string family, int age)
{
this._name = name;
this._family = family;
this._age = age;
}
public Person()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Student:Person
{
private string _id;
public string Id
{
get { return _id; }
set { _id = value;}
}
public Student(string name, string family, int age, string id)
: base(name, family, age)
{
this._id = id;
}
public Student()
{
}
public override string ToString()
{
return string.Format("Name: {0}\nSurname: {1}\nAge: {2}\nId: {3}", _name, _family, _age, _id);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Inheritance_practice
{
class Program
{
static void Main(string[] args)
{
Person p = new Person("Ali","Moradpour",28);
Student s = new Student();
s.Id = "30069802";
Console.WriteLine(p);
Console.WriteLine(s);
Console.ReadKey();
}
}
}
|
|
|
|
|
you really should be more accurate and specific. What is it you pass, what do you expect, what do you get?
Here is my take on things: you have created two people, and hope to see them both listed, however
1. Console.WriteLine(p); will only show the exact type of Person, as that class did not override its ToString method. You should uncomment the ToString stuff within Person.
2. Console.WriteLine(s); will show a student's ID but no name or age, as you created the Student instance by calling its constructor-without-parameters, and later provided the ID information.
If that is what you see, everything is fine; may not be what you want, but it is what you ordered.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Hi Folks,
on to the second of tonight's threading queries...
Does anyone know of a suitable pattern for exception handling where threads are involved, or if there's a special kind of exception which can bubble up into the parent thread?
class Program
{
static void Main(string[] args)
{
try
{
Demo demo = new Demo();
Timer timer1 = new Timer();
timer1.Interval = 3000;
timer1.Elapsed += new ElapsedEventHandler(demo.Run);
timer1.Enabled = true;
}
catch (Exception e)
{
Console.WriteLine("You should write an exception handler");
}
Console.ReadKey();
}
}
class Demo
{
public void Run(object source, ElapsedEventArgs e)
{
try
{
System.Threading.Thread.Sleep(2000);
DoSomething();
System.Threading.Thread.Sleep(2000);
}
catch (Exception e)
{
throw new IllTellMyDadThreadException(e);
}
}
private void DoSomething()
{
throw new Exception("aaaugh");
}
}
Thanks once again for any suggestions,
JB
|
|
|
|
|