473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Passing values to a property in C#?

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
2 2268
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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
1
by: Matt M | last post by:
Ok. So I'm passing values between web pages, as per microsoft's framework development guide (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/ht...
7
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException...
2
by: craigkenisston | last post by:
Hi, I created an array of objects like this : object Values = {myObject.myprop, otherobject.otherprop, thirdobject.xprop}; Then I pass it to a method. and I get the values filled in that...
1
by: Russell | last post by:
Hi there, I'm currently creating a .NET Web Application and I have a question about passing values from one screen to another. I previously used Session variables in the code to store these...
8
by: Chris | last post by:
Hi, I have two froms (form1 and form2). I want to be able to pass values from form 1 to form2 and be able to use those values leter in form2. This is my code for form1 Private Sub...
2
by: o1j2m3 | last post by:
I created 2 forms, A and B using VB.NET 2003 in A, the codes are : ========================== public AsName as string public AsPrice as double public sub setValue(ByVal sname as string,...
7
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
8
by: =?Utf-8?B?UmF2aQ==?= | last post by:
Hi, I'm trying to pass values of different data-types to a web-service. I thought it would be easier to box these values and pass them as a System.object parameter, like public void...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.