I need to get a private string from a class, and count the frequency of words in the string.
the counting is the easy part... the bit I am struggling with is getting the string from the second class.
Heres what I am trying to get
public class GetString
{
private string myText = "this is the string that i need to get"
private string text;
public GetString()
{
text = myText
}
any and all help would be very much appreciated. I am also told I cannot edit this class
You have three options in my view:
You can make myText public.
You can return myText from another public member, preferably a property.
You can access the value via reflection (see: How to get the value of private field in C#?).
It seems that your getString class (by the way - bad naming a class with lowercase letter) contains a text property. If this property is public you can use it to get the string. If it is not public, there might be a method that exposes it. Your code is not complete, so it cannot be said for certain.
If there are no public properties or methods that expose the string, then the only way to get is through reflection
This seems rather fundemental, you have nothing in your getString class returning the string. Try something like
public class getString
{
private string myText = "this is the string that i need to get"
public String getString()
{
return myText
}
getString a = new getString();
String hiddenString = a.getString();
You cannot use the constructor for this purpose. Try this:
public class Foo
{
private string myText = "this is the string that i need to get";
public Foo()
{
}
public String GetString()
{
return this.myText;
}
}
Every method should either have the void keyword or the return type, which is in your case a String.
Use Properties (property is a member that provides a flexible mechanism to read, write, or compute the value of a private field.):
public string MyText
{
get { return myText; }
private set { myText = value; }
}
To follow on from #Petrichor, you could also potentially use interfaces:
public interface IHasText
{
string GetPrivateText();
}
public class GetString : IHasText
{
private string myText = "this is the string that i need to get";
string IHasText.GetPrivateText()
{
return myText;
}
}
var val = new GetString();
var asInterface = (IHasText)val;
string text = asInterface.GetPrivateText();
Related
i have a question.
I would like to do something like that:
[PutStars]
public string telephone
where PutStars could be a custom attribute for example.
PutStars acts on the string, so it replace telephone value [333-123456789] and when getting value, it retrieves for example [333-12xxxx789].
Is it possible?
Thanks a lot!
Well, you can implement a helper method and call it when getting the value:
private string _tel;
public string Tel
{
set{ _tel = value; }
get {
return _tel.PutStars();
}
}
public static string PutStars(this string str)
{
return str.Replace("1", "*");
}
Alternatively when you get the string you can do:
var starred = myObj.Tel.PutStars();
The closest you'll get to that inbuilt will probably be [PasswordPropertyText], but a: that is intended to mask an entire field, and b: it depends entirely on the UI framework you are using looking for this attribute; nothing is automatic in attributes. Your best bet, frankly, is probably to add a second property that you use for UI binding:
public string Telephone {get;set;}
public string TelephoneMasked {
get { /* your code here */ }
}
and bind to TelephoneMasked.
i am returning a list from a database like this;
ClsCampus campus = new ClsCampus();
List<ClsCampus> camp = campus.GetCampusAll();
Utility.BindComboBox(ComboBoxCampus, camp, "CampusName", "CampusId");
I have a class name Utillity, in which i have created a static method called BindComboBox.
public static void BindComboBox(DropDownList listName, List<Object> list,
string textField, string valueField)
{
listName.DataTextField = textField;
listName.DataValueField = valueField;
listName.DataSource = list;
listName.DataBind();
}
However, it gives me a compilation error.
So, How do i write a general purpose method where i can bind a generic list of records to a combobox
Error 1
The best overloaded method match for 'KenMISSchool.Repository.Utility.BindComboBox(System.Web.UI.WebControls.DropDownList, System.Collections.Generic.List<object>, string, string)' has some invalid arguments D:\Projects\KenMISSchool\Web\forms\student\registration.aspx.cs 20 9 Web
You are passing List<ClsCampus> but method expects List<Object> so I would change it to generic way:
public static void BindComboBox<T>(DropDownList listName, List<T> list, string textField, string valueField)
{
listName.DataTextField = textField;
listName.DataValueField = valueField;
listName.DataSource = list;
listName.DataBind();
}
and call it like this:
Utility.BindComboBox<ClsCampus>(ComboBoxCampus, camp, "CampusName", "CampusId");
We had a similar idea, but solved it in another way. I'm not sure if it will work for you, but here's a suggestion:
We implemented an Interface called IDropdownable (not the best name, I know), that forced two properties:
public string dropdown_value;
public string dropdown_text;
And we then implemented this interface over all classes that needed to be easily bound to dropdownlists:
public class Foo : IDropdownable
{
//Let's say these fields are already in the class:
private int _id;
private string _name;
private DateTime _date;
//The interface was then implemented:
public string dropdown_value
{
get
{
return this._id.ToString();
}
}
public string dropdown_text
{
get
{
return String.Format("{0} ({1})", this._name, this._date.Year);
//Or simpler: return this._name;
}
}
}
The only thing left was to create a generic function that takes in a IDropdownable parameter and outputs the dropdownlist the way you like it (HTML string, SelectList, List<SelectListItem>, ... many options there).
public List<SelectListItem> GenerateDDL(
List<IDropdownable> items,
string selected_value)
{ ... }
If you want I can give you an approximation of the function.
Comment
We did this in MVC2 ASP.Net 4. To be honest, I'm not sure what type should be returned in classic ASP.NET, but I assume some type of List/Array will work.
So I have this struct:
public struct PurchaseOrderStatus {
public const string Open = "Open", Received = "Received";
}
How do I convert if I have the following:
string status = "Open";
To:
PurchaseOrderStatus.Open;
By Convert, I mean, how do I do this:
PurchaseOrderStatus POS;
String status = "Open";
POS = status;
I would suggest using "smart enums" here:
public sealed class PurchaseOrderStatus
{
public static readonly PurchaseOrderStatus Open =
new PurchaseOrderStatus("Open");
public static readonly PurchaseOrderStatus Received =
new PurchaseOrderStatus("Received");
private readonly string text;
public string Text { get { return value; } }
private PurchaseOrderStatus(string text)
{
this.text = text;
}
}
You can store arbitrary information here, including text which wouldn't be valid identifiers (so long as it doesn't change, of course). It's still strongly typed (unlike your strings) and you can give other behaviour to it. You can even create subclasses if you remove the sealed modifier and add the subclasses as nested classes so they still have access to the private constructor.
Oh, and there's a genuinely limited set of values (the ones you've defined and null) unlike with regular enums.
The only downside is that you can't switch on it.
I have just written a small piece of code and it struck me that I am not sure what method of initialisation is best practice when it comes to initialising my member variables which will be exposed and used via properties. Which is the best way to initialise my member variables out of the two examples below and more importantly why?
Example 1:
private string m_connectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString;
private string m_providerName = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ProviderName;
public string ConnectionString
{
get { return m_connectionString; }
set { m_connectionString = value; }
}
public string ProviderName
{
get { return m_providerName; }
set { m_providerName = value; }
}
public EntityClusterRefreshServiceDatabaseWorker()
{
}
Example 2:
private string m_connectionString;
private string m_providerName;
public string ConnectionString
{
get { return m_connectionString; }
set { m_connectionString = value; }
}
public string ProviderName
{
get { return m_providerName; }
set { m_providerName = value; }
}
public EntityClusterRefreshServiceDatabaseWorker()
{
ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString;
ProviderName = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ProviderName;
}
NOTE: Assume that I am not using these variables in a static context.
It really doesn't matter which of those you use, except in the very odd situation where a base class constructor calls an overridden member, in which case the timing would change: instance variable initializers are run before the base class constructor call, whereas obviously the constructor body is executed afterwards.
In the latter case, you can make your code a lot simpler though using automatically implemented properties:
public string ConnectionString { get; set; }
public string ProviderName { get; set; }
public EntityClusterRefreshServiceDatabaseWorker()
{
// Code as before
ConnectionString = ...;
ProviderName = ...;
}
You can't do this with the first form, as automatically implemented properties don't have any way of specifying an initial value.
(You may also want to consider making the setters private, but that's a separate concern.)
You are essentially doing the same thing but writing it in a different form.
I always prefer (and use) the second aprroach because I don't like methods being executed in the middle of nowhere. It's better to split things. Attributes are declared on class body and initialized on class constructor.
As long as the connection strings are not supposed to be changed, you can initialize them as static readonly:
private readonly static string m_connectionString = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ConnectionString;
private readonly static string m_providerName = ConfigurationManager.ConnectionStrings["ApplicationDefault"].ProviderName;
readonly variables are allowed to be initialized only in class declaration/contructor and are better optimized for performance than regular private variables.
And back on the question - it really doesn't matter where you'll initialize these.
Drop the fields and go for automatic properties & make your setters private.
public string ConnectionString {get; private set;}
public string ProviderName {get; private set;}
Rob
I have a class in test.cs in which I have a string value string user="testuser". I want to use the test.cs's user value in another class. How can I do this?
Declare the string public:
public string user = "testuser";
Then you can access it from another class via
Test.user
However, depending on what exactly you want, you should perhaps make the field read-only:
public readonly string user = "testuser";
Or use a property, bound to a backing field:
public string User
{
get { return this.user; }
}
In fact, properties are the canonical way of making information accessible from the outside except for very few, very special cases. Public fields are generally not recommended.
As Ant mentioned in a comment, there is also the option of making it a constant (assuming it is, in fact, a constant value):
public const string user = "testuser";
Make a public property.
Public string TestUser
{
get { return testUser;}
}
You should make a property of user and expose this to any other class that want to read or write it's value.
class MyClass
{
private static string user;
public static string User
{
get { return user; }
set { user = value; }
}
}
class MyOtherClass
{
public string GetUserFromMyClass()
{
return MyClass.User;
}
}
public class AClass
{
// declarations
private string _user = "testUser";
// properties
public string User { get { return this._user;} set { this._user = value; } }
}
then call your class property, e.g.
AClass myClass = new AClass();
string sYak = myClass.User;
As suggested in the earlier answers, making "user" into a Property is the ideal technique of accomplishing this. However, if you want to expose it directly anyhow, you should use static to avoid having to instantiate an object of that class. In addition, if you don't want the demo class to manipulate the value of user, you should declare is readonly as well, like below
public static readonly user="text user";