|
So you want to call a method from outside of its containing class, without specifying the class name?
Prior to C# 6, you can't. The closest you could get would be to use an extension method:
using YourNamespace;
...
string sGroup = sGroupCodeVariable.SomeExtensionMethod().RoleName;
With C# 6, you could use the using static syntax[^] to make all of the methods of your static class available without qualification:
using static YourNamespace.YourStaticClass;
...
string sGroup = YourStaticMethod(sGroupCode).RoleName;
However, I think this is generally a bad idea. It tends to make your code less readable. Plus, it's too close to VB's "Module" behaviour for comfort.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
modified 7-Sep-16 12:58pm.
|
|
|
|
|
The link to the Geeks With Blogs archive is broken; this works: [^].
btw, I agree with you that the C#6 "using static" namespace import syntax could, potentially, reduce code readability.
cheers, Bill
«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
|
|
|
|
|
It was working when I posted it. I think the hamsters stole the "x" from the end.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am working with vs2013 to develop a console apps. how to add localdb to db folder of my console project. Please tell me the way to add localdb in db folder of my project? thanks
tbhattacharjee
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
So try explaining in more detail exactly what you are trying to do, and why it's causing you a problem.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
What do you mean? Add an access database? That's easy. Copy one to your folder and then use a connection string.
Do you mean a sql database? Also easy, don't copy it to your folder, access it via the server and the proper connection string.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
|
Ah, probably.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
I am trying to receive values from a listbox and make a string which separates the items by ","
The following code does the job, but instead of the items, i get "System.Data.DataRowView" over and over.
Here is my code:
string[] items = listBox1.Items.OfType<object>().Select(item => item.ToString()).ToArray();
string result = string.Join(",", items);
label1.Text = result;
|
|
|
|
|
"Trying to receive values from a listbox"?
Are you trying to use a listbox to transfer values from one part of your program to another? This is bad practice.
Controls should never be used as a transport mechanism for data. Controls, like a ListBox, are a conduit through which you display parts of your data model to the user and are also used to get data from the user.
Since you're not getting the list of selected items from a user this leads me to believe you're doing something you should not be doing while trying to manage your data model.
A ListBox manages its items using a ObjectCollection class, which is essentially just a List<object> . What you did, when you added items to the ListBox, is added a bunch of DataRowView objects to it. When the ListBox displays these items, it called .ToString() on each of them to get some kind of text to display.
To convert the items in this list to a list of strings, you need to get an appropriate value of a field in each DataRowView. What happens in your code is that the objects in the ListBox are all cast to Object, which really doesn't have any methods or properties on them. The default .ToString() implentation of Object just returns the underlying type name. That's where you're getting the "System.Data.DataRowView" string from.
In order to treat these objects as a type you can actually do something with, you have to cast the objects to the type you can work with, instead of .OfType<object> it should be .OfType<DataViewRow> . Now you've got the methods and properties of a DataViewRow object to work with and your Select() code can be changed to pick an appropriate field/property to get a value from and return an actual value string instead of the type name.
|
|
|
|
|
Thank you Dave for your fast replay
I will learn your answer and hope
to finish my project
|
|
|
|
|
You're on the right track!
string items = string.Join(",", listBox1.Items.OfType<string>().Select(o => o));
Do not forget about Dave's notes.
|
|
|
|
|
No need for the trailing .Select(o => o) - OfType<T> already returns an IEnumerable<T> .
And since you're using .NET 4.5, you don't really need the .OfType<string>() either. There's an overload of string.Join which takes an IEnumerable<T> , and converts each item to a string.
string items = string.Join(",", listBox1.Items.Cast<object>());
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard, i agree with you, but i feel in duty to explain why a OfType<string>() has been used in my code.
Well, OP wrote:
Quote: The following code does the job, but instead of the items, i get "System.Data.DataRowView " over and over.
You're suggesting to use Cast<object>() , because string.Join() method is able to convert object to string.
The differences between OfType() and Cast() are well known. Using OfCast() is always safer way. That's way that method is much slower than Cast() .
Cheers!
Maciej
Using Cast Or OfType Methods[^]
|
|
|
|
|
|
It's the note half a tone above C. It is also the name of the language Microsoft invented to go along with the .NET framework.
This space for rent
|
|
|
|
|
You could have easily answered this yourself simply by typing "What is C#?" into Google.
|
|
|
|
|
Now we wait for the follow-up question, "what is Google?"
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
"C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run on the .NET Framework. " -- MSDN: Introduction to the C# Language and the .NET Framework[^]
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
it is the subject of this forum
|
|
|
|
|
Pithy as ever.
This space for rent
|
|
|
|
|
It's what you do after you eat a lot of carrots.
Java/javascript is what you do after your carrots are digested (my advice - be in a sitting position when you do it, and wash your hands afterwards).
".45 ACP - because shooting twice is just silly" - JSOP, 2010
- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010
- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
I am using a model that to be converted into json with Newtonsoft Json as follows
MyCars[
Car{
Name:""
Model:""
}
Car{
Name:"",
Model:""
}
]
but I am getting
MyCar[
Car[
{Name:"",
Model:""
}
{
Name:"",
Model:""
}
]
]
how to get first format?
|
|
|
|
|
This has nothing to do with C#. You should try the Newtonsoft forums.
|
|
|
|
|
You'll need to fix your C# model, which you haven't shown.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|