|
hi
I tried to write code that implicitly convert a generic type to another but it failed with interface. With subclass it worked, i thought it should be the other way.
public class ConvertibleClass<T>
{
T holder;
public ConvertibleClass(T obj)
{
holder = obj;
}
public static implicit operator ConvertibleClass<T>(T obj)
{
return new ConvertibleClass<T>(obj);
}
public static implicit operator T(ConvertibleClass<T> obj)
{
return obj.holder;
}
}
public interface IGenericInterface<T>
{
}
public class GenericClass : IGenericInterface<AnyClass>
{
public GenericClass(int id)
{
ID = id;
}
public int ID { get; set; }
}
public class AnyClass
{ }
In this code i need any class that implements 'IGenericInterface<T>' to be converted to 'Convertible' class and vice versa.
But!!
public void DoSomthing(IGenericInterface<AnyClass> doSomthingWithThis)
{
List<ConvertibleClass<IGenericInterface<AnyClass>>> ListOfMedies = new List<ConvertibleClass<IGenericInterface<AnyClass>>>();
ConvertibleClass<IGenericInterface<AnyClass>> item = new GenericClass(4);
ListOfMedies.Add(new GenericClass(1));
ListOfMedies.Add(item);
ListOfMedies.Add(new GenericClass(2));
item = doSomthingWithThis;
ListOfMedies.Contains(doSomthingWithThis);
ListOfMedies.Add(doSomthingWithThis);
}
And code for calling the method
DoSomthing(new GenericClass(4));
I cant figure it out. HELP!
And THX
|
|
|
|
|
The generic interface is a red-herring. Your code will fail even with a non-generic interface, because the C# specification explicitly forbids it:
A class or struct is permitted to declare a conversion from a source type S to a target type T provided all of the following are true:
- S and T are different types.
- Either S or T is the class or struct type in which the operator declaration takes place.
- Neither S nor T is object or an interface-type.
- T is not a base class of S, and S is not a base class of T.
...
User-defined conversions are not allowed to convert from or to interface-types. In particular, this restriction ensures that no user-defined transformations occur when converting to an interface-type, and that a conversion to an interface-type succeeds only if the object being converted actually implements the specified interface-type.
Eric Lippert posted a great explanation for this restriction on StackOverflow:
More on implicit conversion operators and interfaces in C# (again) - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks alot, so i should find another way.
|
|
|
|
|
GOOD BY
modified 26-Apr-16 11:17am.
|
|
|
|
|
So you have one user, and his username is "textUsername.Text", and an SQL system that doesn't mind errors?
Look at your code:
SqlDataAdapter sda = new SqlDataAdapter("select count(*) from Login Whare Username = textUsername.Text ; Password = textPassword.Text; +", conn);
DataTable dt = new System.Data.DataTable(); Dump some of it to make it more obvious:
string s = "select count(*) from Login Whare Username = textUsername.Text ; Password = textPassword.Text; +"; And you have "just" the SQL you are getting the server to execute.
1) WHERE is spelled with an 'E', not an 'A':
from Login Whare Username
2) This inserts the name of your controls into the command, rather than the contents the user entered. Even if you fix the WHARE problem, it won't work.
3) Semicolon is a statement terminator in SQL, not an item separator:
Whare Username = textUsername.Text ; Password = textPassword.Text
Probably you wanted to say AND instead of ';'
4) What does '+' do as an SQL command? Nothing - it will be an unexpected character.
5) Even if it did work, never store passwords in clear text! It is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] Also see here: Code Crime 1[^]
6) Don't hard code connection strings: They should be in settings files, not in the code.
7) Attaching a DB file is a bad, bad idea. It starts a new instance of your local DB server and will only work with Local SQL Express installations - it's also very much inefficient.
Make the DB a part of SQL by creating it in SQL and let it manage it.
8) SQL Connections and so forth are a scarce resource - and you are responsible for clearing up after yourself. Always Dispose of them when you are finished with them.
9) Fix all that, and your code is going to be vulnerable to SQL Injection. Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Follow the link at (5) and you will see much better code to do all this.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
|
I don't know where you learn C#, the most c# I learn on You tube.
If I want to go to school I will built my own Visual Studio and new Computer System
If you answer at my massage in this way "You are dump" and will be stupid for ever, doesn't matter how much you know C#.
David
|
|
|
|
|
Don't try to learn C# from youtube - get a book, or better go on a course. They present the material in a coherent manner, moving from the simple to the more complex. Learning from random youtube videos is like trying to become an F1 driver by stealing a car and driving the wrong way up the motorway / interstate so the police won't chase you! It might work. You might survive. You might even get away. But when you come to the first real bend, you are going to crash.
It's pretty clear from your code that the youtube stuff you have looked at doesn't cover any basics, or covers them so badly they might have well not bothered, or you got bored and skipped it. Because that code fragment is so full of basic errors that anyone who has got to the point of talking to a database should know exactly how to fix themselves by now!
No one is calling you "dump" (whatever the heck that means), no one is saying you are stupid. But...if you don't listen when people point out problems, then there isn't a lot of point you wasting your time by asking the question, is there? Never mind our time to answer it...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
To OriginalGriff, that message was not for you, I appreciate your answer, I try your suggestion do not work, thanks I will find the problem on my own built computer I don't have to bother no body any more.
DB.
|
|
|
|
|
This is a public forum, not a private message board!
Anyone can (and is often encouraged) to reply to anyone.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Yes ,It's Public Forum.
The greatest Invention was made IN PRIVATE GARAGE NOT ON FUBLIC FORUM.
D.B.
|
|
|
|
|
Fire was not invented in a garage.
This space for rent
|
|
|
|
|
Like Griff, I also recommend books over YouTube videos. This is a good, free PDF-book on programming in C# for beginners: Introduction to Programming with C# / Java Books[^]
It doesn't cover database, UI, and other advanced fields of programming but C# language constructs (e.g. LINQ) and the most important classes from the .NET framework (e.g. dictionaries). So it's not exactly helping you here with database programming but I assume you'll find it helpful nonetheless.
I didn't call you dumb and I didn't mean my message to sound like it. The only thing I'm saying is if you're asking a question here and you get an answer, it would be appreciated if you actually read that answer and follow the given advice before posting the next question about the same thing. If you have difficulties of any kind in making use of an answer you're free to ask for clarification - and I would have provided it.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
This is the data in the list box, from a database
Johnie Black
Sarah Smith
Carol Willis
Maggie Dubois
This is the data in the combo Box
(M)
(F)
I want to select a name in the listbox then when I proceed to select the gender from the comboBox the value I select must be added to the end of the name that is selected
example.
Carol Willis(F)
Any form of help will be appreciated
|
|
|
|
|
On 'selectedIndexChanged' event of combo box, get selected text from it and append it to selected text of Listbox
Note: There is no direct way to append text, you need to first remove selected name from listbox and then add new name with gender
-hope it helps
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
|
A few comments:
1. consider the design of displaying the abbreviation for the gender at the end of the name. If you have a synchronized ComboBox/ListBox isn't it enough that your current choice in the ListBox exposes its gender value in ComboBox ?
2. Have you worked with DataBinding these Controls: that's the way to get them synchronized; in WinForms that does have a certain over-head: you may have to reset the binding state as the data type you bind to changes its internal values.
3. Consider using a Dictionary where the Keys are strings, and the Values are either strings or an Enum.
public enum GenderType
Indeterminate,
Ambivalent,
RefuseToState,
Male,
Female,
Hermaphrodite,
TransPreOpMToF,
TransPreOpFToM,
TransPostOpMToF,
TransPostOpFToM,
Eunuch,
Alien
}
private Array EnumTypeValues { set; get; }
private BindingSource EnumBindingSource { set; get; }
public void SomeMethod(Type enumtype, ComboBox combo)
{
EnumTypeValues = Enum.GetValues(enumtype);
EnumBindingSource = new BindingSource();
EnumBindingSource.DataSource = EnumTypeValues;
combo.DataSource = EnumBindingSource;
combo.DisplayMember = "Name";
combo.SelectedValueChanged += ComboOnSelectedValueChanged;
}
private void ComboOnSelectedValueChanged(object sender, EventArgs eventArgs)
{
GenderType gType = (GenderType) comboBox1.SelectedValue;
switch (gType)
{
case GenderType.Indeterminate:
break;
case GenderType.Male:
break;
case GenderType.Female:
break;
case GenderType.Ambivalent:
break;
case GenderType.Alien:
break;
default:
break;
}
}
«There is a spectrum, from "clearly desirable behaviour," to "possibly dodgy behavior that still makes some sense," to "clearly undesirable behavior." We try to make the latter into warnings or, better, errors. But stuff that is in the middle category you don’t want to restrict unless there is a clear way to work around it.» Eric Lippert, May 14, 2008
|
|
|
|
|
BillWoodruff wrote: public enum GenderType
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I want to develop an app that gets employee-data from the SharePoint - My Site Collection and I'd like to change their properties through webservice communication.
What is the best practice to get me going?
I'm searching for a sample of:
1. The webservice communication
2. A get function to retrieve info
3. A set function to set properties
If anyone has the experience and can help me with this, that would be gold.
|
|
|
|
|
Member 12482866 wrote: What is the best practice to get me going? Hook up with MSDN[^]. Using that link you can ask what sites are in a collection, enumerate them, and get/set some properties.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
I need a simple code...that will just get selected item data od my listView column selected item.
i have few items on listview colum
Hello| World | What | is
when i will click on HELLO column it will show "Hello" on messagebox.
Thanks
|
|
|
|
|
Sorry, but this website isn't a code to order service. The purpose of these forums is to help with problems you encounter while trying on your own.
Give it a go yourself and if you encounter a problem, post your code and we will try to help you.
You might also search the abundance of articles here to get some inspiration or maybe even find exactly what you're looking for.
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
I am really sorry.
=>Give it a go yourself and if you encounter a problem, post your code and we will try to help you.
You are right. i should post my code here first...Sorry for my current work.
Thank you for you advice... i am going to correction it
|
|
|
|
|