By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

Passing values to a property in C#?

P: n/a
I am converting an application I wrote in VB.NET to C#. I am not a C#
guy, at all. I am having a problem passing values to a property in C#.
I understand that it is probably not the best way of achieving my
result, but it works in VB.NET, flawlessly.

Basically, I am storing a bunch of settings in a hashtable. Then,
setting/getting my values in the hastable with a public shared
property. Below is the property. The function GetSetting's return will
look similiar to this....

Return System.Web.HttpContext.Current.Application("config ")(item)
Public Shared Property Settings(ByVal Item As String) As String

Get
Return CType(GetSetting(Item), String)
End Get

Set(ByVal Value As String)

Try
System.Web.HttpContext.Current.Application("config ")(Item) = Value

Catch ex As Exception

GetSetting(Item)

System.Web.HttpContext.Current.Application("config ")(Item) = Value

End Try

End Set

End Property
When I convert to C#, Visual Studio is screaming at me. And here is my
C# version....

public static string Settings(string Item)
{
get
{
return ((string)(GetSetting(Item)));
}
set
{
try
{

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
catch (Exception ex)
{
GetSetting(Item);

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
}
}
If it was simply rewriting a few lines of code, I wouldn't have even
posted here. But, again, this is my method of setting/getting all my
application variables. So, if I have to change the way I am doing this,
I will have to do it everywhere in the application. Please help! :(

Jan 11 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi, what error message(s) are you getting?

j...@twdo.com wrote:
I am converting an application I wrote in VB.NET to C#. I am not a C#
guy, at all. I am having a problem passing values to a property in C#.
I understand that it is probably not the best way of achieving my
result, but it works in VB.NET, flawlessly.

Basically, I am storing a bunch of settings in a hashtable. Then,
setting/getting my values in the hastable with a public shared
property. Below is the property. The function GetSetting's return will
look similiar to this....

Return System.Web.HttpContext.Current.Application("config ")(item)
Public Shared Property Settings(ByVal Item As String) As String

Get
Return CType(GetSetting(Item), String)
End Get

Set(ByVal Value As String)

Try
System.Web.HttpContext.Current.Application("config ")(Item) = Value

Catch ex As Exception

GetSetting(Item)

System.Web.HttpContext.Current.Application("config ")(Item) = Value

End Try

End Set

End Property
When I convert to C#, Visual Studio is screaming at me. And here is my
C# version....

public static string Settings(string Item)
{
get
{
return ((string)(GetSetting(Item)));
}
set
{
try
{

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
catch (Exception ex)
{
GetSetting(Item);

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
}
}
If it was simply rewriting a few lines of code, I wouldn't have even
posted here. But, again, this is my method of setting/getting all my
application variables. So, if I have to change the way I am doing this,
I will have to do it everywhere in the application. Please help! :(
Jan 11 '07 #2

P: n/a
<jo***@twdo.comschrieb im Newsbeitrag
news:11*********************@i39g2000hsf.googlegro ups.com...
>I am converting an application I wrote in VB.NET to C#. I am not a C#
guy, at all. I am having a problem passing values to a property in C#.
I understand that it is probably not the best way of achieving my
result, but it works in VB.NET, flawlessly.

Basically, I am storing a bunch of settings in a hashtable. Then,
setting/getting my values in the hastable with a public shared
property. Below is the property. The function GetSetting's return will
look similiar to this....

Return System.Web.HttpContext.Current.Application("config ")(item)
Public Shared Property Settings(ByVal Item As String) As String

Get
Return CType(GetSetting(Item), String)
End Get

Set(ByVal Value As String)

Try
System.Web.HttpContext.Current.Application("config ")(Item) = Value

Catch ex As Exception

GetSetting(Item)

System.Web.HttpContext.Current.Application("config ")(Item) = Value

End Try

End Set

End Property
This is a parameterized property, wich doesn't exist in C#. But in C# exist
indexers, wich are equivalent to default properties in VB.Net.
>
When I convert to C#, Visual Studio is screaming at me. And here is my
C# version....

public static string Settings(string Item)
{
get
{
return ((string)(GetSetting(Item)));
}
set
{
try
{

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
catch (Exception ex)
{
GetSetting(Item);

System.Web.HttpContext.Current.Application["config"][Item] = value;
}
}
}

You could translate this as:
public static string this [string key]
{
get
{
return ((string)(GetSettings(key)));
}
set
{
try
{

System.Web.HttpContext.Current.Application["config"][key] = value;
}
catch (Exception ex)
{
GetSetting(Item);

System.Web.HttpContext.Current.Application["config"][key] = value;
}
}
}

To access ist use:
ClassName[key]
wich can be read and written like a propertie access.
ClassName is the name of the class the indexer is declared in. If the
indexer is not static, it is an instance expression of that class.

One deissadvantage is, that you can't distinguish them by name. But you can
overload them like methods.

Otherwise you have to declare the getter or setter as seperate methods.

hth
Jan 11 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.